python小提琴图 [英] Violin Plot with python
问题描述
我想在一张图中创建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)
# http://matplotlib.org/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小提琴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!