2017-05-22 32 views
1

我正在使用python 3和jupyter笔记本。我有一个结构类似这样的熊猫数据帧:使用Matplotlib创建Boxplot

  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'> 

我只是想创建一个箱线图的每个位置,显示在每个位置项之间的价格范围内。

当我运行下面的代码:

import matplotlib.pyplot as plt 
import numpy as np 
%matplotlib inline 


plt.boxplot(postings) 
plt.show() 

它返回类型错误:unhashable类型:“片”

+0

你确定'postings'是一个数据框吗?尝试传递列的值而不是整个数据帧。 –

+0

这是一个df。当我尝试传递plt.boxplot(postings.location)时,它会输出IndexError:0 –

回答

2

我想你需要箱图在同一图中的每个位置。 我修改给数据帧添加样本数据看起来喜欢 -

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 

现在,只需拨打这个箱线图框上,并提供参数 - columnby

postings.boxplot(column='price', by='location') 

enter image description here 另一个位置

0

从数据巨鼎,你想有一个箱线图从5单箱价格价值你有。您需要传递您想要制作boxplot的实际数据。

plt.boxplot(postings["price"]) 

查看示例here

0

我想“价格”是你想要有盒子图的数据列。因此,您需要先选择此列并仅向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() 

enter image description here