Pie chart in Python using Seaborn
Fellow coders, in this tutorial we are going to plot a Pie chart in Python with the help of Seaborn and Matplotlib. We will learn about data visualization and what is the benefit of data visualization in the field of Data Science. So, let us begin with what is data visualization.
Data visualization is one of the pillars of data science where we graphically visualize the data in order to better understand it and explain it to others. There is a huge amount of data present in any given dataset and to make a sense of all that data, we use Data Visualization.
What is Seaborn:
Seaborn is a Python data visualization library that is very widely used because we can create beautiful charts with a lot of customization options available to us. Seaborn is based on Matplotlib. We can visualize univariate and bivariate distributions with the help of Seaborn.
How to make a pie chart in Python using Seaborn
We will be writing our code in Jupyter Notebook in this tutorial. If you do not have seaborn installed, you can do it by:
!pip install seaborn
Let’s first import our weapons:
import seaborn as sb import matplotlib.pyplot as plt import numpy as np import pandas as pd %matplotlib inline
Moving on with the code:
#creating a one dimentional numpy array arr1 = np.array([23, 45, 65, 32, 67]) #creating a two-dimentional numpy array arr2 = np.array([[2010, 2011, 2012, 2013], [5000, 6000, 7000, 8000]])
We could simply perform a “distplot()” operation on the arrays we just created to check whether our code is working fine or not.
The output of the above code is:
Now, let us proceed further by creating some beautiful Pie charts:
cols = ['c', 'b', 'r', 'k'] #we can add explode parameter to pop out the different sections of our pie chart #remove explode parameter for a normal pie chart plt.pie(array, labels = array, colors = cols, startangle = 90, shadow = True, explode = (0.1, 0.1, 0.1, 0.1)) plt.show()
The output of the code above is shown below:
Now let us create a more customized and beautiful Pie chart on our second array:
plt.rcParams['text.color'] = '#000000' plt.rcParams['axes.labelcolor']= '#909090' plt.rcParams['xtick.color'] = '#909090' plt.rcParams['ytick.color'] = '#909090' plt.rcParams['font.size']=11 color_palette_list = ['#009ACD', '#ADD8E6', '#63D1F4', '#0EBFE9', '#C1F0F6', '#0099CC'] plt.pie(arr, labels=arr, startangle=90, colors=color_palette_list, autopct='%1.0f%%', explode=(0,0,0,0,0.1))
The output of the code above is shown in the image below: