Visualizing Data with Pie Charts and Seaborn in Python
Data visualization is a crucial skill for any data scientist or analyst. Being able to create clear, informative, and visually appealing charts, plots and graphics is essential for exploring datasets, understanding patterns and relationships, and communicating insights to others.
Python has emerged as the go-to programming language for data science and one of the top reasons is the powerful data visualization tools and libraries available. While Matplotlib provides the foundation for plotting in Python, other libraries like Seaborn, Plotly, Bokeh, Altair and others have been built on top of it to allow for creating beautiful statistical graphics in just a few lines of code.
In this article, we‘ll take a closer look at visualizing data with Python and Seaborn, with a focus on one of the most commonly used plots for showing proportions – the pie chart. We‘ll walk through how to create and customize pie charts and also touch on some other plot types available in Seaborn for visualizing categorical data.
What is a Pie Chart?
A pie chart (or a circle chart) is a circular statistical graphic, which is divided into slices to illustrate numerical proportion. In a pie chart, the arc length of each slice (and consequently its central angle and area), is proportional to the quantity it represents.
It‘s named for its resemblance to a pie which has been sliced. The earliest known pie charts are generally credited to William Playfair‘s Statistical Breviary of 1801.
Pie charts are generally used to show percentage or proportional data and usually the percentage represented by each category is provided next to the corresponding slice of pie. Pie charts are good for displaying data for around 6 categories or fewer.
After that, the slices tend to get too small and hard to distinguish. They work best when the values are quite different. With similar values, it‘s hard to see the differences between the slice sizes.
Creating a Pie Chart with Seaborn
Now let‘s see how to create a pie chart in Python using Seaborn. First, we need to import the necessary libraries and create some example data to plot:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Create example data
data = {‘Cat‘: 40, ‘Dog‘: 30, ‘Fish‘: 15, ‘Turtle‘: 7, ‘Hamster‘: 5, ‘Other‘: 3}
Here we have a dictionary showing the percentage breakdown of popular pet types. To create a basic pie chart with Seaborn, we can pass the dictionary keys to label and the values to sizes:
plt.pie(x=list(data.values()), labels=list(data.keys()))
plt.show()

This gives us a basic pie chart showing the breakdown of pet types. However, there‘s a lot we can do to customize and improve this chart.
Customizing a Seaborn Pie Chart
Seaborn provides several parameters we can use to adjust the appearance of our pie chart:
colors = sns.color_palette(‘bright‘)[0:5]
plt.pie(data.values(), labels = data.keys(), colors = colors, autopct=‘%.0f%%‘)
plt.title(‘Most popular pet types‘, fontsize=14);
Here‘s what we did:
- Specified custom colors using one of Seaborn‘s built-in color palettes
- Used
autopctto display the percentage value for each category - Added a title to the chart

Other customizations you can do include:
- Exploding a slice using
explodeto emphasize it - Rotating the labels using
startangle - Adjusting the label text size with
textprops - Adding a legend with
plt.legend()
When to (and not to) use a Pie Chart
Pie charts are popular because they give an immediate visual sense of the relative proportions of different categories. However, they have some limitations and are not always the best choice:
- Pie charts become less accurate at showing data for more than 5-6 categories
- They don‘t show exact values (unless you add labels)
- It‘s hard to compare the size of slices, especially if the values are close
- They take up a lot of space compared to the information conveyed
In general, pie charts work best when:
- You have a small number of categories (2-5)
- Each category represents a large percentage of the total (at least 5%)
- The values of each category are very different
If these conditions aren‘t met, consider using an alternative like a bar chart or table instead. Speaking of alternatives, let‘s look at some other ways to visualize the same data.
Other Plot Types for Proportional Data
Donut Charts
A donut chart is essentially a pie chart with a hole in the center. Some argue this focuses attention on the proportions better than a pie chart. You can create one by setting the `wedgeprops` parameter:
plt.pie(data.values(), labels=data.keys(), wedgeprops={‘width‘: 0.5});

Bar Charts
A bar chart is a good alternative for showing counts or proportions, especially with more than 5 categories. With Seaborn, you can use `sns.barplot()`:
plt.figure(figsize=(8,4))
ax = sns.barplot(x=list(data.keys()), y=list(data.values()))
plt.xlabel(‘Pet type‘)
plt.ylabel(‘Percentage‘);

Treemaps
For hierarchical or nested proportional data, a treemap can be a good choice. It shows proportion by area like a pie chart but can also show nesting.
import squarify
plt.figure(figsize=(8,4))
squarify.plot(sizes=data.values(), label=data.keys(), alpha=.7 )
plt.axis(‘off‘)

These are just a few examples – Seaborn has many other statistical plot types worth checking out like count plots, box plots, violin plots, swarm plots, and more.
Tips for Effective Visualizations
Regardless of what type of plot you create, here are some general tips to keep in mind:
-
Know your audience. Who will be viewing this visualization? What is the key message you want them to take away? Let that guide your design choices.
-
Keep it simple. Don‘t try to cram too much into one graphic. If needed, break it up into multiple focused charts.
-
Choose the right chart type for your data and message. Is it proportional data? Consider a pie or donut chart. Is it counts of a category? Try a bar plot. Showing a distribution? Go for a histogram or density plot.
-
Use color meaningfully, not gratuitously. Use color to highlight or for categorization, and pick a palette that is color-blind friendly and won‘t distract from the data.
-
Pay attention to text sizing and labels. Make sure labels are clear and readable. The title should communicate the main message.
-
Always include a legend, data source, and any relevant context. Don‘t make your audience guess what they‘re looking at.
-
Be wary of chart junk. 3D charts, unnecessary animations, garish colors, etc. usually just distract rather than inform. Keep the data-to-ink ratio high.
Conclusion
We‘ve covered a lot of ground in this post! To recap, we focused on visualizing proportional data using pie charts and Python‘s Seaborn library.
We walked through how to create a basic pie chart, looked at some ways to customize it, and touched on a few alternative plot types to consider like donut charts, bar plots and treemaps.
The main takeaways are:
- Pie charts are best for showing proportions when you have a small number of very different sized categories.
- Seaborn makes it easy to create and customize pie charts and many other statistical graphics in just a few lines of Python code
- Always pick your visualization intentionally based on the data type, key message, and audience
- Keep the chart simple, informative, and visually appealing
I encourage you to explore the Seaborn gallery and documentation to see what other plot types are available. The best way to learn data visualization is to practice, so find some data you‘re interested in and start visualizing it!
What other tips and best practices for data visualization do you recommend? Leave a comment below to share your thoughts.