8

I would like to have 2 histograms to appear on the same plot (with different colors, and possibly differente alphas). I tried

import random
x = pd.DataFrame([random.gauss(3,1) for _ in range(400)])
y = pd.DataFrame([random.gauss(4,2) for _ in range(400)])


x.hist( alpha=0.5, label='x')
y.hist(alpha=0.5, label='y')
x.plot(kind='kde', style='k--')
y.plot(kind='kde', style='k--')

plt.legend(loc='upper right')
plt.show()

This produces the result in 4 different plots. How can I have them on the same one?

meto
  • 3,425
  • 10
  • 37
  • 49

2 Answers2

9

If I understood correctly, both hists should go into the same subplot. So it should be

fig = plt.figure()
ax = fig.add_subplot(111)
_ = ax.hist(x.values)
_ = ax.hist(y.values, color='red', alpha=.3)

You can also pass the pandas plot method an axis object, so if you want both kde's in another plot do:

fig = plt.figure()
ax = fig.add_subplot(111)
x.plot(kind='kde', ax=ax)
y.plot(kind='kde', ax=ax, color='red')

To get everything into a single plot you need two different y-scales since kde is density and histogram is frequency. For that you use the axes.twinx() command.

fig = plt.figure()
ax = fig.add_subplot(111)
_ = ax.hist(x.values)
_ = ax.hist(y.values, color='red', alpha=.3)

ax1 = ax.twinx()
x.plot(kind='kde', ax=ax1)
y.plot(kind='kde', ax=ax1, color='red')
rustil
  • 136
  • 4
  • Thanks, this almost works! I now have both histograms in a single plot and both kde's in another one. How can I merge all of them in the same figure? Running all your code, without the second `fig = ...` still gives me 2 different plots. – meto Nov 14 '14 at 12:51
  • Well the thing is that kde (density) has another scale as histogram (frequency) so you have to introduce a second y-scale by using `ax.twinx()` like here: [link](http://matplotlib.org/examples/api/two_scales.html). Will update my answer in a sec – rustil Nov 21 '14 at 09:22
  • The problem with the first solution in that the counts are determined by the bin width. If they are different, the vertical axes are incomparable. – user48956 Apr 24 '17 at 21:44
  • @rustil is this solution still viable? I don't think so. – Olu Adeyemo Feb 21 '19 at 07:28
1

You can use plt.figure() and the function add_subplot(): the first 2 arguments are the number of rows and cols you want in your plot, the last is the position of the subplot in the plot.

fig = plt.figure()
subplot = fig.add_subplot(1, 2, 1)
subplot.hist(x.ix[:,0], alpha=0.5)
subplot = fig.add_subplot(1, 2, 2)
subplot.hist(y.ix[:,0], alpha=0.5)
polku
  • 1,575
  • 2
  • 14
  • 11