Python 使用 Matplotlib 创建箱线图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44119653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:44:50  来源:igfitidea点击:

Creating a Boxplot with Matplotlib

pythonpandasmatplotlibboxplot

提问by Keenan Burke-Pitts

I am using python 3 and jupyter notebook. I have a pandas dataframe that is structured like this:

我正在使用 python 3 和 jupyter notebook。我有一个结构如下的熊猫数据框:

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

I am simply trying to create a boxplot for each location to show the range of prices among items in each location.

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

When I ran the following code:

当我运行以下代码时:

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


plt.boxplot(postings)
plt.show()

It returned TypeError: unhashable type: 'slice'

它返回 TypeError: unhashable type: 'slice'

回答by Amey Dahale

I guess you require boxplot for each location in same graph. I modified given dataframe to add sample data for another location which looks like-

我猜您需要为同一图中的每个位置绘制箱线图。我修改了给定的数据框以添加另一个位置的示例数据,看起来像 -

   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

Now, just call boxplot on this frame and provide parameters- columnand by

现在,只需在此框架上调用 boxplot 并提供参数 -columnby

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

? enter image description here?

? 在此处输入图片说明?

回答by ImportanceOfBeingErnest

I guess "price" is the column of data that you want to have boxplotted. So you need to first select this column and supply only that column to plt.boxplot.

我猜“价格”是您想要绘制的数据列。因此,您需要首先选择此列并仅将该列提供给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

在此处输入图片说明

回答by fbence

Juding from the data, you want to have a boxplot with a single box from the 5 price values you have. You need to pass the actual data you want to make the boxplot from.

从数据来看,您希望有一个箱线图,其中包含您拥有的 5 个价格值中的一个箱体。您需要传递要从中制作箱线图的实际数据。

plt.boxplot(postings["price"])

Check out the examples here.

查看此处的示例。