向 Pandas 子图添加垂直线
我正在尝试将垂直线添加到pandas.dataframe.plot(子图),但挣扎着很大的时间。当前代码:
df_final = df_final / df_final.max(axis=0)
df_final.loc[30102040].plot(subplots=True, layout=(17, 1), figsize=(25, 25), sharex=True, sharey=True)
plt.legend(loc='center left')
plt.axvline(sepsis_onset, color='red', linestyle='dashed')
plt.show()
目前的外观:
当前均未正确显示传说和axvline。我想念什么?
I am trying to add a vertical line to a Pandas.dataframe.plot(subplot), but am struggling big time. Current Code:
df_final = df_final / df_final.max(axis=0)
df_final.loc[30102040].plot(subplots=True, layout=(17, 1), figsize=(25, 25), sharex=True, sharey=True)
plt.legend(loc='center left')
plt.axvline(sepsis_onset, color='red', linestyle='dashed')
plt.show()
what it currently looks like:
Neither the legend, nor the axvline is currently displayed correctly. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

无法验证 sepsis_onset 是否确实位于您的 y 轴上,但使用
subplots=True
时,问题就出现在您真正想要的四个图中的哪一个上。 plt.axvline 不会绘制所有轴对象,但直接在各个轴对象上调用该函数可以为您提供更多控制。方便的是,plot()函数返回所有轴对象的数组,我们需要这些对象在每个子图中绘制垂直线并控制其图例位置。示例:结果:

当然,您也可以将垂直线仅指向一个特定的子图:
结果:

It was not possible to verify that
sepsis_onset
is actually on your y-axis, but withsubplots=True
the question arises on which of the four plots you actually want the line.plt.axvline
will not plot to all of them, but calling the function directly on the individual axis objects gives you more control. Conveniently, theplot()
function returns an array of all axis objects which we need to plot your vertical line in each of the subplots and control its legend location. An example:result:

Of course you could also the vertical line only to one specific subplot:
result:
