如何修复绘制箱线图?

时间:2016-09-14 06:16:51

标签: python dataframe

我正在尝试绘制我的boxplot但是我收到了'nonetype属性错误'。

请问,我该如何解决这个问题?

我的代码:

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

1 个答案:

答案 0 :(得分:0)

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

这是Python3的一个工作片段,在Jupyter笔记本中运行,用于保存到文件sample.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')