You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Python Pandas DataFrame时序图添加超出阈值的绿色信号标记点

实现方法

你可以直接在原有绘图逻辑基础上叠加散点图层,修改后的代码如下:

import matplotlib.pyplot as plt

def time_series_graph_horizontal_threshold(df, x_var, y_var):
    plt.figure(figsize=(10, 6))
    plt.grid(True)
    # 获取当前绘图的坐标轴对象,用于叠加标记点
    ax = df.plot(x='Year_Week', y=['Reported_Cases', 'Horizontal_Threshold'])
    # 筛选出触发正信号的行数据
    positive_points = df[df['Positive_Signal'] == 1]
    # 绘制绿色标记点,zorder参数保证点显示在折线上方不被遮挡
    ax.scatter(
        x=positive_points['Year_Week'],
        y=positive_points['Reported_Cases'],
        color='green',
        s=40, # 可自行调整点的大小
        zorder=5,
        label='正信号'
    )
    # 显示图例
    ax.legend()
    plt.show()

如果你不想依赖提前生成的Positive_Signal列,也可以直接通过条件筛选生成正信号子集,替换上述筛选代码即可:

positive_points = df[df['Reported_Cases'] > df['Horizontal_Threshold']]

内容的提问来源于stack exchange,提问作者KubiK888

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 04:45:04