简体   繁体   中英

Matplotlib subplots Figure size

I'm attempting to decrease the size of my figures with a title and legend attached. While the actual figure ends up to my liking, the legend remains large and title runs off of the image. Included in an example of one of my plots.

在此处输入图片说明

Below is my code that plots this data. Does anyone have suggestions to make this look cleaner? Thanks!

fig, ax = plt.subplots(figsize=(3,2.25))
ax.plot(df3['difference'],'r-',label="Observations")
ax.plot(df4['difference'],'b-',label='MERRA')
ax.set_xlim(0,205)
ax.set_ylim(-60,60)
plt.xlabel('Year')
plt.ylabel('Snow Depth Departures(cm)')
plt.title('Station '+str(stations[c])+' Snow Depth Correlations R='+str("%0.2f"%corr[0])+'')
ax.autoscale(False)
ax.set_xticks(np.arange(0,193,48))
ax.set_xticklabels(['1979','1983','1987','1991','1995'])
plt.legend(loc='best')

#plt.show()
plt.savefig('Z:/Dan/'+str(stations[c])+'CorrPlot.png')

I think you almost had it. Try setting xlabel , ylabel , title , and legend on ax .

fig, ax = plt.subplots(figsize=(3,2.25))
ax.plot(df3['difference'],'r-',label="Observations")
ax.plot(df4['difference'],'b-',label='MERRA')
ax.set_xlim(0,205)
ax.set_ylim(-60,60)
ax.set_xlabel('Year')
ax.set_ylabel('Snow Depth Departures(cm)')
ax.set_title('Station '+str(stations[c])+' Snow Depth Correlations R='+str("%0.2f"%corr[0])+'')
ax.autoscale(False)
ax.set_xticks(np.arange(0,193,48))
ax.set_xticklabels(['1979','1983','1987','1991','1995'])
ax.legend(loc='best')

Here's what I came up with (with random data):

import matplotlib.pyplot as plt
import numpy as np

diff1 = np.random.randint(-50, 40, 193)
diff2 = np.random.randint(-55, 40, 193)
corr = [0.5]

fig, ax = plt.subplots(figsize=(3,2.25))
ax.plot(diff1,'r-',label="Observations")
ax.plot(diff2,'b-',label='MERRA')
ax.set_xlim(0,205)
ax.set_ylim(-60,60)
label = ax.set_xlabel('Year', fontsize=8)
ax.xaxis.set_label_coords(1.06, 0)
label = ax.set_ylabel('Snow Depth Departures(cm)', fontsize=8)
ax.yaxis.set_label_coords(-0.087, 0.5)
plt.title('Station 5555555 \nSnow Depth Correlations R='+str("%0.2f"%corr[0])+'', fontsize=10, y=0.875)
ax.autoscale(False)
ax.set_xticks(np.arange(0,193,48))
ax.set_xticklabels(['1979','1983','1987','1991','1995'])
plt.tick_params(axis='both', which='major', labelsize=6)
leg = plt.legend(loc=4, fontsize=8)
leg.get_frame().set_alpha(0.85)

plt.savefig('CorrPlot.png')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM