Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Plotly · Function

px.violin

Creates an interactive violin plot for visualizing numerical-data distributions. It supports categorical comparisons, box plots, and optional observation points.

How it works

px.violin builds an interactive statistical distribution chart from a dataframe. Map a numeric column to y or x, optionally split distributions with a categorical dimension such as color, and use options such as box and points to add complementary distribution details.

Syntax

plotly.express.violin(data_frame=None, x=None, y=None, color=None, ..., box=False, points=None, violinmode=None, **kwargs)

Parameters

NameTypeDescription
data_framedata frameDataframe or compatible data source containing the columns used by the chart.
xstr or column-likeColumn or columns supplying the horizontal coordinate or categorical grouping.
ystr or column-likeColumn or columns supplying the numerical values.
colorstr or column-likeColumn used to split observations into colored distributions.
boxboolWhether to draw a box plot inside each violin.
pointsstr or FalseWhich observations to display as points: all, outliers, or False.

Returns

A Plotly Figure object.

Key points

  • Use it to show the distribution of numerical observations rather than only a single summary statistic.
  • Compared with a box plot, the violin shape exposes the estimated density and distribution form.
  • Long-form data is supported as the general input style for Plotly Express functions.
  • px.violin also accepts wide-form and mixed-form data, including multiple dataframe columns.
  • Pass the source dataframe as the first positional argument, or provide it with the data_frame keyword.
  • For advanced trace-level customization, the related go.Violin class provides the lower-level interface.

Common mistakes

  • With wide-form data, Plotly Express automatically assigns the variable names to x and the dataframe columns to y; specify axes explicitly when that default is not desired.
  • The points option accepts all, outliers, or False; choose deliberately because displaying every observation can make dense distributions difficult to read.

Example: Grouped violin plot

import plotly.express as px

df = px.data.tips()
fig = px.violin(
    df,
    x="day",
    y="total_bill",
    color="sex",
    box=True,
    points="all",
)
fig.show()

Creates an interactive violin chart of total bills by day, splitting distributions by sex and displaying both an inner box plot and all observations.

When to use it

Categorical distribution comparison

Compare a numerical measure across categories such as days, groups, or product types.

Exploratory distribution analysis

Inspect distribution shape, central tendency, and individual observations together.

Wide-form data visualization

Display several numeric dataframe columns as separate violins using wide-form input.

Interview questions

QWhat does px.violin visualize?

It creates a violin plot, which combines a distribution shape with statistical information similar to a box plot. The chart is returned as a Plotly figure and can be rendered with fig.show().

Follow-up: How would you show individual observations? Pass points="all", or use points="outliers" to limit markers to outliers.

QHow do you compare distributions across categories?

Use x and y to map dataframe columns to the chart axes. Add color to create separate distributions by category, and box=True to draw a box plot inside each violin.

Follow-up: How can several category violins be arranged? Use violinmode="group" for side-by-side violins or violinmode="overlay" to place them over one another.

QWhat dataframe shapes can the function handle?

The function accepts long-form data and also supports wide-form and mixed-form data. In wide form, a list of dataframe columns can be supplied, such as y=["experiment_1", "experiment_2"].

Follow-up: What alternative is available when Plotly Express is not sufficient? Use the more generic plotly.graph_objects.Violin class for lower-level trace control.

  • plotly.graph_objects.Violin
  • px.box
  • px.histogram
  • px.strip

Source: Violin plots in Python - Plotly. Checked 2026-10-15.