我想使用matplotlib.pyplot在python中显示Two Figure,但它要么1不起作用,要么两者都不起作用

rjee0c15  于 2023-02-23  发布在  Python
关注(0)|答案(1)|浏览(272)

第1个图

>    `This code is under a while loop`
>    `This is the code for 1st graph`
>    `it works fine until i added plt.figure()`
a1=plt.figure(1)
df.set_index("Date",inplace=True)
df[["inflow","outflow"]].plot(kind='bar',color=['Green','Red'])
plt.xlabel("Date")
plt.ylabel("Amount")
plt.title(file)
plt.subplots_adjust(bottom=0.25)
a1.show()

第二个图

`This is code for 2nd graph and main problem it did show simultaneously`
`But it is always empty even though i copied same data in both file`
a2=plt.figure(2)
Year = int(input("Which Years Data You Want to Visualise (YYYY): "))
Month = int(input("Enter Which month of above mentioned Year (MM): "))

This is a self main modula only used to convert No. of month to Their Name MN=日时间月.月名称操作(月)dft="{}-{}.csv”.格式(MN,年)尝试:df 2 =pd.read_csv(dft)Handling File not Found error,文件未找到错误除外:print(“未找到{}的记录”)中断Handeling Empty data error,除了pd.错误。空数据错误:print(“无法对空CSV文件执行操作”)中断

设置索引以正确绘图

df2.set_index("Date",inplace=True)
`this the line on which basis the graph has to be plot`
df2[["inflow","outflow"]].plot(kind='bar',color=['Green','Red'])
plt.xlabel("Date")
plt.ylabel("Amount")
plt.title(file)
plt.subplots_adjust(bottom=0.25)
a2.show()
input()
break

我希望这段代码并排显示2个图形,而不是一次显示一个图形

62lalag4

62lalag41#

要显示相邻的两个图,需要调用plt.subplots而不是plt.figure,例如

fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, z)

还可以查看此演示的子图:https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html

相关问题