python绘制小提琴图,python小提琴图

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.

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)

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).

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.

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

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)

ad3141690191912165bd908337402f9a.png

You should file a bug report.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值