我正在搜索如何使用多个小提琴图来绘制一个图,该图来自两种模式(时间和治疗)的数据集的响应度量。 我成功绘制了该小提琴图,但无法成功为每种治疗反应涂上特定的颜色。我搜索一个简单的东西:我不想给边缘上色,等等
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy
Lcolor = ['red','green','blue'] # targeted colors per treatment
## create a dataset with 2 columns of two modalities (remark: not the purpose of the question)
a = numpy.random.randn(60,1) # create the random variable "measure"
# create the modalities
Ltime = [1,3]
Ltreatment = [0.2,0.6,0.8]
modalities = [[0.2,1], [0.2,3], [0.6, 1], [0.6, 3], [0.8, 1], [0.8,3]]
tempList = []
for i in modalities:
tempList.extend([i]*10)
NpModLis = numpy.array(tempList)
# create a list of violinplot positions
position = [1.2,1.6, 1.8, 3.2, 3.6, 3.8]
# merging into a 2d-array of modalities and the random variable
DATA = numpy.c_[NpModLis,a]
## Dataset is made of first column: treatment, second column: time of mesure, third column: response measure
# Now I want to plot with violin plot for each time, the three violinplots due to treatment, each violinplot with a color specific to treatment
Lcolors = ['red','green','blue'] # respectively fro treatment 0.2, 0.6, 0.8
fig, ax = plt.subplots(figsize=(9, 4))
data=[]
for i in range(len(Ltime)):
j=Ltime[i]
for k in Ltreatment:
data.append(DATA[numpy.logical_and(DATA[:,0]==k,DATA[:,1]==j)][:,2])
ax.violinplot(data, position) # which isd violinplot(measure of one treatment at one time, position)
plt.show()
感谢您的帮助和评论来理解^^
答案 0 :(得分:1)
violinplot()
返回包含创建的艺术家的词典,因此您可以修改其属性。这似乎可以为您提供理想的输出
out = ax.violinplot(data, position) # which isd violinplot(measure of one treatment at one time, position)
for b,c in zip(out['bodies'],itertools.islice(itertools.cycle(Lcolors), 0, len(out['bodies']))):
b.set_facecolor(c)
但是,我建议您改用seaborn's violingplot。您必须重构数据的格式,但这会简化绘图部分。