Matplotlib: раскраска скрипичных сюжетов несколькими цветами в зависимости от модальности - PullRequest
0 голосов
/ 23 мая 2019

Я ищу, как создать сюжет с несколькими скриптами и мерами ответа из набора данных с двумя модальностями (время и обработка).Мне удалось построить этот скрипичный сюжет, но я не могу раскрасить каждый ответ на лечение определенным цветом.Я ищу простые вещи: я не хочу раскрашивать края и т. Д.

# -*- 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()

спасибо за помощь и прокомментировал ответы, чтобы понять ^^

1 Ответ

1 голос
/ 23 мая 2019

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)

Однако я бы посоветовал вам использовать вместо этого violingplot seaborn.Вам нужно было бы реорганизовать формат ваших данных, но это упростило бы построение графика.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...