Pandas酒吧阴谋到纽比阵列

2024-09-25 00:24:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将条形图图形转换为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}$

Tags: 代码图像numpy绘图dfpngchartfig
1条回答
网友
1楼 · 发布于 2024-09-25 00:24:07

那是因为你的fig确实是空的。您可以通过plt.show()代替plt.savefig('curr_bar_chart.png')来查看:

df = pd.DataFrame.from_dict(data)    

fig = plt.figure()
fig.add_subplot(1, 1, 1)

df.plot.bar()

plt.show()

您将看到两个数字,而第一个(空)是您的fig。要解决这个问题,可以将fig的轴传递给pandas bar plot。然后您将得到一个单独的绘图,即您的fig

^{pr2}$

相关问题 更多 >