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.
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.
A violin plot syntax typically follows this structure using Plotly graph objects:
import plotly.graph_objects as gofig = go.Figure()fig.add_trace(go.Violin(y=data, # Data for the y-axisx=grouping_variable, # Optional: Categorical data for the x-axisbox_visible=True, # Display the inner box plotline_color='blue',meanline_visible=True # Display the mean line inside the violin))
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.
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.
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 &
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.
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 and its methods
Learn the core concepts of Plotly Graph Objects, including its structure, methods, and how to create fully customized visualizations.
Creating a density heatmap plot with Plotly Express in Python
Learn to visualize data density using heatmaps, making patterns in large datasets easy to interpret.
How to create a line plot with Plotly Express in Python
Master the basics of line plots to represent trends over time and relationships between variables.
How to create a bar plot with Plotly Express in Python
Understand how to create bar plots to compare categorical data effectively.
How to create a histogram with Plotly Express in Python
Explore histograms to analyze data distribution and frequency counts efficiently.
How to create a box plot with Plotly Express in Python
Learn to use box plots for statistical visualization, identifying outliers and data spread.
How to create a violin plot with Plotly Express in Python
Combine box plots and KDE plots to compare data distributions effectively.
How to create a 3D line plot with Plotly Express in Python
Extend your data visualization skills by creating 3D line plots for multi-dimensional data representation.
How to create a choropleth map with Plotly Express in Python
Learn how to create geospatial visualizations with choropleth maps for regional data analysis.
Creating parallel coordinates plots with Plotly Express in Python
Visualize multi-dimensional data efficiently with parallel coordinate plots for feature comparison.
How to create a scatter plot on a Mapbox map with Plotly Express
Utilize Mapbox maps to plot scatter data points based on geographic coordinates.
Creating a scatter plot matrix with Plotly Express in Python
Understand relationships between multiple numerical variables using scatter plot matrices.
Plotly Graph Objects: Customization and advanced features
How to create a 3D surface plot with Plotly Graph Objects
Create 3D surface plots for visualizing complex surfaces and mathematical functions.
How to create a box plot with Plotly Graph Objects in Python
Gain full control over box plots, including styling, custom axes, and multiple data series.
How to create a 3D scatter plot with Plotly Express in Python
Visualize high-dimensional data using 3D scatter plots for better insight.
Creating a histogram plot with Plotly Graph Objects in Python
Customize histogram bins, colors, and overlays using Plotly Graph Objects for in-depth analysis.
How to create a bar plot with Plotly Graph Objects in Python
Build highly customizable bar plots, adjusting layout, colors, and interactivity.
How to create a heatmap plot with Plotly Graph Objects in Python
Generate heatmaps with flexible color scales and annotations for better data storytelling.
How to create a pie plot with Plotly Graph Objects in Python
Learn to create pie charts with custom labels, colors, and hover interactions.
Creating a Choropleth plot with Plotly Graph Objects in Python
Explore geospatial visualizations with advanced choropleth maps for regional comparisons.
How to create a violin plot with Plotly Graph Objects in Python
Customize violin plots to represent distribution, density, and probability density functions.
How to create a scatter plot with Plotly Graph Objects in Python
Learn to create scatter plots with detailed hover information, styling, and annotations.
How to create a table with Plotly Graph Objects in Python
Build interactive tables with styling options for presenting structured data.
How to create a bubble plot with Plotly Graph Objects in Python
Understand how to create bubble plots to visualize three variables in a single chart.
Create a 3D scatter plot with Plotly Graph Objects in Python
Explore multi-dimensional data using customized 3D scatter plots.
Creating a density contour plot with Plotly Express in Python
Learn how to visualize data density using contour plots to detect clusters.
How to create a scatter plot with Plotly Express in Python
Master scatter plots to identify correlations, trends, and patterns in datasets.
Free Resources