首页 > 解决方案 > 为什么保存我的 matplotlib 图像时,标题没有出现?

问题描述

我做了一个阴谋。我设定了自己的传奇。当我保存图像时,标题没有出现。我不知道为什么。

这是我的代码:

ax = plt.gca()
df1_MAXN.plot(kind = 'line', y = 'MemUsed', ax=ax, legend=True, label='MAXN')
df1_MAXQ.plot(kind = 'line', y = 'MemUsed', color='red', ax=ax, legend=True, label='MAXQ')


#Annotate the max values using the data coordenates
ax.annotate('3284.97',xy=(70000, 3284.97), xycoords='data')
ax.annotate('2903.8',xy=(70000, 2903.8), xycoords='data')

#Remove top and right axis
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

#The legend
myLegend=plt.legend(bbox_to_anchor=(0., 1.1, 1., .102), prop ={'size':10}, loc='center',frameon=False, ncol=2, title=r'TX2 (GPU) USED MEMORY - I_V1')                    
myLegend.get_title().set_fontsize('14')


ax.set_xlabel("NUMBER OF INFERENCES")
ax.set_ylabel("USED MEMORY (MB)")
plt.savefig('Memory_Iv1_TX2.png', dpi = 1000)
plt.show()

这是保存的图像的样子: 在此处输入图像描述

应该是这样的:

在此处输入图像描述

标签: pythonpandasmatplotlibdata-visualization

解决方案


我刚刚找到了答案。我补充说bbox_inches='tight。现在标题出现在保存的图像中:)

#Save image removing the whitespace around it
plt.savefig('Memory_Iv1_TX2.png', bbox_inches='tight', dpi = 1000)
plt.show()

推荐阅读