Search code examples
pythonpandasplotrowsscatter

Plotting a scatter plot of each 15 rows of a dataframe?


I want to create a scatter plot of my data frame which contains 800 rows. Instead of plotting them in a whole graph, I want to separate the graph by 15 rows of my data frame. I supposed to see 54 graphs as the result. How can I do this by using python?


Solution

  • I created some random data with two columns in a dataframe. You can then use numpy to loop through every 15 rows of your dataframe (don't use DataFrame.iterrows as it is extremely inefficient), and created a simple scatter plot for each chunk of data.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    np.random.seed(1234)
    data = np.random.rand(800,2)
    df = pd.DataFrame(data=data, columns=['x','y'])
    
    ## split the dataframe into 54 chunks, this is hardcoded since you specified that your dataframe contains 800 rows = 15 rows x 54
    for index, df_chunk in enumerate(np.array_split(df, 54)):
        plt.scatter(df_chunk.x, df_chunk.y)
    
        ## this will immediately save 54 scatter plots, numbered 1-54, be warned! 
        plt.title('Scatter Plot #' + str(index+1))
        plt.savefig('./scatter_plot_' + str(index+1) + '.png')
    
        ## clear the figure
        plt.clf()
    

    Below I have included one of the 54 scatterplots created. Feel free to modify the title, x- and y-axis titles, marker color and type as you like.

    enter image description here