Violin Plot Examples

This example demonstrates violin plot functionality in PubliPlots, including simple violin plots, grouped violin plots, split violins, and combined violin+swarm plots.

Examples

import publiplots as pp
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Set style
pp.set_notebook_style()

Simple Violin Plot

Basic violin plot showing distribution by category.

# Create sample data
np.random.seed(42)
n = 200
violin_data = pd.DataFrame({
    'category': np.repeat(['A', 'B', 'C', 'D'], n // 4),
    'value': np.concatenate([
        np.random.normal(10, 2, n // 4),
        np.random.normal(15, 3, n // 4),
        np.random.normal(12, 2.5, n // 4),
        np.random.normal(18, 4, n // 4)
    ])
})

# Create simple violin plot
fig, ax = pp.violinplot(
    data=violin_data,
    x='category',
    y='value',
    title='Simple Violin Plot',
    xlabel='Category',
    ylabel='Value',
)
plt.show()
Simple Violin Plot

Violin Plot with Hue Grouping

Use the hue parameter to create grouped violin plots.

# Add group variable
violin_data['group'] = np.tile(['Group 1', 'Group 2'], n // 2)

# Create grouped violin plot
fig, ax = pp.violinplot(
    data=violin_data,
    x='category',
    y='value',
    hue='group',
    gap=0.1,
    title='Grouped Violin Plot',
    xlabel='Category',
    ylabel='Value',
    palette={'Group 1': '#8E8EC1', 'Group 2': '#75B375'},
)
plt.show()
Grouped Violin Plot

Split Violin Plot

Split violins to compare two groups side by side.

fig, ax = pp.violinplot(
    data=violin_data,
    x='category',
    y='value',
    hue='group',
    split=True,
    inner='quart',
    gap=0.1,
    title='Split Violin Plot',
    xlabel='Category',
    ylabel='Value',
)
plt.show()
Split Violin Plot

Horizontal Violin Plot

Create horizontal violin plots by swapping x and y.

fig, ax = pp.violinplot(
    data=violin_data[violin_data['group'] == 'Group 1'],
    x='value',
    y='category',
    title='Horizontal Violin Plot',
    xlabel='Value',
    ylabel='Category',
)
plt.show()
Horizontal Violin Plot

Combined Violin and Swarm Plot

Overlay swarm plot on violin plot to show distribution shape and individual data points.

fig, ax = plt.subplots(figsize=(6, 5))

# First, create the violin plot
pp.violinplot(
    data=violin_data[violin_data['group'] == 'Group 1'],
    x='category',
    y='value',
    ax=ax,
    inner=None,
)

# Then overlay the swarm plot
pp.swarmplot(
    data=violin_data[violin_data['group'] == 'Group 1'],
    x='category',
    y='value',
    ax=ax,
    alpha=1,
    legend=False,
    size=3,
)

ax.set_title('Combined Violin and Swarm Plot')
ax.set_xlabel('Category')
ax.set_ylabel('Value')
plt.tight_layout()
plt.show()
Combined Violin and Swarm Plot

Customization

Violin Plot with Custom Alpha

Adjust transparency of violin fill.

fig, ax = pp.violinplot(
    data=violin_data,
    x='category',
    y='value',
    hue='group',
    gap=0.1,
    title='Violin Plot with Custom Alpha',
    xlabel='Category',
    ylabel='Value',
    alpha=0.3,
)
plt.show()
Violin Plot with Custom Alpha

Violin Plot with Different Inner Representations

Compare different inner representations: box, quart, stick, point.

fig, axes = plt.subplots(2, 2, figsize=(10, 8))

inner_types = ['box', 'quart', 'stick', 'point']
for ax, inner in zip(axes.flat, inner_types):
    pp.violinplot(
        data=violin_data[violin_data['group'] == 'Group 1'],
        x='category',
        y='value',
        inner=inner,
        ax=ax,
        title=f'Inner: {inner}',
        xlabel='Category',
        ylabel='Value',
    )

plt.tight_layout()
plt.show()
Inner: box, Inner: quart, Inner: stick, Inner: point

One-Sided Violin Plots

Create one-sided (half) violin plots using the side parameter.

fig, axes = plt.subplots(1, 2, figsize=(12, 5))

# Left-sided violin
pp.violinplot(
    data=violin_data,
    x='category',
    y='value',
    hue='group',
    side='left',
    gap=0.1,
    ax=axes[0],
    title='Left-Sided Violin Plot',
    xlabel='Category',
    ylabel='Value',
    palette={'Group 1': '#8E8EC1', 'Group 2': '#75B375'},
)

# Right-sided violin
pp.violinplot(
    data=violin_data,
    x='category',
    y='value',
    hue='group',
    side='right',
    gap=0.1,
    ax=axes[1],
    title='Right-Sided Violin Plot',
    xlabel='Category',
    ylabel='Value',
    palette={'Group 1': '#8E8EC1', 'Group 2': '#75B375'},
)

plt.tight_layout()
plt.show()
Left-Sided Violin Plot, Right-Sided Violin Plot

Total running time of the script: (0 minutes 2.698 seconds)

Gallery generated by Sphinx-Gallery