How to Create a Pie Chart with Seaborn Easily?

Rate this post

Seaborn is a data visualization library for Python. This tutorial will briefly describe simple techniques for styling a pie chart using only a single function from this robust library. 

Although Seaborn does not include a function to build pie charts, it can be used to refine the aesthetics of pie charts created with Matplotlib. The first step will therefore be to create a pie chart with Matplotlib.

An Easy Example of Seaborn Pie Chart

The following short program creates a basic pie chart illustrating the numerical proportion of the first ten prime numbers and labeling those proportions with the first ten letters of the alphabet:

import matplotlib.pyplot as plt
 
data = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
plt.pie(data, labels=letters)
plt.show()

Now, let’s see how that same pie chart looks after we use Seaborn to alter various themes.

How to Install Seaborn?

The first step should be installing the library. Seaborn can be easily installed using the Python Package Installer. Running the following command from the terminal installs the latest version of Seaborn:

pip install seaborn

How to Check the Version of Seaborn?

If Seaborn is already installed on your system, you may check which version of the library has been installed by running the pip show terminal command:

pip show seaborn

How to Modify a Seaborn Pie Chart with set_theme()

Knowing the version, we may now use Seaborn to modify our pie chart by importing the library and calling the set_theme() function. When called without parameters, this function applies the default Seaborn visual themes to plots produced by the Seaborn and Matplotlib libraries.

Note that versions of Seaborn before 0.8 call the set_theme() function implicitly when the library is imported. Code examples here were produced with Seaborn version 0.11.2 so will call set_theme() explicitly.

import matplotlib.pyplot as plt
import seaborn as sns
 
sns.set_theme()
 
data = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
plt.pie(data, labels=letters)
plt.show()

The set_theme() function can also take seven parameters and this is what the library produces with each of these set to the default value. In the following sections we will experiment with changing the values of several of these parameters.

How to Color a Pie Chart with Seaborn?

The default Seaborn color palette called β€˜deep’ is a slightly less intense version of the default matplotlib color palette. For instance, the default matplotlib color palette can be retained by calling sns.set_theme() with the β€œpalette” value set to β€œtab10” as follows:

import matplotlib.pyplot as plt
import seaborn as sns
 
sns.set_theme(palette="tab10")
 
data = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
plt.pie(data, labels=letters)
plt.show()

Seaborn includes six variations of β€œtab10”, the default matplotlib standard color palette.

The set_theme() function defaults to one of these, the β€œdeep” palette, but I encourage you to experiments with five others (β€œmuted”, β€œpastel”, β€œbright”, β€œdark”, and β€œcolorblind”) and to peruse the Seaborn color palette documentation (link) for other options.

How to Set Chart Fonts and Changing Font Element Sizes with Seaborn?

In addition to modifying the color palette, the set_theme() function can accept a number of other parameters.

Many of these other parameters, however, do not apply to the pie chart as it is a proportional representation and unlike many other standard charts (e.g. bar charts, box plots, line plots, etc.) has no x, y axes, and does not support a grid background.

Our chart does include labels and Seaborn’s set_theme() function can modify the font and size of these characters with the β€œfont” and β€œfont_scale” parameters. These parameters take a string value matching a font or font family from the matplotlib font manager and a float respectively.

The font defaults to the β€œsans-serif” family so let’s go ahead and change our font-family to β€œserif”, increase the font size slightly, and change the color palette to β€œdark” while we’re at it:

import matplotlib.pyplot as plt
import seaborn as sns
 
sns.set_theme(palette="dark", font="serif", font_scale= 1.5)
data = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
plt.pie(data, labels=letters)
plt.show()

In addition to β€œserif” and β€œsans-serif”, and the other font families (β€œcursive”, β€œfantasy”, and β€œmonospace”) the set_theme() function can accept any font available in the matplotlib library as a value for the font parameter.

🧩 Try out “Comic Sans MS“, β€œHelvetica”, “Times New Roman“, “Papyrus” or another favorite.

The following short program will print all the fonts available in your current installation of matplotlib.

import matplotlib.font_manager as fm

for font in fm.fontManager.ttflist:
    print(font.name)

I hope this brief tutorial has been helpful. Good luck exploring Seaborn and please be sure to check the project documentation (link) to learn more about what this library can offer.