我如何解决我的箱线图绘制?

2024-09-30 22:12:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试绘制箱线图,但遇到了“非类型属性错误”

拜托,我该怎么修?你知道吗

我的代码:

Failed = train_df['Fees'][train_df['Passedornot'] == 0]
Passed = train_df['Fees'][train_df['Passedornot'] ==1]


average_fees = DataFrame([mean(Passed), mean(Failed)])
std_fees = DataFrame([standard_deviation(Passed), standard_deviation(Failed)])
fig, axis4 = plt.subplots(1,1)
train_df['Fees'].plot(kind='hist', figsize=(15,5),bins=100, xlim=(0,30), ax=axis4)


fig, axis5 = plt.subplots(1,1)
average_fees.plot(kind='', legend=False, ax=axis5)

错误:

average_fees.plot(kind='bp', legend=False, ax=axis5)
AttributeError: 'NoneType' object has no attribute 'plot'

数据样本:

    Name        Sex     Age Score    Passedornot Fees
    Tim Cole    male    18   148          1       90
    Ellen James female  47   143          1       80
    Jerome Miles male   62   144          0       80
    Waltz Albert male   27   153          0       90

Tags: dataframedfplot错误trainaxmaleaverage
1条回答
网友
1楼 · 发布于 2024-09-30 22:12:34

不知道那里发生了什么,但我认为你的average_fees不是一个合适的数据帧。此外,我的编译器还在抱怨kind='bp'。这就是为什么社区总是要求提供一个功能齐全的工作示例的原因之一。你知道吗

这里是Python3的一个工作片段,运行在Jupyter笔记本中,用于保存到文件中的您提供的数据片段示例.txt地址:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
train_df = pd.read_csv('sample.txt', sep='\t')
# train_df.head()
Failed = train_df['Fees'][train_df['Passedornot'] == 0]
Passed = train_df['Fees'][train_df['Passedornot'] == 1]
average_fees = pd.DataFrame([np.mean(Passed), np.mean(Failed)])
std_fees = pd.DataFrame([np.std(Passed), np.std(Failed)])

fig1, axis4 = plt.subplots(1,1)
n, bins, patches = plt.hist(train_df.Fees, 100, linewidth=1)
plt.axis([0, 100, 0, 3])
plt.show()

fig2, axis5 = plt.subplots(1,1)
# print(type(average_fees))
average_fees.boxplot(return_type='axes')

相关问题 更多 >