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

如何正确使用errorbar=('ci', False)消除Seaborn绘图警告?

解决Seaborn ci参数弃用警告问题

问题背景

运行自定义seasonal_plot函数绘制季节性图表时,图像可正常生成,但收到Seaborn版本兼容警告:

The ci parameter is deprecated. Use errorbar=('ci', False) for the same effect.

该变更始于Seaborn v0.12.0,尝试添加errorbar=('ci', False)后报错无法生成图像,原自定义函数代码如下:

def seasonal_plot(X, y, period, freq, ax=None):
    if ax is None:
        _, ax = plt.subplots()
    palette = sns.color_palette("husl", n_colors=X[period].nunique(),)
    ax = sns.lineplot(
        x=freq,
        y=y,
        hue=period,
        data=X,
        ci=False,
        ax=ax,
        palette=palette,
        legend=False,
    )
    for line, name in zip(ax.lines, X[period].unique()):
        y_ = line.get_ydata()[-1]
        ax.annotate(
            name,
            xy=(1, y_),
            xytext=(6, 0),
            color=line.get_color(),
            xycoords=ax.get_yaxis_transform(),
            textcoords="offset points",
            size=14,
            va="center",
        )
    return ax

解决方案

修改sns.lineplot调用中的参数:移除已弃用的ci=False,替换为errorbar=('ci', False),确保不同时保留两个参数(否则会引发参数冲突报错)。

修改后的完整函数代码:

def seasonal_plot(X, y, period, freq, ax=None):
    if ax is None:
        _, ax = plt.subplots()
    palette = sns.color_palette("husl", n_colors=X[period].nunique(),)
    ax = sns.lineplot(
        x=freq,
        y=y,
        hue=period,
        data=X,
        errorbar=('ci', False),  # 替换原ci=False参数
        ax=ax,
        palette=palette,
        legend=False,
    )
    for line, name in zip(ax.lines, X[period].unique()):
        y_ = line.get_ydata()[-1]
        ax.annotate(
            name,
            xy=(1, y_),
            xytext=(6, 0),
            color=line.get_color(),
            xycoords=ax.get_yaxis_transform(),
            textcoords="offset points",
            size=14,
            va="center",
        )
    return ax

补充说明

  • 若仅需完全关闭误差线,也可使用errorbar=None,效果与原ci=False完全一致
  • 必须确保删除原ci参数,避免因参数重复导致报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:10:00