利用sns.boxplot绘制箱型图的时候报错:AttributeError: ‘numpy.ndarray‘ object has no attribute ‘boxplot‘

(1)问题介绍

最近在做一个机器学习入门项目,鸢尾花种类的预测,在利用sns.boxplot()绘制箱型图的时候,程序报错:

AttributeError: 'numpy.ndarray' object has no attribute 'boxplot'

我的原代码是:

fig, axes = plt.subplots(2,2,figsize=(15,8))
sns.boxplot(x="species", y ="sepal_length", data = df, palette="Pastel1" , ax = axes[0])
sns.boxplot(x="species", y ="sepal_width", data = df, palette="Pastel1" , ax = axes[1])
sns.boxplot(x="species", y ="petal_length", data = df, palette="Pastel1" , ax = axes[2])
sns.boxplot(x="species", y ="petal_width", data = df, palette="Pastel1" , ax = axes[3])

我的目的是想绘制出每种鸢尾花的四个特征的箱型图,在一张画步上进行绘制,我是参考了别人的代码。

(2)错误原因

画布是2维的,但是这里面我天真的以为ax = axes[0],按照一维的索引即可实现绘制,所这里面的索引需要按照二维进行处理。改正后的代码:

fig, axes = plt.subplots(2,2,figsize=(15,8))
sns.boxplot(x="species", y ="sepal_length", data = df, palette="Pastel1" , ax = axes[0,0])
sns.boxplot(x="species", y ="sepal_width", data = df, palette="Pastel1" , ax = axes[0,1])
sns.boxplot(x="species", y ="petal_length", data = df, palette="Pastel1" , ax = axes[1,0])
sns.boxplot(x="species", y ="petal_width", data = df, palette="Pastel1" , ax = axes[1,1])

结果:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AttributeError: 'numpy.ndarray' object has no attribute 'new_box'是由于在使用sns.boxplot()函数时,numpy数组对象(ndarray)没有new_box属性导致的错误。 根据提供的代码段,可以看到sns.boxplot()函数被调用了四次,每次都使用了不同的y轴变量和axes对象。然而,numpy数组对象没有名为new_box的属性,所以导致了属性错误的异常。要解决这个问题,需要检查代码中是否有使用类似于new_box的属性或方法。如果是一个拼写错误,可以将其更正。如果是一个自定义的属性或方法,需要确保在使用之前正确地定义它。请检查代码以找到并修复这个错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [# Pycharm关于AttributeError: ‘DataFrame’ object has no attribute ‘score’的错误](https://download.csdn.net/download/weixin_38720756/13749766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [利用sns.boxplot绘制箱型图的时候报错AttributeError: ‘numpy.ndarrayobject has no attribute ...](https://blog.csdn.net/basktball_poet/article/details/128167699)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值