我想在一张图中创建10个小提琴图.我看了很多像这样的例子:小提琴图matplotlib ,这说明了我的想法喜欢在结尾处.
I want to create 10 violin plots but within one diagram. I looked at many examples like this one: Violin plot matplotlib, what shows what I would like to have at the end.
但是我不知道如何使它适应真实的数据集.它们都只生成一些正态分布的随机数据. 我的数据格式为D [10,730],如果我尝试通过上面的链接使用以下方法进行改编: 例如:
But I did not know how to adapt it to a real data set. They all just generate some random data which is normal distributed. I have data in form D[10,730] and if I try to adapt it from the link above with : example:
axes[0].violinplot(all_data,showmeans=False,showmedians=True)我的代码:
axes[0].violinplot(D,showmeans=False,showmedians=True)它不起作用. 它应该并行打印10个小提琴图(D的第一维).
it do not work. It should print 10 violin plot in parallel (first dimension of D).
那么我的数据看起来如何才能获得相同类型的小提琴图?
So how do my data need to look like to get the same type of violin plot?
推荐答案您只需要转置数据数组D.
You just need to transpose your data array D.
axes[0].violinplot(D.T,showmeans=False,showmedians=True)这似乎是matplotlib中的一个小错误.对于一维数组和2D数组,以不一致的方式处理轴.
This appears to be a small bug in matplotlib. The axes are treated in a non-consistent manner for a list of 1D arrays and a 2D array.
import numpy as np import matplotlib.pyplot as plt n_datasets = 10 n_samples = 730 data = np.random.randn(n_datasets,n_samples) fig, axes = plt.subplots(1,3) # matplotlib/examples/statistics/boxplot_vs_violin_demo.html axes[0].violinplot([d for d in data]) # should be equivalent to: axes[1].violinplot(data) # is actually equivalent to axes[2].violinplot(data.T)
您应该提交错误报告.
更多推荐
python小提琴图
发布评论