3

I am trying to plot n number of histograms and show them all together side by side (not in the same histograms.

I have tried this code:

for r in range(1, n):
    plt.hist(combined['apple{} tomato'.format(n)], bins = bin, alpha=0.5, color='#0bf9ea')
    plt.hist(combined['apple{} potato'.format(n)], bins =  bin, alpha=0.5, color='#ff7fa7')
    plt.show()

When I type this code, it will show me histogram 1, then when I close the figure, histogram 2 will appear and so on until histogram n.

for r in range(1, n):
    plt.hist(combined['apple{} tomato'.format(n)], bins = bin, alpha=0.5, color='#0bf9ea')
    plt.hist(combined['apple{} potato'.format(n)], bins =  bin, alpha=0.5, color='#ff7fa7')
plt.show()

However, when I try this code, it shows me one histogram with all the histograms in one.

Is there a way to show all n different histograms separately but in one window ?

Thanks :D

pcu
  • 1,204
  • 11
  • 27
Flora
  • 235
  • 2
  • 6
  • 11

2 Answers2

3

Have you tried using matplotlib.pyplot.subplot()?

 for r in range(1,n): 
    matplotlib.pyplot.subplot(nrows, ncols, r) 
    plt.hist() 
Ellen
  • 85
  • 4
2

Try this:

a = plt.subplots(n-1, 2)[1].ravel()

for r in range (1, n):
    a[0].hist(combined['apple{} tomato'.format(n)], bins = bin, alpha=0.5, color='#0bf9ea')
    a[1].hist(combined['apple{} potato'.format(n)], bins =  bin, alpha=0.5, color='#ff7fa7')

plt.show()
gommb
  • 1,121
  • 1
  • 7
  • 21