简体   繁体   中英

How to plot multiple subplot dataframe histograms in Python?

I am trying to plot four dataframe column histograms in 4 subplots as follows:

fig2, ax2 = plt.subplots(nrows=2, ncols=2)
ax2[0, 0] = completeDF['Number_of_Weeks_Used'].plot.hist(bins=100, alpha=0.8)
ax2[0, 1] = completeDF['Season'].plot.hist(bins=100, alpha=0.8)

But it is merging both plots in 1 subplot as follows: 在此处输入图片说明

  • The axes are being specified in an incorrect manner
import pandas_datareader as web  # not part of pandas; conda or pip install
import pandas as pd
import matplotlib.pyplot

# get test data
df = web.DataReader('^gspc', data_source='yahoo', start='2020-09-01', end='2020-09-28').iloc[:, :4]

# set figure
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))

# plot to different axes
df.High.plot.hist(bins=100, alpha=0.8, ax=ax[0, 0])
df.Low.plot.hist(bins=100, alpha=0.8, ax=ax[0, 1])
df.Open.plot.hist(bins=100, alpha=0.8, ax=ax[1, 0])
df.Close.plot.hist(bins=100, alpha=0.8, ax=ax[1, 1])

plt.tight_layout()
plt.show()

在此处输入图片说明

  • The following, will also work
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))

df.High.plot.hist(bins=100, alpha=0.8, ax=ax1, label='High')
df.Low.plot.hist(bins=100, alpha=0.8, ax=ax2, label='Low')
df.Open.plot.hist(bins=100, alpha=0.8, ax=ax3, label='Open')
df.Close.plot.hist(bins=100, alpha=0.8, ax=ax4, label='Close')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM