savefig返回空白图像

时间:2020-05-27 01:09:46

标签: python pandas matplotlib savefig

我正在尝试使用pandas绘图功能来绘制pandas数据帧(result_m),但是当我尝试使用savefig保存该图但它返回空白pdf时。它在笔记本窗口中绘制良好。不知道我在做什么错

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

1 个答案:

答案 0 :(得分:0)

问题在于您创建的绘图不在您创建(并保存)的图形上。在第二行:

Map<String, Integer> tempMap = getMap(someFilePath, Map<String, Integer>)
// or
Map<String, SomeClass> tempMap = getMap(someFilePath, LinkedHashMap<String, SomeClass>)

pandas创建了一个新图形,因为您没有提供轴(ax = result_m.plot( kind='line', figsize=(20, 10),fontsize=15) )参数。请参阅plotting to specific subplots上的pandas文档。

您可以通过跳过图形创建步骤,然后从轴对象获取熊猫创建的图形来解决此问题:

ax

或通过将绘图添加到您创建的图形中,首先创建一个子绘图:

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

相关问题