python - 如何在 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)
解决方案
- 以不正确的
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')
推荐阅读
- c# - 将 .NET Core 与 EF 结合使用时,使用示例数据为大型系统数据库播种的正确方法是什么?
- php - PHP ldap_connect 返回负数
- c# - 如何使用 ASP.NET Core 项目在 Visual Studio 2017 中创建指向图像的符号链接?
- reactjs - 是否可以在 vps 上开发 create-react-app?
- css - 在 Bootstrap 4 选项卡中对齐关闭图标
- java - Mopub Android 设置:“找不到 moat-mobile-app-kit”
- google-drive-api - 您可以为 Google Drive 构建数据库应用程序吗?
- scala - 如何通过 Spark 上的 Apache Livy 执行一个 jar 打包的 scala 程序,该程序直接响应客户端请求的结果?
- php - 如何在 PHP 中使用正则表达式匹配这种类似数组的表示法?
- android - Kotlin/Android - 只能检查一个 ToggleButton