python - 绘制熊猫数据帧中的 Numpy 数组命令
问题描述
我正在使用 pandas 数据框和 numpy 打印子图。此代码每页打印 4 个图,然后将图保存为单独的 pdf。
我可以调用 numpy 数组以便以更有效的方式绘制 dataframe2 吗?也许使用 enumerate 命令?
我附上了我正在输出的图表的图片。
我的代码完成了工作,但是,我觉得总的来说效率很低。为了我自己的学习,我想简化代码。
no_of_graphs = 2*2
for a in range(2):
start = 1 + counter * no_of_graphs
end = 4 + counter * no_of_graphs
ax = df1[df1.columns[start:end]].plot(figsize=(12,15), grid = True, legend = True, subplots=True, layout = (2,2), sharex = False, color = 'blue')
df2[df2.columns[1 + counter * no_of_graphs]].plot(ax=ax[0][0], grid = True, legend = True, linestyle = 'dashdot', color = 'red')
df2[df2.columns[2 + counter * no_of_graphs]].plot(ax=ax[0][1], grid = True, legend = True, linestyle = 'dashdot', color = 'red')
df2[df2.columns[3 + counter * no_of_graphs]].plot(ax=ax[1][0], grid = True, legend = True, linestyle = 'dashdot', color = 'red')
df2[df2.columns[4 + counter * no_of_graphs]].plot(ax=ax[1][1], grid = True, legend = True, linestyle = 'dashdot', color = 'red')
ax = np.squeeze(ax)
fname = 'file' + str(a) + '.PDF'
for y in range(0,2):
for x in range(0,2):
ax[x,y].set_xticks(df1.index[::60])
ax[x,y].set_xticklabels(df1.Time[::60], rotation=270)
plt.tight_layout()
plt.gcf()
plt.savefig(fname, dpi = 1000)
counter +=1
print(counter)
解决方案
推荐阅读
- maven - Maven Surefire:启动分叉时发生错误
- r - 为各种 stat_ellipse 添加图例
- tableau-api - 在 Tableau 中连接到 SQL Analysis Services 时在图表后面显示完整数据集
- amazon-web-services - 如何授予对特定存储桶的访问权限?
- python - 如何修复错误异常以允许重试而不在 Python 中循环异常
- c# - 从以 varbinary 格式保存的 SQL 读取数据并在系统发送的电子邮件中附加文件?
- javascript - 如何使用反应测试库测试自定义 Hook
- excel - 保存自动过滤器设置并重新应用
- javascript - 如何在提交后立即显示评论而不在 Vue 中渲染页面
- javascript - Div 在点击时显示,但仅显示一秒钟