Python Matplotlib 箱线图
Python Matplotlib Box plot
这是我的数据框:
{'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}}
我正在尝试按站点绘制参数的箱线图。最简单的方法是什么?另一个问题是,如果我有超过 1 个参数,那么使用 matplotlib 按参数绘制箱线图的最简单方法是什么?谢谢
您将要使用 DataFrame.boxplot
方法并按 "Parameter" 和 "Site" 列分组。
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()
如果要绘制数据的特定列,可以将 column
关键字参数用于 boxplot
。
# Plot single value
df.boxplot(column='Value', by=['Parameter', 'Site'])
# Plot Multiple values
df.boxplot(column=['Value', 'OtherValue'], by=['Parameter', 'Site'])
这是我的数据框:
{'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}}
我正在尝试按站点绘制参数的箱线图。最简单的方法是什么?另一个问题是,如果我有超过 1 个参数,那么使用 matplotlib 按参数绘制箱线图的最简单方法是什么?谢谢
您将要使用 DataFrame.boxplot
方法并按 "Parameter" 和 "Site" 列分组。
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()
如果要绘制数据的特定列,可以将 column
关键字参数用于 boxplot
。
# Plot single value
df.boxplot(column='Value', by=['Parameter', 'Site'])
# Plot Multiple values
df.boxplot(column=['Value', 'OtherValue'], by=['Parameter', 'Site'])