Skip to content
Advertisement

How to combine multiple data frames into a single violinplot and add a swarmplot

I have two data frames, with different shapes. I’d like to plot the two data frame values of the violin plots next to each other instead of overlapping.

JavaScript

Here is my output image.

Output image

But I’d like to get each blue and red violin plot next to each other instead of overlapping. I’d further like to show the datapoints via a swarm plot.

Advertisement

Answer

Seaborn works easiest with data in “long form”. You can create such a dataframe directly from the given dictionaries without the need to fill up with NaNs.

JavaScript

sns.violinplot from 2 dataframes with different sizes

PS: To combine the violin plot with a swarm plot, you also need hue= and dodge=True e.g. sns.swarmplot(data=df, x='label', y='value', hue='source', palette=['black', 'black'], dodge=True, ax=ax). You might also want to remove the existing inner of the violinplot.

JavaScript

combining violinplot and swarmplot with hue

Alternatively, you could create a split violinplot:

JavaScript

split sns.violinplot together with sns.swarmplot

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement