python - 绘制子图时,直方图未显示在 pdf 中
问题描述
我正在尝试在 pdf 文件中为我的数据框中的每个变量绘制时间序列和直方图。每个动作单独工作,但在同一页面中对它们进行子图绘制时,直方图未显示。知道我做错了什么吗?这是我的代码:
with PdfPages('test.pdf') as pdf:
for i in range(df.shape[1]):
fig = plt.figure()
#time series
plt.subplot(2, 1, 1)
ax1 = df.iloc[:,i].plot(color='blue', grid=True, label='lab')
plt.title(df.columns.values[i])
#histograms
plt.subplot(2, 1, 2)
hist=df.hist(df.columns.values[i])
plt.plot()
pdf.savefig(fig)
plt.close()
解决方案
我不太确定我是否真的可以重现您的错误 - 但是,我会在您的代码中优化一些内容,也许您可以通过以下示例考虑一下:
with PdfPages('test.pdf') as pdf:
for c in df:
fig, axs = plt.subplots(2)
#time series
fig.suptitle(c)
df[c].plot(color='blue', grid=True, label='lab', ax=axs[0])
#histogram
hist=df.hist(c, ax=axs[1])
pdf.savefig(fig)
#plt.close()
主要提示:
- 无需迭代数据框列的值
- 用于
plt.subplots()
一个图中的多个绘图 - 我删除
plt.plot()
了 - 它没有做任何事情
推荐阅读
- javascript - 无法从选择框中选择项目/在输入字段中写入。我是否错误地处理了“密钥”?
- reactjs - 从 App.js 控制组件状态并更改它们的值
- asp.net - ASP.NET MVC 应用无法连接到 Azure SQL
- python - 如何使用 spaCy 对挪威语进行词形还原?
- r - 用“.”分割字符 在 R 中
- windows - 水晶报表--加载数据库信息失败--在客户端PC上
- swift - 将泛型类型约束为同一方法中另一个泛型类型的子类
- reactjs - 在 CRA 应用程序中,如何先等待某些操作(redux)完成,然后才继续使用 App.js 的 render() 函数?
- mysql - 在 MySQL 中获取具有多个数据集的过滤数据作为过滤器
- c++ - 使用自定义 QSortFilterProxyModel 覆盖哪些函数