Matplotlib子图图形大小

时间:2015-05-05 19:25:31

标签: python matplotlib figure

我试图通过附加的标题和图例来减小我的数字的大小。虽然实际数字最终符合我的喜好,但图例仍然很大,标题会从图像中消失。包含在我的一个情节的示例中。

enter image description here

下面是我的代码,用于绘制此数据。有没有人建议让它看起来更干净?谢谢!

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')

2 个答案:

答案 0 :(得分:1)

我觉得你差不多了。尝试在xlabel上设置ylabeltitlelegendax

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')

答案 1 :(得分:1)

这是我提出的(随机数据):

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')

enter image description here