Provide an example `seaborn.violinplot` call using `hue` with random generated data.
Oct 3, 12:15am

Here's a minimal example:

python
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
df = pd.DataFrame({
    "value": np.concatenate([
        rng.normal(0, 1, 150),
        rng.normal(1, 1.2, 150),
        rng.normal(0.5, 0.8, 150),
        rng.normal(1.5, 1.1, 150),
    ]),
    "group": ["A"] * 150 + ["B"] * 150 + ["A"] * 150 + ["B"] * 150,
    "condition": ["control"] * 300 + ["treated"] * 300,
})

sns.violinplot(
    data=df,
    x="group",
    y="value",
    hue="condition",
    split=True,       # draws the two hue levels back-to-back
    inner="quart",
    palette="Set2",
)

plt.show()

A few notes:

  • hue splits each x category by a second categorical variable (condition here).
  • split=True merges both hue levels into one violin shape (one per side); omit it to get two separate violins per category.
  • On seaborn ≥ 0.14, replace palette with hue="condition", palette=...-style mapping plus legend=True — actually palette combined with hue is still fine; the deprecation is about palette without hue.
  • inner="quart" shows the quartiles inside; other options are "box", "stick", "point", or None.
Oct 3, 12:15am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more