Python 如何修复绘制箱线图?
我正在尝试绘制箱线图,但得到了一个“非类型属性错误” 求你了,我怎样才能解决这个问题 我的代码:Python 如何修复绘制箱线图?,python,dataframe,Python,Dataframe,我正在尝试绘制箱线图,但得到了一个“非类型属性错误” 求你了,我怎样才能解决这个问题 我的代码: 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_de
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
不确定那里发生了什么,但我认为你的
平均费用
不是一个合适的数据框架。此外,我的编译器还抱怨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')
不确定那里发生了什么,但我认为你的
平均费用
不是一个合适的数据框架。此外,我的编译器还抱怨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')
谢谢我本来打算创建函数的,但是这个效果要好得多。我能够得到一个箱线图,但这并没有将两个箱线图绘制为0和1,失败并通过了它们的标准偏差?有没有想过如何修改它来实现这一点?也许,您可以尝试类似于
train_df.boxplot(column='Fees',by='passedorno',return_type='axes')
?您可以使用by
选择选择数据所依据的标准。如果跳过列=…
,它将显示包含数字数据的所有行的多个绘图。谢谢!我本来打算创建函数的,但是这个效果要好得多。我能够得到一个箱线图,但这并没有将两个箱线图绘制为0和1,失败并通过了它们的标准偏差?有没有想过如何修改它来实现这一点?也许,您可以尝试类似于train_df.boxplot(column='Fees',by='passedorno',return_type='axes')
?您可以使用by
选择选择数据所依据的标准。如果跳过列=…
,它将显示包含数字数据的所有行的多个绘图。