使用sns.boxplot报错:Axes.boxplot()不支持fill关键字参数
问题解决:seaborn boxplot 报错 TypeError: Axes.boxplot() got an unexpected keyword argument 'fill'

当我按照Google Colab文档的相同结构编写代码时,程序返回错误:
执行代码:
sns.boxplot(data=df_clean, x="funnel", y="ctr", hue="funnel", fill=False, gap=.1)
报错信息:
TypeError Traceback (most recent call last) <ipython-input-63-2814d552d352> in <cell line: 1>() ----> 1 sns.boxplot(data=df_clean, x="funnel", y="ctr", hue="funnel", fill=False, gap=.1) 3 frames /usr/local/lib/python3.10/dist-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs) 1440 def inner(ax, *args, data=None, **kwargs): 1441 if data is None: -> 1442 return func(ax, *map(sanitize_sequence, args), **kwargs) 1443 1444 bound = new_sig.bind(ax, *args, **kwargs) TypeError: Axes.boxplot() got an unexpected keyword argument 'fill'
原因及解决方案
这个错误的核心是当前使用的seaborn版本过低,fill和gap是seaborn v0.13.0及以上版本才新增的参数,旧版本不支持。
方案1:升级seaborn到最新版本
在Colab中执行以下命令升级:
!pip install --upgrade seaborn
升级完成后重启内核,再运行绘图代码即可正常使用这两个参数。
方案2:适配旧版本seaborn
如果无法升级,需要修改参数兼容旧版本:
- 去掉
gap=.1参数,若需要调整箱线间距,可以通过matplotlib的subplots_adjust方法手动调整 - 将
fill=False替换为color="white"(实现无填充效果),或者直接删除该参数
内容的提问来源于stack exchange,提问作者Marcia Galeno
相关产品推荐
相关产品推荐

