python-3.x - 将 Pandas 绘制成子图
问题描述
da 是我的数据框。我想把这个数字变成我将拥有的 2 个子图中的一个子图。当我为这个图添加 plt.subplots(2,1,2) 时,它最终将该图分成一个单独的图,并且子图是一个空图。
我怎样才能把这段代码变成一个子图?
ax1 = da.plot(rot = 90, title ='Pre-Folsom Dam Spring Recession')
ax1.set_xlabel('Water Year Day')
ax1.axhline( y = float(fSP_Mag) , xmin=0, xmax=35,color ='r', linestyle='--',zorder=0,label= 'Magnitude')
ax1.axvline(x=float(fSP_Tim), color ='r',linestyle='--', label='Timing')
ax1.legend(framealpha=1, frameon=True)
解决方案
import pandas as pd
import matplotlib.pyplot as plt
data=pd.DataFrame({"col1":[1,2,3,4,5],"col2":[2,4,6,8,10]})
fig=plt.figure()
ax1=fig.add_subplot(2,1,1)
ax2=fig.add_subplot(2,1,2)
data["col1"].plot(ax=ax1)
data["col2"].plot(ax=ax2)
创建一个 plt.figure() 并将子图分配给 ax1 和 ax2。现在使用这些轴绘制数据框。
参考:-
推荐阅读
- javascript - 从 10 个下拉列表中仅选择 5 个并在 Javascript JQuery 中阻止剩余的一个
- python - 用于获取已安装软件列表以及安装日期的 Python 脚本
- django - Django Concat 不能在 AddConstraint 中使用
- list - Prolog 中的 ListsfromList 函数
- python - 重命名具有向后兼容性的python包(用于pickle)
- swiftui - 不使用 WindowGroup 时工具栏不出现
- amazon-web-services - AWS Cognito 首先等待用户验证电子邮件,然后再使用 updateUserAttributes() 进行更新
- azure - Azure ADF 挂在 for 循环中什么都不做
- context-free-grammar - FOLLOW 已消除左因式的语法集
- python - Django/Python 从 UUID 生成唯一的礼品卡代码