~ 3 min read

Creating Pie Charts with Seaborn in Python

By: Adam Richardson
Share:

Creating Pie Charts with Seaborn in Python

Introduction to Pie Charts with Seaborn

Pie charts are a popular way to represent and visualize data by displaying the relative proportions of various categories within a single donut-shaped chart. Seaborn, a powerful Python library built on top of Matplotlib, eases the process of creating aesthetically pleasing and informative statistical graphics. In this article, we will explore how to create pie charts using Seaborn to enhance data analysis and visualization for professionals working with data.

Properties and Parameters of Pie Charts with Seaborn

Though Seaborn does not have a dedicated pie chart function, we can achieve the desired result by utilizing a combination of Seaborn components and the matplotlib.patches.Wedge class. Let’s explore the properties and parameters involved in creating pie charts with Seaborn:

Seaborn Barplot

To create pie charts, we need the Seaborn barplot() function to display data in a bar chart format, which will then be transformed into a pie chart. Some useful parameters of barplot() are:

  • x: Categorical data to be represented on the x-axis.
  • y: A sequence of datapoints to be represented as bars.
  • hue: Optional categorical variable to group bars by color.
  • palette: A Seaborn palette or a list of colors to choose for the bars.

Matplotlib.Patches.Wedge

To transform the bar chart into a pie chart, we use the Wedge class from the matplotlib.patches module, which takes the following parameters:

  • center: Tuple of (x, y) coordinates for the center of the wedge.
  • r: Radius of the pie chart.
  • theta1: Starting angle of the wedge (in degrees).
  • theta2: Ending angle of the wedge (in degrees).
  • color: The wedge color.

Simplified Real-Life Example

Consider representing the market share of different smartphone brands using a pie chart. Here is a simple example using Seaborn and Matplotlib:

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge

# Sample data
smartphone_brands = ["Apple", "Samsung", "Huawei", "Xiaomi", "OPPO"]
market_share = [20, 19, 16, 12, 10]

# Pie chart creation
fig, ax = plt.subplots(figsize=(8, 8))
sns.barplot(x=market_share, y=smartphone_brands, ax=ax)
ax.pie(market_share, labels=smartphone_brands, autopct="%.1f%%", startangle=90)
plt.title("Market Share of Smartphone Brands")
plt.show()

Complex Real-Life Example

Suppose we want to represent the market share of different smartphone brands segmented by user age groups. Here is a more complex example:

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge

# Sample data
smartphone_brands = ["Apple", "Samsung", "Huawei", "Xiaomi", "OPPO"]
market_share = {
    "18-24": [25, 22, 14, 10, 9],
    "25-34": [23, 20, 18, 11, 10],
    "35-44": [17, 18, 20, 12, 9],
    "45-54": [15, 19, 17, 14, 11],
    "55+": [10, 20, 15, 16, 12],
}

fig, axes = plt.subplots(3, 2, figsize=(14, 20), dpi=80)

for i, age_group in enumerate(market_share):
    row, col = divmod(i, 2)
    sns.barplot(x=market_share[age_group], y=smartphone_brands, ax=axes[row][col])
    axes[row][col].pie(
        market_share[age_group],
        labels=smartphone_brands,
        autopct="%.1f%%",
        startangle=90,
    )
    axes[row][col].set_title(f"Market Share of Smartphone Brands ({age_group})")

plt.tight_layout()
plt.show()

Personal Tips

  1. Choose a color palette that complements your data and makes the pie chart easy to read. Seaborn provides numerous options.
  2. Customize labels and legends to provide additional information and make the chart more accessible to a wider audience.
  3. Consider using other chart types if pie charts do not effectively convey the information. Seaborn offers a range of visualization alternatives like bar charts, line charts, and scatter plots.
  4. Use Seaborn themes to customize and enhance the appearance of your charts, making them more visually appealing.
  5. Experiment with various Seaborn features to unlock the full potential of your data visualization and analysis journey.
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts