How to plot 2 histograms side by side?

I have 2 data frames. I want to plot a histogram based on the "rate" column for each, side by side. How to do it?

I've 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 didn't have the desired effect. He showed two blank charts, then one filled chart.

+3


source to share


1 answer


Use to define a shape with two axes. Then specify the axis to draw within using the parameter . subplots

hist

ax

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

+4


source







All Articles