Changing the color of matplotlib's violin plots in a subplot

2.9k views Asked by At

I'm trying to change the color of the mean in a violin plot like is discribed here: Matplotlib differentiate between mean and median with colour or shape

but my violin plot is a subplot and this doesn't work. Is there a way for me to do this?

Here's my code:

fig, axes = plt.subplots(figsize = (16, 7), ncols = 2)
fig.suptitle('Distribution of Domain Lengths', size = 18)

axes[0].boxplot(human_pfam_domains['length'], showmeans = True)

axes[1].violinplot(human_pfam_domains['length'], showmeans = True, showmedians=True, vert=True, points= 500)

axes[1]['cmeans'].set_color('b')

and the error:

TypeError                                 Traceback (most recent call last)
<ipython-input-62-22cdd9b07029> in <module>()
  6 axes[1].violinplot(human_pfam_domains['length'], showmeans = True, showmedians=True, vert=True, points= 500)
  7 
----> 8 axes[1]['cmeans'].set_color('b')

TypeError: 'AxesSubplot' object has no attribute '__getitem_

Thanks!!

1

There are 1 answers

0
ImportanceOfBeingErnest On

As the linked solution suggests, you need to work on the violinplot dictionary, like so

r = axes[1].violinplot(...)
r['cmeans'].set_color('b')