I want to show Two Figure in python using matplotlib.pyplot but its either 1 didn't work or both didn't work

33 views Asked by At

1st Plot

>    `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()

2nd Plot

`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=DayTimeMonth.Month_Name_oprational(Month) dft="{}-{}.csv".format(MN,Year) try: df2=pd.read_csv(dft) Handling File not Found error except FileNotFoundError: print("No record Found for {}") break Handeling Empty data error except pd.errors.EmptyDataError: print("Oprations Cannot be performe on empty CSV file") break

Setting Index for proper ploting

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

I am expecting this segment of code to show 2 graphs side by side not like one at a time

1

There are 1 answers

0
max_jump On

To show two plots next to each other, you need to call plt.subplots rather than plt.figure, e.g.

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

Also check this demo on subplots: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html