使用 Matplotlib 创建箱线图
Creating a Boxplot with Matplotlib
我正在使用 python 3 和 jupyter notebook。我有一个结构如下的 pandas 数据框:
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'>
我只是想为每个位置创建一个箱线图,以显示每个位置的商品之间的 运行ge 价格。
当我运行以下代码时:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
plt.boxplot(postings)
plt.show()
它返回类型错误:无法散列的类型:'slice'
从数据来看,您希望从您拥有的 5 个价格值中得到一个包含单个框的箱线图。您需要传递要从中制作箱线图的实际数据。
plt.boxplot(postings["price"])
查看示例 here。
我想 "price" 是您想要绘制箱线图的数据列。因此,您需要首先 select 这一列,然后仅将该列提供给 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()
我想您需要为同一图表中的每个位置绘制箱线图。
我修改了给定的数据框以添加另一个位置的样本数据,看起来像-
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
现在,只需在此帧上调用箱线图并提供参数 - column
和 by
postings.boxplot(column='price', by='location')
✿

我正在使用 python 3 和 jupyter notebook。我有一个结构如下的 pandas 数据框:
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'>
我只是想为每个位置创建一个箱线图,以显示每个位置的商品之间的 运行ge 价格。
当我运行以下代码时:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
plt.boxplot(postings)
plt.show()
它返回类型错误:无法散列的类型:'slice'
从数据来看,您希望从您拥有的 5 个价格值中得到一个包含单个框的箱线图。您需要传递要从中制作箱线图的实际数据。
plt.boxplot(postings["price"])
查看示例 here。
我想 "price" 是您想要绘制箱线图的数据列。因此,您需要首先 select 这一列,然后仅将该列提供给 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()
我想您需要为同一图表中的每个位置绘制箱线图。 我修改了给定的数据框以添加另一个位置的样本数据,看起来像-
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
现在,只需在此帧上调用箱线图并提供参数 - column
和 by
postings.boxplot(column='price', by='location')
✿