我正在尝试使用pandas绘图函数绘制pandas数据帧(result_m),但是当我尝试使用savefig保存图形时,它返回一个空白的pdf。它在notebook窗口中打印正常。不知道我做错了什么
fig = plt.figure()
ax = result_m.plot( kind='line', figsize=(20, 10),fontsize=15)
ax.set_title('Harkins Slough Diversions',fontsize= 20)
ax.set_xlabel( "Date",fontsize=18)
ax.set_ylabel("cubic meters",fontsize=18)
plt.legend(fontsize=15)
fig.savefig(os.path.join(outPath4,'plot_fig.pdf'))发布于 2020-05-27 10:31:09
问题是您创建的绘图不在您创建(并保存)的图形上。在第二行中:
ax = result_m.plot( kind='line', figsize=(20, 10),fontsize=15)因为您没有提供轴(ax)参数,所以pandas会创建一个新的图形。请参阅plotting to specific subplots上的熊猫文档。
您可以通过跳过图形创建步骤,然后从axis对象获取pandas创建的图形来修复此问题:
ax = result_m.plot( kind='line', figsize=(20, 10),fontsize=15)
fig = ax.figure或者将绘图添加到您创建的图形中,首先创建一个子图:
fig = plt.figure(size=(20, 10))
ax = fig.add_subplot(111)
ax = result_m.plot( kind='line', fontsize=15, ax=ax)请注意,在此选项中,在创建图形时定义图形的size属性,而不是将figsize传递给DataFrame.plot。
https://stackoverflow.com/questions/62033877
复制相似问题