In the previous post, we learnt how to draw different types of plots using the matplotlib module.

In this post, we will see basic plotting using the seaborn module. Seaborn is widely used for more stylish less code-oriented plots. Seaborn actually uses matplotlib for its underlying functionalities.

Let’s install the module using pip install seaborn and import it as follows:

import seaborn as sns

Here, we will use some basic datasets for plotting that seaborn module already includes in its library. We can check the datasets using the following command.

sns.get_dataset_names()

now, we will see the following output that includes the dataset names.

['anagrams',
 'anscombe',
 'attention',
 'brain_networks',
 'car_crashes',
 'diamonds',
 'dots',
 'exercise',
 'flights',
 'fmri',
 'gammas',
 'geyser',
 'iris',
 'mpg',
 'penguins',
 'planets',
 'taxis',
 'tips',
 'titanic']

We can load a particular dataset using seaborn.load_dataset(<dataset_name>). You can check first few rows using the dataframe_obj.head() method.

data = sns.load_dataset('titanic')
data.head()

For additional stat, use dataframe_obj.describe() method.

data.describe()

Here, I provide sample examples of different sort of plots. Feel free to play around those. You can check the code outputs in this Google Colab.

Usual Plots

Count Plot

sns.countplot(x='sex',data=data)

Bar Plot

sns.barplot(x='sex',y='total_bill',data=data)
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(12,8))
sns.barplot(x='sex',y='total_bill',data=data, ax=ax)

Box Plot

sns.boxplot(x="day", y="total_bill", data=data, palette='rainbow')
sns.boxplot(data=data,palette='rainbow',orient='h')

Factor Plot

# factorplot has been changed to catplot
sns.catplot(x='sex',y='total_bill',data=data,kind='bar')  
sns.factorplot(x='sex',y='total_bill',data=data,kind='bar')

Linear Regression Plot

sns.lmplot(x='total_bill',y='tip',data=tips)
sns.lmplot(x='total_bill',y='tip',data=tips,hue='sex')

Distribution Plots

Dist Plot

sns.distplot(data['total_bill'])
sns.distplot(data['total_bill'],kde=False,bins=30)

Joint Plot

sns.jointplot(x='total_bill',y='tip',data=data,kind='scatter')
sns.jointplot(x='total_bill',y='size',data=data,kind='scatter')
sns.jointplot(x='total_bill',y='tip',data=data,kind='hex')
sns.jointplot(x='total_bill',y='tip',data=data,kind='reg')

Pair Plot

# plots for entire dataframe
sns.pairplot(data)
sns.pairplot(data,hue='sex',palette='coolwarm')

Matrix Plots

Heatmap of Correlation Plot

# Matrix form for correlation data
data.corr()
sns.heatmap(data.corr())
sns.heatmap(data.corr(),cmap='coolwarm',annot=True)
cor = flights.pivot_table(values='passengers',index='month',columns='year')
sns.heatmap(cor)
sns.heatmap(cor,cmap='magma',linecolor='white',linewidths=1)

Cluster Map

sns.clustermap(cor)
sns.clustermap(cor,cmap='coolwarm',standard_scale=1)

Once again, feel free to check the code outputs in this Google Colab.

Have a good day! Cheers!!!

For accessing all data science in python related posts, check this post:

Collection of Data Science in Python Posts in my Blog.

Leave a comment