Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to plot 2 histograms side by side?

I have 2 dataframes. I want to plot a histogram based on a column 'rate' for each, side by side. How to do it?

I tried this:

import matplotlib.pyplot as plt
plt.subplot(1,2,1)

dflux.hist('rate' , bins=100) 
plt.subplot(1,2,2) 
dflux2.hist('rate' , bins=100) 
plt.tight_layout() 
plt.show() 

It did not have the desired effect. It showed two blank charts then one populated chart.

like image 288
Mark Ginsburg Avatar asked Jul 13 '17 01:07

Mark Ginsburg


1 Answers

Use subplots to define a figure with two axes. Then specify the axis to plot to within hist using the ax parameter.

fig, axes = plt.subplots(1, 2)

dflux.hist('rate', bins=100, ax=axes[0])
dflux2.hist('rate', bins=100, ax=axes[1])

Demo

dflux = pd.DataFrame(dict(rate=np.random.randn(10000)))
dflux2 = pd.DataFrame(dict(rate=np.random.randn(10000)))

fig, axes = plt.subplots(1, 2)

dflux.hist('rate', bins=100, ax=axes[0])
dflux2.hist('rate', bins=100, ax=axes[1])

enter image description here

like image 103
piRSquared Avatar answered Jan 04 '23 10:01

piRSquared