Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to make Violinpot with data points in Seaborn?
In data analysis and visualization, violin plots are powerful tools for visualizing the distribution of numeric data across different categories. Unlike box plots, violin plots show the full distribution shape by combining a box plot with a kernel density estimation. In this tutorial, we will learn how to create violin plots with data points using Seaborn.
To create violin plots in Seaborn, we need to import the necessary libraries: Seaborn for plotting, Matplotlib for customization, and Pandas for data manipulation.
Syntax
The basic syntax for creating a violin plot with data points is ?
import seaborn as sns import matplotlib.pyplot as plt # Create violin plot with data points sns.violinplot(data=df, x="category", y="values", inner="points") plt.show()
The key parameter is inner="points" which displays individual data points inside the violin shapes.
Parameters
Important parameters for violinplot() include ?
x ? The column name for categorical variable (x-axis)
y ? The column name for numeric variable (y-axis)
data ? The DataFrame containing the data
inner ? Controls interior markings: "points", "box", "quart", or "stick"
palette ? Color palette for different categories
Example 1: Basic Violin Plot with Data Points
Let's create a dataset and visualize it using a violin plot with data points ?
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Create sample dataset
np.random.seed(42)
category_a = np.random.normal(0, 1, 100)
category_b = np.random.normal(2, 1.5, 100)
data = pd.DataFrame({
'Category': ['A'] * 100 + ['B'] * 100,
'Values': np.concatenate([category_a, category_b])
})
# Create violin plot with data points
plt.figure(figsize=(8, 6))
sns.violinplot(x='Category', y='Values', data=data, inner='points', palette='Set2')
plt.title('Violin Plot with Data Points')
plt.xlabel('Category')
plt.ylabel('Values')
plt.show()
A violin plot showing two categories with individual data points visible inside each violin shape.
Example 2: Multiple Groups with Custom Styling
Here's an example with exam scores across three groups ?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Create exam scores dataset
data = {
'Group A': [75, 80, 85, 90, 70, 65, 90, 85, 80, 75],
'Group B': [80, 85, 90, 95, 75, 70, 95, 90, 85, 80],
'Group C': [85, 90, 95, 100, 80, 75, 100, 95, 90, 85]
}
# Convert to long format for seaborn
df = pd.melt(pd.DataFrame(data), var_name='Group', value_name='Score')
# Create violin plot
plt.figure(figsize=(10, 6))
sns.violinplot(x='Group', y='Score', data=df, inner='points', palette='viridis')
plt.title('Exam Scores Distribution by Group')
plt.xlabel('Student Groups')
plt.ylabel('Exam Scores')
plt.show()
A violin plot displaying exam score distributions for three groups with individual data points shown inside each violin.
Different Inner Styles
You can customize the inner appearance of violin plots ?
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
np.random.seed(42)
data = pd.DataFrame({
'Group': np.repeat(['A', 'B'], 50),
'Values': np.concatenate([np.random.normal(0, 1, 50), np.random.normal(1, 1.2, 50)])
})
# Create subplots for different inner styles
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
styles = ['points', 'box', 'quart', 'stick']
for i, style in enumerate(styles):
row, col = i // 2, i % 2
sns.violinplot(x='Group', y='Values', data=data, inner=style, ax=axes[row, col])
axes[row, col].set_title(f'Inner = "{style}"')
plt.tight_layout()
plt.show()
Four violin plots showing different inner styles: points (scatter points), box (box plot), quart (quartile lines), and stick (individual observations as lines).
Conclusion
Violin plots with data points provide a comprehensive view of data distribution, combining density estimation with individual data points. Use inner="points" to show all data points, making it ideal for smaller datasets or when you need to see outliers clearly.
