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 Create a Pie Chart in Seaborn?
A pie chart is a circular chart divided into slices to represent proportions of different categories in a dataset. While Seaborn doesn't have a direct pie chart function, we can combine Seaborn's color palettes with Matplotlib's pie() function to create visually appealing pie charts.
Seaborn is a Python data visualization library built on top of Matplotlib that provides high-level statistical graphics with beautiful default themes and color palettes.
Basic Pie Chart with Seaborn Colors
Let's create a simple pie chart using Matplotlib's pie() function with Seaborn's color palette ?
import matplotlib.pyplot as plt
import seaborn as sns
# Sample data
data = [76, 84, 62, 93, 79]
labels = ['Class A', 'Class B', 'Class C', 'Class D', 'Class E']
# Create pie chart with default colors
plt.pie(data, labels=labels, autopct='%.1f%%')
plt.title('Distribution of Grades Across Classes')
plt.show()
Enhanced Pie Chart with Seaborn Color Palette
Now let's enhance our pie chart using Seaborn's color palettes for better visual appeal ?
import matplotlib.pyplot as plt
import seaborn as sns
# Sample data
data = [60, 25, 35, 45, 55]
labels = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
# Define Seaborn color palette
colors = sns.color_palette('Set2', len(data))
# Create pie chart
plt.figure(figsize=(8, 6))
plt.pie(data, labels=labels, colors=colors, autopct='%.1f%%', startangle=90)
plt.title('Distribution of Items Across Categories')
plt.show()
Exploded Pie Chart with Custom Styling
We can create an exploded pie chart where certain slices are separated for emphasis ?
import matplotlib.pyplot as plt
import seaborn as sns
# Sample data
data = [30, 20, 25, 15, 10]
labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
# Explode specific slices (0 = no explosion, 0.1 = slight explosion)
explode = [0.1, 0, 0.1, 0, 0]
# Use Seaborn's pastel color palette
colors = sns.color_palette('pastel', len(data))
# Create exploded pie chart
plt.figure(figsize=(10, 8))
plt.pie(data, labels=labels, colors=colors, explode=explode,
autopct='%.1f%%', startangle=45, shadow=True)
plt.title('Sales Distribution by Product (Q4 2023)')
plt.axis('equal') # Ensure pie chart is circular
plt.show()
Available Seaborn Color Palettes
Seaborn offers various color palettes that work well with pie charts ?
import matplotlib.pyplot as plt
import seaborn as sns
# Sample data
data = [40, 30, 20, 10]
labels = ['North', 'South', 'East', 'West']
# Different Seaborn palettes
palettes = ['Set1', 'Set2', 'pastel', 'dark', 'bright', 'colorblind']
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
axes = axes.ravel()
for i, palette in enumerate(palettes):
colors = sns.color_palette(palette, len(data))
axes[i].pie(data, labels=labels, colors=colors, autopct='%.1f%%')
axes[i].set_title(f'Palette: {palette}')
plt.tight_layout()
plt.show()
Customization Options
| Parameter | Purpose | Example |
|---|---|---|
autopct |
Format percentage labels | '%.1f%%' |
startangle |
Rotate pie chart | 90 |
explode |
Separate slices | [0.1, 0, 0] |
shadow |
Add shadow effect | True |
Conclusion
While Seaborn doesn't have a dedicated pie chart function, combining Matplotlib's pie() with Seaborn's color palettes creates beautiful, professional-looking pie charts. Use different palettes and customization options to match your data visualization needs.
