How to create a violin plot with Plotly graph objects in Python

Plotly is a versatile Python graphing library that allows users to create interactive and visually appealing charts and plots. Within this library, graph objects serve as the low-level interface, offering users more control over a chart’s appearance and behavior. One of the specialized plots available is the violin plot—a method of depicting numeric data that aids in visualizing the distribution across varying levels of one or more categorical variables. This plot is unique in its design, merging features from the box and kernel density plots.

Features of the violin plot in Plotly graph objects

The violin plot in Plotly graph objects offers several notable features that make it a powerful tool for data visualization:

  • Two-sided visualization: The violin plot provides a mirrored visualization, comparing two data distributions side-by-side.

  • Kernel density estimation (KDE): It visualizes the estimated probability density function of the variable, showing the distribution shape of the data.

  • Inner box plot: Some violin plots in Plotly can display a mini box plot inside, indicating the quartiles and the median of the data, which provides a summary of the central tendency and spread of the data.

  • Customizable bandwidth: Users can adjust the bandwidth of the KDE, which can change the smoothness of the data visualization.

  • Points display: Individual data points can be displayed inside the violin, helping to identify outliers or specific data concentrations.

  • Span mode: It determines the span of the density estimation. Options like “soft” extend the density to the data range, whereas “hard” cuts the density off at the data range.

  • Customizable colors and themes: Like other plots in Plotly, the appearance of a violin plot can be customized to match desired aesthetics or themes.

  • Side-by-side comparisons: For categorical data, multiple violin plots can be positioned side-by-side for easy comparison between different categories.

  • Interactivity: Being a part of Plotly, violin plots come with built-in interactivity, such as zooming, panning, and hover tooltips for detailed data insights.

  • Flexibility with orientation: A violin plot can be oriented vertically or horizontally based on the user’s preference or the nature of the data.

Syntax

A violin plot syntax typically follows this structure using Plotly graph objects:

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Violin(
y=data, # Data for the y-axis
x=grouping_variable, # Optional: Categorical data for the x-axis
box_visible=True, # Display the inner box plot
line_color='blue',
meanline_visible=True # Display the mean line inside the violin
))
Syntax of a violin plot

Parameters

The following are the key parameters for creating a violin plot using Plotly graph objects:

  • y: It is a list or array of data values for the y-axis.

  • x: (Optional) It is a list or array corresponding to the categorical grouping variable for the x-axis. If specified, we’ll see separate violins for each category.

  • box_visible: It is of type boolean (default is False). If set to True, it displays a mini box plot inside the violin plot.

  • line_color: It sets the color of the line that encircles the violin plot.

  • meanline_visible: It is of type boolean (default is False). If True, the mean line is shown inside the violin.

  • points: It determines which and how points appear:

    • all: All the points are shown

    • outliers: Only the outlier points are shown

    • suspectedoutliers: Only the suspected outliers are shown

    • none: No points are shown

  • pointpos: It is of type float (ranging between -2 and 2) and contains the position of the sample points relative to the violin. For vertical (horizontal) violins, it determines the placement along the y (x) axis.

  • jitter: It is of type float (value between 0 and 1). The amount of jitter applied to the sample points is useful for displaying a small number of points.

  • scalemode: It determines how the violin plot width is scaled:

    • count: The width of the violins is scaled by the number of points in that bin or category.

    • width: All violins have the same width.

  • side: It determines on which side of the categorical axis the violin should appear:

    • both: It shows the violin on both sides.

    • positive: It shows the violin on the positive side.

    • negative: It shows the violin on the negative side.

  • fillcolor: It sets the fill color for the violin.

  • line: It is the dictionary that defines the aspects of the surrounding line:

    • width: The width of the surrounding line

    • color: Color of the surrounding line

  • spanmode: It determines the span of the density estimation:

    • soft: It extends the density to the data range.

    • hard: It cuts the density off at the data range.

  • orientation: It sets the orientation of the plot (v for vertical, h for horizontal).

  • name: It is the name of the trace, which appears in legends and hover labels.

  • hoverinfo: It determines what appears when hovering over the plot.

Return type

The return type of creating a violin plot using graph objects is a Figure object. This object can be manipulated further or displayed using methods like show(). The Figure contains all the information about the plot’s layout, data traces, and visualization settings.

Implementation

In the following playground, we create a violin plot using a sample dataset, gapminder, provided by Plotly Express. Attributes of the gapminder dataset (country, continent, year, lifeExp , pop , gdpPercap and species) are defined as follows:

  • country: The name of the country for the data point.

  • continent: The continent to which the country belongs.

  • year: The year in which the data was recorded.

  • lifeExp: The life expectancy of the population in years.

  • pop: The population of the country.

  • gdpPercap: The gross domestic product (GDP) per capita, which is the economic output per person in USD.

  • iso_alpha: The ISO alpha-3 code representing the country.

cd /usercode && python3 main.py
python3 -m http.server 5000 > /dev/null 2>&1 &
Generating a violin plot of life expectancy in 2007 for different continents using Plotly graph objects

Explanation

  • Lines 1–3: We import the necessary modules: plotly.graph_objects for creating custom plots, plotly.express for simplified plotting, and pandas for data manipulation.

  • Line 6: We load the gapminder dataset using Plotly Express’s built-in sample dataset.

  • Line 9: We print the first five rows of the loaded dataset using the head() method to inspect the data.

  • Line 12: We filter the df DataFrame to retain only rows where the year column is 2007. This filtered data is stored in a new DataFrame, df_2007.

  • Lines 15–20: We initialize a Figure object with a violin plot trace. Here’s a breakdown of the parameters inside go.Violin:

    • y=df_2007['lifeExp']: It specifies the y-axis data as the lifeExp (life expectancy) column from the df_2007 DataFrame.

    • x=df_2007['continent']: It specifies the x-axis data as the continent column from the df_2007 DataFrame. This creates separate violins for each continent.

    • box_visible=True: It indicates that an inner box plot should be displayed within each violin.

    • line_color='blue': It sets the color of the line surrounding each violin to blue.

  • Line 23: We display the finalized violin plot figure using the show() method.

Conclusion

The violin plot in Plotly graph objects offers a comprehensive visualization tool for understanding data distributions. Merging the density estimation of a KDE plot with the summarized representation of a box plot provides a holistic view of data spread and central tendencies. With Plotly’s interactive capabilities and customization options, the violin plot stands out as an intuitive and detailed approach for exploring datasets across diverse categories or groups.

Unlock your potential: Plotly Graphing and Visualization series, all in one place!

To deepen your understanding of data visualization using Plotly, explore our comprehensive Answer series below:

Plotly express: quick and intuitive visualization

Plotly Graph Objects: Customization and advanced features

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved