我已经使用R很长时间了,我最近在学习Python。 我想在Python的一个面板中创建多个框图。 我的数据集是矢量形式,标签矢量表示每个数据元素对应的框图。示例如下所示:
N = 50
data = np.random.lognormal(size=N, mean=1.5, sigma=1.75)
label = np.repeat([1,2,3,4,5],N/5)
从各种网站(例如matplotlib: Group boxplots),创建多个箱图需要一个矩阵对象输入,其列包含一个箱图的样本。所以我根据数据和标签创建了一个列表对象:
savelist = data[ label == 1]
for i in [2,3,4,5]:
savelist = [savelist, data[ label == i]]
但是,下面的代码给出了一个错误:
boxplot(savelist)
Traceback (most recent call last):
File "<ipython-input-222-1a55d04981c4>", line 1, in <module>
boxplot(savelist)
File "/Users/yumik091186/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2636, in boxplot
meanprops=meanprops, manage_xticks=manage_xticks)
File "/Users/yumik091186/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 3045, in boxplot labels=labels)
File "/Users/yumik091186/anaconda/lib/python2.7/site-packages/matplotlib/cbook.py", line 1962, in boxplot_stats
stats['mean'] = np.mean(x)
File "/Users/yumik091186/anaconda/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2727, in mean
out=out, keepdims=keepdims)
File "/Users/yumik091186/anaconda/lib/python2.7/site-packages/numpy/core/_methods.py", line 66, in _mean
ret = umr_sum(arr, axis, dtype, out, keepdims)
ValueError: operands could not be broadcast together with shapes (2,) (10,)
有人可以解释发生了什么吗?
答案 0 :(得分:1)
您最终会使用嵌套列表而不是平面列表。试试这个:
savelist = [data[label == 1]]
for i in [2,3,4,5]:
savelist.append(data[label == i])
它应该有用。