在pandas / matplotlib中由另一列分组的一列的框图

时间:2017-03-17 16:15:15

标签: python pandas matplotlib boxplot

假设我有一个这样的数据框:

species,weight
lion,130
lion,190
giraffe,803
lion,150
giraffe,1200
hippo,1300
giraffe,1000
hippo,1800
giraffe,1100
lion,160

每个物种有不同数量的动物(较少,抱歉 - 较少 - 例如河马)。我想制作一个箱形图,显示每个物种的重量分布。怎么样?

2 个答案:

答案 0 :(得分:-1)

import matplotlib.pyplot as plt
import numpy as np

# fake up some data
spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low), 0)

# basic plot
plt.boxplot(data)

# notched plot
plt.figure()
plt.boxplot(data, 1)


# horizontal boxes
plt.figure()
plt.boxplot(data, 0, 'rs', 0)

# change whisker length
plt.figure()
plt.boxplot(data, 0, 'rs', 0, 0.75)

# fake up some more data
spread = np.random.rand(50) * 100
center = np.ones(25) * 40
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
d2 = np.concatenate((spread, center, flier_high, flier_low), 0)
data.shape = (-1, 1)
d2.shape = (-1, 1)

plt.show()

不是Matplotlib的专家,但我在网上找到了这个代码,对我来说看起来不错。我只想在数据部分中输入您的值。

答案 1 :(得分:-2)

df.boxplot(by = 'species', vert = False)