Python Pandas - Draw a violin plot and control order by passing an explicit order with Seaborn

A violin plot in Seaborn combines a boxplot with a kernel density estimate to show data distribution. The seaborn.violinplot() function creates these plots, and you can control the category order using the order parameter.

Creating Sample Data

Let's create sample cricket data to demonstrate violin plots ?

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt

# Create sample cricket data
data = {
    'Role': ['Batsman', 'Bowler', 'Batsman', 'Bowler', 'Batsman', 'Bowler', 
             'Batsman', 'Bowler', 'Batsman', 'Bowler', 'Batsman', 'Bowler'],
    'Age': [28, 32, 25, 29, 31, 27, 24, 30, 26, 33, 29, 28]
}

dataFrame = pd.DataFrame(data)
print(dataFrame.head())
      Role  Age
0  Batsman   28
1   Bowler   32
2  Batsman   25
3   Bowler   29
4  Batsman   31

Basic Violin Plot

Create a simple violin plot without specifying order ?

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
data = {
    'Role': ['Batsman', 'Bowler', 'Batsman', 'Bowler', 'Batsman', 'Bowler', 
             'Batsman', 'Bowler', 'Batsman', 'Bowler'],
    'Age': [28, 32, 25, 29, 31, 27, 24, 30, 26, 33]
}

dataFrame = pd.DataFrame(data)

# Basic violin plot
plt.figure(figsize=(8, 6))
sb.violinplot(x='Role', y='Age', data=dataFrame)
plt.title('Age Distribution by Role')
plt.show()

Controlling Order with the order Parameter

Use the order parameter to specify explicit category ordering ?

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
data = {
    'Role': ['Batsman', 'Bowler', 'Batsman', 'Bowler', 'Batsman', 'Bowler', 
             'Batsman', 'Bowler', 'Batsman', 'Bowler'],
    'Age': [28, 32, 25, 29, 31, 27, 24, 30, 26, 33]
}

dataFrame = pd.DataFrame(data)

# Violin plot with explicit order
plt.figure(figsize=(8, 6))
sb.violinplot(x='Role', y='Age', order=['Batsman', 'Bowler'], data=dataFrame)
plt.title('Age Distribution by Role (Ordered: Batsman, Bowler)')
plt.show()

Reversing the Order

You can also reverse the order by changing the sequence in the order list ?

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
data = {
    'Role': ['Batsman', 'Bowler', 'Batsman', 'Bowler', 'Batsman', 'Bowler', 
             'Batsman', 'Bowler', 'Batsman', 'Bowler'],
    'Age': [28, 32, 25, 29, 31, 27, 24, 30, 26, 33]
}

dataFrame = pd.DataFrame(data)

# Violin plot with reversed order
plt.figure(figsize=(8, 6))
sb.violinplot(x='Role', y='Age', order=['Bowler', 'Batsman'], data=dataFrame)
plt.title('Age Distribution by Role (Ordered: Bowler, Batsman)')
plt.show()

Key Parameters

Parameter Description Example
x Categorical variable for x-axis 'Role'
y Numerical variable for y-axis 'Age'
order Explicit order for categories ['Batsman', 'Bowler']
data DataFrame containing the data dataFrame

Conclusion

Use the order parameter in seaborn.violinplot() to control category ordering. This helps create consistent, meaningful visualizations by placing categories in logical sequences rather than alphabetical order.

Updated on: 2026-03-26T13:23:18+05:30

476 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements