Violin Plot with Jitter#

This example combines a violin plot with a jittered scatter overlay to show the distribution and individual values of flipper length across penguin species. The transparent violins provide a summary shape, while the scatter points reveal the underlying data.

import hvplot.pandas  # noqa

df = hvplot.sampledata.penguins('pandas')

df.hvplot.violin(
    y='flipper_length_mm',
    by='species',
    title='Violin Plot by Species (Bokeh)',
    ylabel='Flipper Length (mm)',
    violin_fill_alpha=0
) * df.hvplot.scatter(
    x='species',
    y='flipper_length_mm',
    color="black",
    alpha=0.7,
    size=8,
).opts(jitter=0.25)
import hvplot.pandas  # noqa
import numpy as np

hvplot.extension('matplotlib')

df = hvplot.sampledata.penguins('pandas')
df = df[df['species'].notna()].copy()
df = df.sort_values('species')
species_map = {'Adelie': 0, 'Chinstrap': 1, 'Gentoo': 2}
df['x_jitter'] = df['species'].map(species_map) + np.random.uniform(-0.2, 0.2, size=len(df))

df.hvplot.violin(
    y='flipper_length_mm',
    by='species',
    title='Violin Plot by Species (Matplotlib)',
    ylabel='Flipper Length (mm)',
    edgecolors='black',
    facecolors='white',
) * df.hvplot.scatter(
    x='x_jitter',
    y='flipper_length_mm',
    color='black',
    alpha=0.7,
    size=20,
)

Note

  • In the Bokeh example, jitter is a plot option supported by HoloViews.

  • In the Matplotlib example, the jitter plot option is not supported by HoloViews. Therefore, we manually added jitter to the x-axis using random offsets to simulate the effect.

This web page was generated from a Jupyter notebook and not all interactivity will work on this website.