Home > Software engineering >  How to plot bar charts in separate plots in Python
How to plot bar charts in separate plots in Python

Time:11-12

I have multiple bar charts that I want to plot in their own figure separately but they all plot on the same figure.

f1 = plt.bar(values, stats_list_running, width, label='Running time')
f2 = plt.bar(values, stats_list_noofact, width, label='No. of activations')
f3 = plt.bar(values, stats_list_avgstract, width, label='Avg. strength of activations')
f4 = plt.bar(values, stats_list_avglenact, width, label='Avg. length of activations')
f5 = plt.bar(values, stats_list_avgtimbet, width, label='Avg. time between activations')


plt.xlabel('Tests')
plt.ylabel('Values')
plt.title('Session results')
plt.show()

I tried using subplot but got an error that this can't be done on a BarContainer. Any help appreciated.

CodePudding user response:

In your case, you can use plt.subplots to create a sequence of subplots, and use the Axes method to draw your figure, such as:

fig, ax = plt.subplots(2, 3)
ax[0,0].bar(...)
ax[0,1].bar(...)
# and so on

If you want to draw a bar per figure, then you need to create a new Figure instance for each of your bar chart.

  • Related