Python 使用Matplotlib创建箱线图
我正在使用Python3和jupyter笔记本。我有一个熊猫数据框,其结构如下:Python 使用Matplotlib创建箱线图,python,pandas,matplotlib,boxplot,Python,Pandas,Matplotlib,Boxplot,我正在使用Python3和jupyter笔记本。我有一个熊猫数据框,其结构如下: location price Apr 25 ASHEVILLE 15.0 Apr 25 ASHEVILLE 45.0 Apr 25 ASHEVILLE 50.0 Apr 25 ASHEVILLE 120.0 Apr 25 ASHEVILLE 300.0 <class 'pandas.core.frame.DataFrame'> 它返回了Type
location price
Apr 25 ASHEVILLE 15.0
Apr 25 ASHEVILLE 45.0
Apr 25 ASHEVILLE 50.0
Apr 25 ASHEVILLE 120.0
Apr 25 ASHEVILLE 300.0
<class 'pandas.core.frame.DataFrame'>
它返回了TypeError:unhable type:“slice”根据数据,您希望有一个包含5个价格值中单个框的箱线图。您需要传递要从中生成箱线图的实际数据
plt.boxplot(postings["price"])
请查看示例。我猜“价格”是您想要绘制的数据列。因此,您需要首先选择此列并仅将该列提供给plt.boxplot
u = u"""index,location,price
Apr 25,ASHEVILLE,15.0
Apr 25,ASHEVILLE,45.0
Apr 25,ASHEVILLE,50.0
Apr 25,ASHEVILLE,120.0
Apr 25,ASHEVILLE,300.0"""
import io
import pandas as pd
import matplotlib.pyplot as plt
data = io.StringIO(u)
df = pd.read_csv(data, sep=",", index_col=0)
plt.boxplot(df["price"])
plt.show()
我想您需要同一图表中每个位置的箱线图。 我修改了给定的dataframe,为另一个位置添加了示例数据,如下所示-
date location month price
0 25 ASHEVILLE Apr 15.0
1 25 ASHEVILLE Apr 45.0
2 25 ASHEVILLE Apr 50.0
3 25 ASHEVILLE Apr 120.0
4 25 ASHEVILLE Apr 300.0
5 25 NASHVILLE Apr 34.0
6 25 NASHVILLE Apr 55.0
7 25 NASHVILLE Apr 70.0
8 25 NASHVILLE Apr 105.0
9 25 NASHVILLE Apr 85.0
现在,只需在此框架上调用boxplot并提供参数-column
和by
postings.boxplot(column='price', by='location')

 是否确定
过帐
是数据帧?尝试只传递列的值,而不是整个数据帧。它是一个df。当我尝试传递plt.boxplot(postings.location)时,它会输出indexer:0
postings.boxplot(column='price', by='location')