如何在Python中绘制多个子图数据帧直方图?

2024-06-28 11:37:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在4个子图中绘制四个数据帧列直方图,如下所示:

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)

但它将两个图合并到一个子图中,如下所示: enter image description here


Tags: 数据alphaplot绘制plt直方图histbins
1条回答
网友
1楼 · 发布于 2024-06-28 11:37:56
  • 正在以不正确的方式指定axes
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()

enter image description here

  • 以下,也将起作用
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')

相关问题 更多 >