向 Pandas 子图添加垂直线

发布于 01-20 21:40 字数 447 浏览 1 评论 0原文

我正在尝试将垂直线添加到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:
Current Output

Neither the legend, nor the axvline is currently displayed correctly. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心空情空意2025-01-27 21:40:32

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

import pandas as pd
import numpy as np

#Create some dummy dataframe
df = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df = df.cumsum()

axes = df.plot(subplots=True, figsize=(6, 6));

#Loop over all axis objects and create a vertical line in each of them and set legend location
for ax in axes:
    ax.axvline(400, color='red', linestyle='dashed')
    ax.legend(loc='center left')

结果:
输入图片此处描述

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

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df = df.cumsum()

axes = df.plot(subplots=True, figsize=(6, 6));
for ax in axes:
    ax.legend(loc='center left')
axes[1].axvline(400, color='red', linestyle='dashed')

结果:
输入图片此处描述

It was not possible to verify that sepsis_onset is actually on your y-axis, but with subplots=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, the plot() 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:

import pandas as pd
import numpy as np

#Create some dummy dataframe
df = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df = df.cumsum()

axes = df.plot(subplots=True, figsize=(6, 6));

#Loop over all axis objects and create a vertical line in each of them and set legend location
for ax in axes:
    ax.axvline(400, color='red', linestyle='dashed')
    ax.legend(loc='center left')

result:
enter image description here

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

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df = df.cumsum()

axes = df.plot(subplots=True, figsize=(6, 6));
for ax in axes:
    ax.legend(loc='center left')
axes[1].axvline(400, color='red', linestyle='dashed')

result:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文