How can I create a pie chart using Seaborn?

To create a pie chart using Seaborn, first import the library and any necessary modules. Then, prepare the data in a format suitable for a pie chart, with labels and corresponding values. Next, use the Seaborn function “pieplot()” to generate the chart, specifying the data, labels, and any other desired customization options. Finally, display the chart using the Seaborn “show()” function. This will produce a visual representation of the data in the form of a circular chart with slices corresponding to the values given.

Create a Pie Chart in Seaborn


The Python data visualization library 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.pyplotas 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 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.pyplotas 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.pyplotas 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 for a complete list of color palettes.

Additional Resources

x