Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set color for each violin in violin plot

I tried violin plot in Seaborn with different colors for each violin as:

sns.violinplot(x='type', y='den', data=mydf, ax=axes[0], color=['red','blue','yellow'])

however I got:

ValueError: to_rgb: Invalid rgb arg "('red', 'blue', 'yellow')" could not convert string to float: red

like image 583
Benjamin Nguyen Avatar asked Jan 07 '23 02:01

Benjamin Nguyen


1 Answers

I think you should use the palette keyword:

color : matplotlib color, optional

Color for all of the elements, or seed for light_palette() when using hue nesting.

palette : seaborn color palette or dict, optional

Colors to use for the different levels of the hue variable. Should be something that can be interpreted by color_palette(), or a dictionary mapping hue levels to matplotlib colors.

import seaborn as sb
import numpy as np

data = np.random.random([100,4])

sb.violinplot(data=data, palette=['r','g','b','m'])

enter image description here

like image 191
Bart Avatar answered Feb 04 '23 02:02

Bart