Using Matplotlib and Seaborn, you can create a pie chart in your Python code.
Seaborn is a fantastic statistical data visualization package, but does not give us the ability to create a pie chart. However, we can create a pie chart using Matplotlib and add a Seaborn color palette.
We can create a “Seaborn” Pie Chart very easily with the following Python code:
import matplotlib.pyplot as plt
import seaborn as sns
data = [25,50,15,45]
labels = ["West", "East", "South", "North"]
#Read in Seaborn color palette
colors = sns.color_palette('hls')[0:4]
#Create pie chart with Matplotlib
plt.pie(data, labels = labels, colors = colors, autopct='%1.1f%%')
plt.show()
Here is the pie chart from the code above:
Using Different Seaborn Color Palettes in Matplotlib Pie Charts
When visualizing data, the ability to create and view pie charts is very useful. When using Python to visualize data, the Seaborn package is great, but doesn’t give us the ability to create a pie chart. Matplotlib on the other hand can create pie charts very easily.
Seaborn has wonderful color palettes, and with these color palettes, we can create beautiful “Seaborn” pie charts.
Let’s take the same data from above.
For example, if we want to create a pie chart in Python using the “hls” color space, we just need to pass ‘hls’ to the Seaborn color_palette() function.
import matplotlib.pyplot as plt
import seaborn as sns
data = [25,50,15,45]
labels = ["West", "East", "South", "North"]
#Read in Seaborn color palette
colors = sns.color_palette('hls')[0:4]
#Create pie chart with Matplotlib
plt.pie(data, labels = labels, colors = colors, autopct='%1.1f%%')
plt.show()
Here’s the pie chart with the color scheme using the “hls” color space:
If we want to use the “Paired” color palette, we just need to pass “Paired” to the Seaborn color_palette() function.
import matplotlib.pyplot as plt
import seaborn as sns
data = [25,50,15,45]
labels = ["West", "East", "South", "North"]
#Read in Seaborn color palette
colors = sns.color_palette('Paired')[0:4]
#Create pie chart with Matplotlib
plt.pie(data, labels = labels, colors = colors, autopct='%1.1f%%')
plt.show()
Here’s the pie chart with the color scheme using the “Paired” color palette:
Hopefully, this article has been helpful for you to learn how to use Matplotlib and Seaborn to create a pie chart in your Python code.