簡體   English   中英

如何在 Python 中繪制多個子圖數據框直方圖?

[英]How to plot multiple subplot dataframe histograms in 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)

但它正在合並 1 個子圖中的兩個圖,如下所示: 在此處輸入圖片說明

  • 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()

在此處輸入圖片說明

  • 以下,也將工作
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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM