Python Forum

Full Version: For loop that prints four subplots
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
I am new to the python world and I was able to make some progress with my code. I have a relative large dataframe with around of 400+ rows. What I am trying to do is to split based on a chunkSize to create set of four sublots (I picked four subplots [2,2] since I believe that are still presentable on a big screen.

What I need your help is on how to make 2X2 subplots and also save them on the file on the place I mark you with comments below in the code.

totalRows=data_1.shape[0] 
data_1.drop(data_1.index[0],inplace=True) # data_1 contains a single erroeneous measurement. remove it
chunkSize=300 # set chunk size so it creates at least for figures
selectPeriods=chunkSize//3 # three periods are showed.
for i in range(1,totalRows,chunkSize): 
    print(i)
    # Here I need help
    #if ((i// chunkSize) % 4==0):# when this is zero try a new set of subplots. It gets values of 0,1,2,3
        #then start a new set of four subplots

    plt.figure()
    plt.plot(data_1['All-Latitude'][i:i+selectPeriods].values, data_1['All-Longitude'][i:i+selectPeriods].values,color="red")
  

    plt.xlim(min(data_1['All-Latitude']),max(data_1['All-Latitude']))
    plt.ylim(min(data_1['All-Longitude']), max(data_1['All-Longitude']))
Check out fig.add_subplot to get desired result. Your code in this case might look as follows:

index = 1
fig = plt.figure()

for i in range(1,totalRows,chunkSize): 
    print(i)
    # Here I need help
    #if ((i// chunkSize) % 4==0):# when this is zero try a new set of subplots. It gets values of 0,1,2,3
        #then start a new set of four subplots
    ax = fig.add_subplot(2, 2, index)
    ax.plot(data_1['All-Latitude'][i:i+selectPeriods].values, data_1['All-Longitude'][i:i+selectPeriods].values,color="red")
    ax.set_xlim(min(data_1['All-Latitude']),max(data_1['All-Latitude']))
    ax.set_ylim(min(data_1['All-Longitude']), max(data_1['All-Longitude']))
    
    index += 1
    
    # if some condition, e.g. index == 4
    # something like this:
    if index == 4:
        index = 1
        fig = plt.figure()
great it works thanks! I did some small changes on your suggestions and I can see the subplots.
One more question can I change the indexing of the subplots so to read them by row instead of by column that are now?

How to save those figures then?

Thanks
Alex
(Jul-02-2019, 06:26 AM)dervast Wrote: [ -> ]One more question can I change the indexing of the subplots so to read them by row instead of by column that are now?

You can define a mapper variable, e.g. mapper = {1:1, 2: 3, 3:2, 4:4}
and pass mapper[index] instead of raw index to add_subplot method.

(Jul-02-2019, 06:26 AM)dervast Wrote: [ -> ]How to save those figures then?

plt.savefig utility function saves current figure to a file. Use it just before fig = plt.figure() in the if-condition.
thanks. I have finished this part.
Alex