pandas/matplotlib 中由另一列分组的一列箱线图
Box plot of one column grouped by another in pandas/matplotlib
假设我有一个这样的数据框:
species,weight
lion,130
lion,190
giraffe,803
lion,150
giraffe,1200
hippo,1300
giraffe,1000
hippo,1800
giraffe,1100
lion,160
每个物种的动物数量不同(例如,更少,抱歉 - 更少 - 河马)。我想制作一个箱形图,显示每个物种的重量分布。怎么做?
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 专家,但我在网上找到这段代码,我觉得它不错。我只想在数据部分输入您的值。
df.boxplot(by = 'species', vert = False)
假设我有一个这样的数据框:
species,weight
lion,130
lion,190
giraffe,803
lion,150
giraffe,1200
hippo,1300
giraffe,1000
hippo,1800
giraffe,1100
lion,160
每个物种的动物数量不同(例如,更少,抱歉 - 更少 - 河马)。我想制作一个箱形图,显示每个物种的重量分布。怎么做?
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 专家,但我在网上找到这段代码,我觉得它不错。我只想在数据部分输入您的值。
df.boxplot(by = 'species', vert = False)