Mastering Horizontal Violin Plots in Python with Seaborn and Matplotlib

Mastering Horizontal Violin Plots in Python with Seaborn and Matplotlib


Data visualization is a critical aspect of data analysis. It not only allows us to understand the underlying patterns in the data but also communicates these insights effectively. While vertical plots are commonly used, horizontal plots offer a different perspective and can sometimes be more insightful. In this article, we’ll explore how to create horizontal violin plots using Python libraries Seaborn and Matplotlib, with the Iris dataset serving as our example.

What is Seaborn?

Seaborn is a Python data visualization library based on Matplotlib that provides a high-level interface for generating various types of informative and attractive statistical graphics. With Seaborn, you can produce a variety of plots, including violin plots, with ease.

What is Matplotlib?

Matplotlib is a plotting library for Python and its numerical mathematics extension, NumPy. It provides the foundation upon which Seaborn is built. Matplotlib allows for a wide variety of plotting options and is highly customizable.

Why Use Horizontal Violin Plots?

Here are some reasons to consider using horizontal violin plots:

1. Better Use of Space: Horizontal layouts can be easier to fit into certain types of presentations or dashboards.
2. Readability: Some people find horizontal plots easier to read, especially when the labels are long or numerous.
3. Emphasis on Variation: A horizontal layout can emphasize the variation along the y-axis.

Setting Up the Environment

Before plotting, it’s often a good idea to set up the visualization environment. Seaborn allows you to set the aesthetic parameters like background grid style, which can be done using `sns.set(style=”darkgrid”)`.

Coding a Horizontal Violin Plot

Switching from a vertical to a horizontal violin plot in Seaborn is simple—just switch the `x` and `y` parameters in the `sns.violinplot()` function. Below is the code snippet to demonstrate this:

# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt

# Set background style

# Load dataset
df = sns.load_dataset('iris')

# Create horizontal violin plot
sns.violinplot(y=df["species"], x=df["sepal_length"])

Code Explanation

– Import Libraries: Importing Seaborn and Matplotlib for plotting.
– Set Background: `sns.set(style=”darkgrid”)` sets a dark grid background. If you’re using Seaborn version 0.11.0 or above, you can also use `sns.set_theme()`.
– Load Dataset: The Iris dataset is loaded into a DataFrame using `sns.load_dataset(‘iris’)`.
– Create Horizontal Violin Plot: We switch the `x` and `y` parameters to make the violin plot horizontal.

End-to-End Example

Here’s the complete code example:

# Import necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt

# Set the background style

# Load the Iris dataset
df = sns.load_dataset('iris')

# Create the horizontal violin plot
sns.violinplot(y=df["species"], x=df["sepal_length"])

# Add title and labels
plt.title('Horizontal Violin Plot of Sepal Length by Species')
plt.xlabel('Sepal Length (cm)')

# Show the plot


Horizontal violin plots offer an alternative perspective to vertical violin plots and can be particularly useful when you have long or numerous labels. With Python’s Seaborn and Matplotlib libraries, creating horizontal violin plots is straightforward. Whether you’re new to data visualization or looking to add another tool to your data visualization toolbox, horizontal violin plots are worth mastering.

Find more … …

Exploring Iris Data Visualization with Seaborn’s Violin Plot in Python

How to generate Violin plots in R using ggpubr package

R Data Visualisation Example – A Guide to Violin plot by group in R using ggplot2