Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subplots on one figure
#1
I'm very new to Python so apologies first for being so simple and secondly if I ask for help in the wrong way. I will get there. I have a script working that goes through a loop plotting 13 test data on one subplot for 40 locations, making 40 graphs with 13 lines. The code runs fine but when I try to put these all on figure (so I have 40 graphs arranged in a 45 panel formation) it goes wrong. I get my 40 axes in a 45 formation but they have no data in them and the axes scale incorrectly to what I have asked it to do. My original working code for the individual figures was:
for j in range(40): #loop through locations
fig, ax = plt.subplots() #set up for plot
LocName = alldata_pd.iloc[1,1+j*41]

for i in range(13): ##loop through each time for a single location

    Depths = alldata_pd.iloc[16:34,3+j*41+i*3]
    Temps = alldata_pd.iloc[16:34,5+j*41+i*3] 
    TimeName = alldata_pd.iloc[15,5++j*41+i*3]

    ax.plot(Temps,Depths,color=plotcolours[i],label=TimeName)

#set up plot axes etc
ax.legend(loc='lower left')
ax.set_ylabel('Depth (mbgl)',fontsize=10)
ax.set_xlabel('Temperature $^\circ$C',fontsize=10)
ax.set_title(LocName)
ax.set_xlim(5,20)
ax.set_ylim(22,0)

#save figure
fig_dir = data_dir
plt.savefig(fig_dir+'\\'+LocName+'.png',dpi=300)
I thought I could get the desired result by simply changing this line:

fig, ax = plt.subplots()

to

fig, ax = plt.subplots(4,5)

But clearly not. I've tried the usual places and the matplotlib site suggests this is the correct way to do it but as I said I am brand new to this so I am probably missing something obvious and it's very hard to know where you are going wrong when its a new skill. I may not even understand the answer if someone gives it to me but I have to learn. There are no errors shown in the command panel. Thanks.
Reply


Messages In This Thread
Subplots on one figure - by SarahColombo96 - Feb-09-2019, 01:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,713 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,744 Aug-19-2022, 11:07 AM
Last Post: dm222
Question Error with MatPlotLib subplots Danno 8 3,475 Apr-01-2022, 12:23 AM
Last Post: Danno
  how to create subplots in for loop? python_newbie09 1 5,931 Sep-25-2019, 02:29 PM
Last Post: stullis
  Change distance and title postion in subplots Mark3232 2 2,856 Jul-19-2018, 10:10 AM
Last Post: Mark3232
  Help with plot legends in subplots Felipe 2 5,883 Feb-06-2017, 02:55 PM
Last Post: Felipe

Forum Jump:

User Panel Messages

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