14.8 C
London
Friday, June 28, 2024
HomeSoftware TutorialsPythonHow to Create a Pie Chart in Seaborn

How to Create a Pie Chart in Seaborn

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The Python data visualization library Seaborn doesn’t have a default function to create pie charts, but you can use the following syntax in Matplotlib to create a pie chart and add a Seaborn color palette:

import matplotlib.pyplot as plt
import seaborn as sns

#define data
data = [value1, value2, value3, ...]
labels = ['label1', 'label2', 'label3', ...]

#define Seaborn color palette to use
colors = sns.color_palette('pastel')[0:5]

#create pie chart
plt.pie(data, labels = labels, colors = colors, autopct='%.0f%%')
plt.show()

Refer to the Seaborn documentation for a complete list of color palettes.

The following examples show how to use this syntax in practice.

Example 1: Pie Chart with Pastel Seaborn Color Palette

The following code shows how to create a pie chart using the ‘pastel‘ Seaborn color palette:

import matplotlib.pyplot as plt
import seaborn as sns

#define data
data = [15, 25, 25, 30, 5]
labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5']

#define Seaborn color palette to use
colors = sns.color_palette('pastel')[0:5]

#create pie chart
plt.pie(data, labels = labels, colors = colors, autopct='%.0f%%')
plt.show()

Example 2: Pie Chart with Bright Seaborn Color Palette

The following code shows how to create a pie chart using the ‘bright‘ Seaborn color palette:

import matplotlib.pyplot as plt
import seaborn as sns

#define data
data = [15, 25, 25, 30, 5]
labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5']

#define Seaborn color palette to use
colors = sns.color_palette('bright')[0:5]

#create pie chart
plt.pie(data, labels = labels, colors = colors, autopct='%.0f%%')
plt.show()

These two examples illustrate how to create a pie chart with two different Seaborn color palettes.

However, there are many more styles you could use. Refer to the online documentation for a complete list of color palettes.

Additional Resources

How to Create Heatmaps in Seaborn
How to Create Barplots in Seaborn
How to Add a Title to Seaborn Plots

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories