HOWTO · Seaborn
How to Create Seaborn Pie Chart
Create a pie chart with Matplotlib's plt.pie and Seaborn color palettes, then add labels, percentages, spacing, and styling.
On this page
Create a pie chart in Python by drawing it with Matplotlib’s plt.pie and choosing its slice colors with Seaborn’s sns.color_palette. Seaborn does not provide a pie-chart function of its own; in this workflow the two libraries have separate roles, because Seaborn supplies the colors while Matplotlib draws the chart. You need both libraries installed (pip install matplotlib seaborn). The examples below use numeric values for a handful of categories, label each slice, and show its share of the total; a second example restyles the chart as a donut with a legend, and a final example shows the error Matplotlib raises when the labels do not match the values.
Create a Pie Chart in Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
values = [35, 21, 29, 39, 11]
labels = ["A", "B", "C", "D", "E"]
colors = sns.color_palette("pastel", n_colors=len(values))
explode = [0.08, 0, 0, 0, 0]
plt.pie(
values,
labels=labels,
colors=colors,
autopct="%1.1f%%",
explode=explode,
startangle=90,
counterclock=False,
textprops={"fontsize": 10},
wedgeprops={"linewidth": 1, "edgecolor": "white"},
)
plt.show()
The code produces the chart above. The five values add up to 135, so the percentages printed on the slices are 25.9% for A, 15.6% for B, 21.5% for C, 28.9% for D, and 8.1% for E. Slice A sits slightly apart from the rest because of its explode offset.
Keep the values and labels aligned by position: the first label names the first value, the second label names the second value, and so on. Use a list of numeric quantities that describe parts of one whole, such as counts by category. The percentages are calculated from those quantities, so they describe each category’s share rather than a separately supplied percentage.
The list passed as values determines the relative size of each slice. plt.pie draws the chart, while sns.color_palette returns the colors for the slices. The palette name is a string; change "pastel" to another Seaborn palette such as "deep", "muted", "colorblind", "bright", or "dark". Setting n_colors from the data length requests one color per value.
The labels list supplies the category names in the same order as the values. The autopct format displays each value’s percentage of the sum of all values; for example, "%1.1f%%" formats the percentage with one digit after the decimal point. Use a different format string to change the displayed precision. The labels identify the categories, while the percentages make their proportions easy to compare.
To separate a slice visually, pass explode a sequence with one entry for each value. Each entry controls the offset of the corresponding slice, so the first 0.08 moves category A outward and the remaining zeros leave their slices in place. Adjust those entries to emphasize a different category, or use zeros throughout for an unoffset chart.
Customize the Pie Chart
You can tune the chart’s orientation and appearance with further plt.pie arguments. startangle rotates the starting position, and counterclock=False sets the direction in which the slices are drawn. Use shadow=True to add a shadow, textprops to set the label and percentage text properties, and wedgeprops to style the slice boundaries. One useful wedgeprops setting is width: it shrinks each wedge toward the rim and turns the pie into a donut chart, as the next example shows. Other arguments include frame=True, which adds the axes frame, center=(x, y), which places the pie’s center, and rotatelabels=True, which rotates the labels along their slices; these Boolean options take the Python values True or False, without quotation marks.
import matplotlib.pyplot as plt
import seaborn as sns
values = [58, 33, 6, 3]
labels = ["Desktop", "Mobile", "Tablet", "Other"]
colors = sns.color_palette("colorblind", n_colors=len(values))
wedges, texts = plt.pie(
values,
colors=colors,
startangle=90,
counterclock=False,
wedgeprops={"width": 0.45, "linewidth": 1, "edgecolor": "white"},
)
plt.legend(wedges, labels, title="Device", loc="center left", bbox_to_anchor=(1, 0.5))
plt.title("Sessions by Device")
plt.show()
Here, wedgeprops={"width": 0.45, ...} keeps only the outer 45% of each wedge, which leaves the center empty. Because the slices carry no labels this time, plt.pie returns the wedge objects, and passing them to plt.legend together with the label list builds the legend beside the chart. plt.title adds the heading. A legend is easier to read than labels on the slices when category names are long or several slices are thin; the example data sums to 100, so the segment sizes read directly as percentages (58%, 33%, 6%, and 3%).
Avoid Length and Value Errors
Every per-slice argument must match the data: labels, explode, and a color list each need exactly one entry per value, and every value must be non-negative, because a slice cannot take up a negative share of the whole. If the lengths disagree, plt.pie stops with a ValueError instead of drawing the chart:
import matplotlib.pyplot as plt
values = [35, 21, 29]
labels = ["A", "B"]
plt.pie(values, labels=labels)
plt.show()
Output:
ValueError: 'labels' must be of length 'x', not 2
The fix is to give labels one entry for each of the three values. The same rule applies to explode: a two-entry explode list for three values raises ValueError: 'explode' must be of length 'x', not 2. Negative entries in values fail as well, with ValueError: Wedge sizes 'x' must be non negative values. Finally, keep the number of categories small: with many thin slices, the labels collide and the shares become hard to compare, so group minor categories into an “Other” slice or switch to a bar chart.