比方说,我有这样一个数据帧:箱线由另一个大熊猫分组/ matplotlib
species,weight
lion,130
lion,190
giraffe,803
lion,150
giraffe,1200
hippo,1300
giraffe,1000
hippo,1800
giraffe,1100
lion,160
有每个物种的动物(少了,不好意思的不同号码 - 少 - 河马,为例)。我想制作一个箱形图,显示每种物种的体重分布。如何?
比方说,我有这样一个数据帧:箱线由另一个大熊猫分组/ matplotlib
species,weight
lion,130
lion,190
giraffe,803
lion,150
giraffe,1200
hippo,1300
giraffe,1000
hippo,1800
giraffe,1100
lion,160
有每个物种的动物(少了,不好意思的不同号码 - 少 - 河马,为例)。我想制作一个箱形图,显示每种物种的体重分布。如何?
df.boxplot(by = 'species', vert = False)
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的专家,但我在网上发现了这个代码,它对我来说很好看。我只是把你的价值观放在数据部分。
如果您发现在线提供的代码,请在评论中链接到它。这不是对这个问题的回答。 – ImportanceOfBeingErnest
虽然此代码片段可能会解决问题,但[包括解释](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – DimaSan
它提供了答案。我会知道,我是这个问题的作者。 – Nucular