如何正确使用errorbar=('ci', False)消除Seaborn绘图警告?
解决Seaborn
ci参数弃用警告问题 问题背景
运行自定义seasonal_plot函数绘制季节性图表时,图像可正常生成,但收到Seaborn版本兼容警告:
The
ciparameter is deprecated. Useerrorbar=('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
相关产品推荐
相关产品推荐

