Python Forum
For loop that prints four subplots
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop that prints four subplots
#1
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']))
Reply
#2
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()
Reply
#3
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
Reply
#4
(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.
Reply
#5
thanks. I have finished this part.
Alex
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create matplotlib subplots from figures vitaly 3 3,087 Mar-02-2020, 12:58 AM
Last Post: scidam
  Dynamically placing axes in subplots via dataframe plot naftis 1 2,920 Jul-29-2019, 12:10 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020