. Advertisement .
. Advertisement .
A pie chart allows users to view all the data in a circular graph, which ensures better data analysis. The following article will introduce about seaborn pie chart and some easy methods to create one.
What Is A Seaborn Pie Chart?
A pie chart is a part of the Seaborn information visualization framework. The framework offers a user-intuitive interface with instructive and compelling analytical visuals.
The pie chart resembles a spherical figure with various colored wedges containing data. In this chart, the numerical information is distributed to determine the segment’s size.
A pie chart is often employed to analyze the numeric compositions and values. It shows the percentage of all records over a total. An element with higher relative ratio will have a larger wedge dimension and percentile.
How To Create A Pie Chart In Seaborn?
The best way to create a pie chart in Seaborn is to employ the Matplotlib’s pie attributes and the Seaborn color pallets. The comprehensive Matplotlib library is widely used to create animated, interactive, and static Python visualizations.
This multi-platform data visualization library comes with various Numpy arrays and works best with the broad Scipy stack. Here is the syntax to install these two necessary components to create a pie chart:
pip install seaborn pip install matplotlib
Now let’s create a random pie chart with the code as follows:
import matplotlib.pyplot as plt import seaborn as sns data = [35, 21, 29, 39, 11] colors = sns.color_palette('pastel') plt.pie(data, colors = colors)
In this example, the Seaborn pastel color pallets are employed by default. Yet, you can adjust the color palette to your preference. Seaborn supports a wide range of color options like colorblind, deep, dark, bright, or muted.
Make sure that the color pallet is passed as a string in the attribute
color_palette(). Besides the color, the pie attributes allow for various changes thanks to its wide range of parameters. Adding new labels is a prime example.
You can also use the autopct parameter to add the text or the slice percentage inside the slice.
import matplotlib.pyplot as plt import seaborn as sns data = [35, 21, 29, 39, 11] labels = ['slice 1', 'slice 2', 'slice 3', 'slice 4', 'slice 5'] colors = sns.color_palette('bright') plt.pie(data, labels=labels,colors = colors, autopct = '%0.0f%%') plt.show()
Use the pctdistance parameter to change the default text distance. The label distance is set by the labeldistance parameter. Plus, the explode option lets you create new space between two slices.
The code below helps ass offset among different slices:
import matplotlib.pyplot as plt import seaborn as sns data = [35, 21, 29, 39, 11] explode = [0.3,0.02,0.02,0.02,0.02] labels = ['slice 1', 'slice 2', 'slice 3', 'slice 4', 'slice 5'] colors = sns.color_palette('bright') plt.pie(data, labels=labels,colors = colors, autopct = '%0.0f%%', explode = explode) plt.show()
If you want to decorate your pie chart with some shadows, let’s use the shadow parameter to draw some under it. Set this parameter to True to accomplish the task.
A pie chart is a perfect tool to demonstrate the proportion of numerical data for various objects or elements. By representing the information in the form of slices, you can easily analyze and get the perfect result.
Check the article to see how to create a Seaborn pie chart.