使用Matplotlib创建箱形图

发布时间:2022-09-25 / 作者:清心寡欲
本文介绍了使用matplotlib创建箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是python3和jupyter笔记本电脑。我有一个 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

我只是想为每个位置创建一个盒子图,以显示每个位置中物品的价格范围。

当我运行以下代码时:

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


plt.boxplot(postings)
plt.show()

返回TypeError:Unhasable type:‘Slice’

推荐答案

我猜您需要在同一图形中的每个位置使用BoxPlot。 我修改了给定的数据帧,以添加另一个位置的样本数据,如下所示-

   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

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

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

 

这篇关于使用Matplotlib创建箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!



[英文标题]Creating a Boxplot with Matplotlib


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。