问题描述
在使用pandas绘图时,如果纵坐标标识过长会导致其只显示部分,而调整图像的大小并不能完美解决问题。

问题原因及解决方法
根据官方文档可以发现,pandas.DataFrame.plot()函数返回了一个matplotlib.axes.Axes,因此问题就是matplotlib搞的鬼,需要使用它的方法来解决。在matplotlib文档中,有一个参数bbox_inches可以尝试保存更紧致的图形,因此,在保存图形时需要加入该参数,设置为”tight”,即可解决问题。
部分代码
以下是示例代码:
df_sparsity = distiller.weights_sparsity_summary(resnet20)
df_sparsity_tmp = df_sparsity[['NNZ(dense)', 'NNZ(sparse)']]
ax = df_sparsity_tmp.iloc[0:-1].plot(kind='bar', figsize=[30,10], title="Sparse vs.Densen(element-wise)")
ax.set_xticklabels(df_sparsity.Name, rotation=90)
fig = ax.get_figure()
fig.savefig('output.png', bbox_inches='tight')
使用该代码会得到完美的图形,标签完全显示。
原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/6926.html
