Python Matplotlib箱形图 [英] Python Matplotlib Box plot

查看:214
本文介绍了Python Matplotlib箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据框:

  {'Parameter':{0:'A',1:'A', 2:'A',3:'A',4:'A',5:'A',6:'A',7:'A'},
'Site':{0:'S1 ',
1:'S2',
2:'S1',
3:'S2',
4:'S1',
5:'S2 ',
6:'S1',
7:'S2'},
'值':{0:2.3399999999999999,
1:2.6699999999999999,
2: 2.5600000000000001,
3:2.8900000000000001,
4:3.4500000000000002,
5:4.4500000000000002,
6:3.6699999999999999,
7:4.5599999999999996}}

我正在尝试按站点绘制参数的boxplot。最简单的方法是什么?另一个问题是,如果我将拥有多个参数,使用matplotlib按参数绘制boxplot的最简单方法是什么?谢谢

解决方案

您将要使用



如果要绘制数据的特定列,可以使用 boxplot的列关键字参数

 #绘制单个值
df.boxplot(column ='Value',by = ['Parameter','Site'])

#绘制多个值
df.boxplot(column = ['Value','OtherValue'],by = ['Parameter','网站'])


This is my dataframe:

{'Parameter': {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A', 5: 'A', 6: 'A', 7: 'A'},
 'Site': {0: 'S1',
  1: 'S2',
  2: 'S1',
  3: 'S2',
  4: 'S1',
  5: 'S2',
  6: 'S1',
  7: 'S2'},
 'Value': {0: 2.3399999999999999,
  1: 2.6699999999999999,
  2: 2.5600000000000001,
  3: 2.8900000000000001,
  4: 3.4500000000000002,
  5: 4.4500000000000002,
  6: 3.6699999999999999,
  7: 4.5599999999999996}}

I am trying to plot boxplot of parameter by site. What is an easiest way to do it? Additional question is if i will have more than 1 parameter, what will be the easiest way to plot boxplot using matplotlib by parameter? Thank you

解决方案

You're going to want to use the DataFrame.boxplot method and group by the "Parameter" and "Site" columns.

import matplotlib.pyplot as plt
from pandas import DataFrame

df = DataFrame({'Parameter': ['A',]*8, 
                'Site': ['S1', 'S2', 'S1', 'S2', 'S1', 'S2', 'S1', 'S2'],
                'Value':  [2.34, 2.67, 2.56, 2.89, 3.45, 4.45, 3.67, 4.56]})

df.boxplot(by=['Parameter', 'Site'])
plt.show()

If you want to plot a specific column of your data, you can use the column keyword argument to boxplot.

# Plot single value
df.boxplot(column='Value', by=['Parameter', 'Site'])

# Plot Multiple values
df.boxplot(column=['Value', 'OtherValue'], by=['Parameter', 'Site'])

这篇关于Python Matplotlib箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆