Sunday 15 September 2013

python - Pandas: plotting two histograms on the same plot -



python - Pandas: plotting two histograms on the same plot -

i have 2 histograms appear on same plot (with different colors, , perchance differente alphas). tried

import random x = pd.dataframe([random.gauss(3,1) _ in range(400)]) y = pd.dataframe([random.gauss(4,2) _ 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 result in 4 different plots. how can have them on same one?

if understood correctly, both hists should go same subplot. should be

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

you can pass pandas plot method axis object, if want both kde's in 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 single plot need 2 different y-scales since kde density , histogram frequency. utilize 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')

python pandas histogram

No comments:

Post a Comment