con
con

Reputation: 6093

How to get different violins with different colors with matplotlib?

I have a working script that generates a violin plot:

import matplotlib.pyplot as plt
data = []
data.append([0.952560240963855,0.957831325301205,0.963855421686747,0.955572289156626])
data.append([0.951807228915663,0.955572289156626,0.955572289156626,0.95933734939759])
data.append([0.559440559440559,0.540983606557377,0.671232876712329,0.542635658914729])
data.append([0.561643835616438,0.520325203252033,0.604026845637584,0.584615384615385])
data.append([0.51219512195122,0.535211267605634,0.328767123287671,0.545454545454545])
data.append([0.5,0.549295774647887,0.383561643835616,0.506493506493506])
data.append([0.0168539325842697,0.0143198090692124,0.0191235059760956,0.0135891286970424])
data.append([0.0184590690208668,0.0159108989657916,0.0247011952191235,0.0119904076738609])
data.append([0.541487450045305,0.527328339941019,0.652109370736233,0.531303388453911])
data.append([0.541172390210179,0.504257856866246,0.580634953286575,0.574899313794581])
data.append([0.655737704918033,0.647058823529412,0.671232876712329,0.673076923076923])
data.append([0.640625,0.615384615384615,0.592105263157895,0.716981132075472])
data.append([0.48780487804878,0.464788732394366,0.671232876712329,0.454545454545455])
data.append([0.5,0.450704225352113,0.616438356164384,0.493506493506494])
data.append([0.98314606741573,0.985680190930788,0.980876494023904,0.986410871302958])
data.append([0.981540930979133,0.984089101034208,0.975298804780876,0.988009592326139])
data.append([0.96837196100693,0.973438883099712,0.978802597827867,0.975474166121648])
data.append([0.966737462318444,0.972587313859289,0.97325765431425,0.972250770811922])
f = plt.figure()
f.set_figwidth(9)
plt.rc('xtick', labelsize = 6)
violin_plot = plt.violinplot(data,
                  showmeans=False, vert = False,
                  showmedians=True)
for pc in violin_plot["bodies"]:
    pc.set_facecolor('red')
    pc.set_edgecolor('black')
plt.xlabel('Metric Value')
plt.ylabel('Metric')
plt.yticks([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], ["Accuracy","Accuracy","F1","F1","False Negative Rate","False Negative Rate","False Positive Rate","False Positive Rate","MCC","MCC","Precision","Precision","Recall","Recall","Specificity","Specificity","roc_auc","roc_auc"])
plt.savefig('violin.svg', bbox_inches='tight', pad_inches = 0.05)

enter image description here

as can be seen, the y-labels repeat in pairs. I would like the first violin in one color, and the second in another color to make the plot easier to understand.

For example, the colors should be red, green, red, green, red, green, etc. to show that the same pair is repeating.

as the above code shows, I can change the color of all violin plots, but not just for one of each (cf. Changing the color of matplotlib's violin plots)

How can I get matplotlib to set the color of each violin so it looks something like this?color set individually

Upvotes: 1

Views: 1431

Answers (1)

Zephyr
Zephyr

Reputation: 12496

You could edit your for loop in this way:

for i, pc in enumerate(violin_plot["bodies"], 1):
    if i%2 != 0:
        pc.set_facecolor('red')
    else:
        pc.set_facecolor('green')
    pc.set_edgecolor('black')

In this way you are counting polycollections (starting from the bottom); the even ones are drawn in green, the odd ones in red.

enter image description here

Upvotes: 1

Related Questions