我正在尝试将条形图图形转换为numpy数组。 我使用以下代码执行此操作:
df = pd.DataFrame.from_dict(data)
fig = plt.figure()
fig.add_subplot(1, 1, 1)
df.plot.bar()
plt.savefig('curr_bar_chart.png')
numpy_array = fig2data(fig)
plt.close()
im = data2img(numpy_array)
在问题的最后,我还附上了fig2data
和{
我的问题是:
保存的图像(curr_bar_chart.png
)显示得很好,但是当使用im.show()
查看最终图像时,我得到了一个没有任何数据的绘图(即带轴的空绘图)。在
这非常令人费解,因为这个设置适用于我在其他地方使用的其他matplotlib绘图。在
正如承诺的那样,代码的其余部分:
^{pr2}$
那是因为你的
fig
确实是空的。您可以通过plt.show()
代替plt.savefig('curr_bar_chart.png')
来查看:您将看到两个数字,而第一个(空)是您的
^{pr2}$fig
。要解决这个问题,可以将fig
的轴传递给pandas bar plot。然后您将得到一个单独的绘图,即您的fig
:相关问题 更多 >
编程相关推荐