Categorical distribution comparison
Compare a numerical measure across categories such as days, groups, or product types.
Plotly · Function
Creates an interactive violin plot for visualizing numerical-data distributions. It supports categorical comparisons, box plots, and optional observation points.
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.
plotly.express.violin(data_frame=None, x=None, y=None, color=None, ..., box=False, points=None, violinmode=None, **kwargs)| Name | Type | Description |
|---|---|---|
data_frame | data frame | Dataframe or compatible data source containing the columns used by the chart. |
x | str or column-like | Column or columns supplying the horizontal coordinate or categorical grouping. |
y | str or column-like | Column or columns supplying the numerical values. |
color | str or column-like | Column used to split observations into colored distributions. |
box | bool | Whether to draw a box plot inside each violin. |
points | str or False | Which observations to display as points: all, outliers, or False. |
A Plotly Figure object.
px.violin also accepts wide-form and mixed-form data, including multiple dataframe columns.data_frame keyword.go.Violin class provides the lower-level interface.x and the dataframe columns to y; specify axes explicitly when that default is not desired.points option accepts all, outliers, or False; choose deliberately because displaying every observation can make dense distributions difficult to read.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.
Compare a numerical measure across categories such as days, groups, or product types.
Inspect distribution shape, central tendency, and individual observations together.
Display several numeric dataframe columns as separate violins using wide-form input.
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.
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.
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.Violinpx.boxpx.histogrampx.strip