简体   繁体   English

python小提琴图

[英]Violin Plot with python

I want to create 10 violin plots but within one diagram. 我想在一张图中创建10个小提琴图。 I looked at many examples like this one: Violin plot matplotlib , what shows what I would like to have at the end. 我看了很多这样的例子: 小提琴情节matplotlib ,显示了我最后想要的东西。

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: 我的数据格式为D [10,730],如果我尝试通过上面的链接使用以下示例进行改编:

axes[0].violinplot(all_data,showmeans=False,showmedians=True)

my code: 我的代码:

axes[0].violinplot(D,showmeans=False,showmedians=True)

it do not work. 它不起作用。 It should print 10 violin plot in parallel (first dimension of D ). 它应该并行打印10个小提琴图( D第一维)。

So how do my data need to look like to get the same type of violin plot? 那么,如何获得相同类型的小提琴图数据呢?

You just need to transpose your data array D . 您只需要转置数据数组D

axes[0].violinplot(D.T,showmeans=False,showmedians=True)

This appears to be a small bug in matplotlib. 这似乎是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)

在此处输入图片说明

You should file a bug report. 您应该提交错误报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM