Skip to main content

Matplotlib and Seaborn for Data Visualization - Python

Examples to learn Matplotlib and Seaborn for Data Visualization.

Install Numpy, Matplotlib, and Seaborn with the following commands on Terminal/Command Prompt

  • pip install numpy OR conda install numpy
  • pip install matplotlib OR conda install matplotlib
  • pip install seaborn OR conda install seaborn 

Run the following in Jupyter Notebook

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
x = np.random.rand(50)
x.sort()
print(x)
Random Dataset
y = x**2
print(y)
x**2


Plot with red line

plt.plot(x, y, 'r-')
plt.show()

Plot with dotted red line

plt.plot(x, y,'r--')
plt.show()
dotted plot

Plot with dots

plt.plot(x, y,'r.')
plt.show()
scattered plot

Plot with '+' sign

plt.plot(x, y,'r+')
plt.show()
scattered + plot

Plot with labels of x and y and title

plt.plot(x, y, 'r.')
plt.xlabel('Number')
plt.ylabel('Square')
plt.title('y = x**2')
labels and titles

Plot two or more using subplot

plt.subplot(1,2,1)
plt.plot(x,y,'r,')
plt.subplot(1,2,2)
plt.plot(y,x,'b,')
subplots

Plot using 'figure'

fig = plt.figure()
a = fig.add_axes([0,0,1,1])
a.plot(x,y,'r.')
a.set_xlabel('X Label')
a.set_ylabel('Y Label')
a.set_title('Title')
figure

Plot second inside first using 'figure'

fig = plt.figure()
axes1 = fig.add_axes([0,0,1,1])
axes2 = fig.add_axes([0.1,0.6,0.4,0.3])
axes1.plot(x,y)
axes1.set_title('Larger Plot')
axes2.plot(x,y**2)
axes2.set_title('Smaller Plot')
figure inside figure

Plot side by side

fig, axes = plt.subplots(nrows=1,ncols=2)
axes[0].plot(x,y)
axes[0].set_title('Title 1')
axes[1].plot(y,x)
axes[1].set_title('Title 2')
plt.tight_layout()
subplots with labels and titles

Set custom size of chart

fig = plt.figure(figsize=(8,2))
ax = fig.add_axes([0,0,1,1])
ax.plot(x,y)
figure dimensions

Multiple charts with custom sizes

fig, ax = plt.subplots(nrows=2,ncols=1,figsize=(8,4))
ax[0].plot(x,y)
ax[1].plot(x,y**2)
plt.tight_layout()
tight layout

Save chart in image file

fig.savefig('my_picture.png', dpi=300)
save png

Two lines in one chart

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.plot(x, y, 'r--',label="x=y")
ax.plot(x, y**2, 'g-.', label="x=y**2")
ax.legend(loc=(0.02,0.85))
two plots in one

A part of the chart

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.plot(x,y,color="purple", linewidth=3, alpha=0.5, linestyle='steps', marker="s", markersize=20, markerfacecolor="yellow", markeredgewidth=3, markeredgecolor="green")
ax.set_xlim([0.3,0.7])
ax.set_ylim([0.3,0.7])
part of a plot

Scattered Chart

plt.scatter(x,y)
scattered chart

Histogram Chart

plt.hist(x)
histogram

Box Chart

plt.boxplot([x,y],vert=True, patch_artist=True)
box chart

SEABORN

import seaborn as sns

Create Dataset using sample provided by Seaborn

tips = sns.load_dataset('tips')
tips.head()
tips dataset

View structure of Dataset

tips.info()
.info()



Dist Plot without KDE


sns.distplot(tips['total_bill'], kde=False, bins=30)
distplot with false kde

Joint Plot

sns.jointplot(x='total_bill',y='tip',data=tips)
joint plot

Joint Plot Hexagonal

sns.jointplot(x='total_bill',y='tip',data=tips,kind='hex')
hexagonal joint plot

Joint Plot Regression

sns.jointplot(x='total_bill',y='tip',data=tips,kind='reg')
joint plot regression

Joint Plot KDE

sns.jointplot(x='total_bill',y='tip',data=tips,kind='kde')
joint plot kde

Pair Plot

sns.pairplot(tips)
Pair Plot

Pair Plot with hue

sns.pairplot(tips, hue='sex', palette='cool')
Pair Plot Hue

Rug Plot

sns.rugplot(tips['total_bill'])
Rug Plot

Dist Plot

sns.distplot(tips['total_bill'])
Dist Plot

Create new Dataset

from scipy import stats
dataset = np.random.randn(1000)
sns.rugplot(dataset)
Setup for calculating KDE

Setup for calculating KDE

x_min = dataset.min() - 2
x_max = dataset.max() + 2
x_axis = np.linspace(x_min,x_max,100)
bandwidth = ((4*dataset.std()**5)/(3*len(dataset)))**.2
kernel_list = []
for datapoint in dataset:
    kernel = stats.norm(datapoint, bandwidth).pdf(x_axis)
    kernel_list.append(kernel)
    
    kernel = kernel/kernel.max()
    kernel = kernel*0.4
    plt.plot(x_axis, kernel, color="grey", alpha=0.5)
    
plt.ylim(0,1)
Individual KDEs

Calculating KDE

sum_of_kde = np.sum(kernel_list, axis=0)
fig = plt.plot(x_axis, sum_of_kde, color="indianred")
sns.rugplot(dataset, c="indianred")
plt.yticks([])
plt.suptitle("Sum of the Basis Functions")
Sum of KDEs

Dist Plot

sns.distplot(dataset)
Dist Plot with KDE

Bar Plot

sns.barplot(x='sex', y='total_bill', data=tips, estimator=np.std)
Bar Plot

Count Plot

sns.countplot(x='sex', data=tips)
Count Plot

Box Plot

sns.boxplot(x='day', y='total_bill', data=tips)
Box Plot

Box Plot with hue

sns.boxplot(x='day', y='total_bill', data=tips, hue='smoker')
Box Plot Hue

Violin Plot

sns.violinplot(x='day', y='total_bill', data=tips)
Violin Plot

Violin Plot with hue and Split

sns.violinplot(x='day', y='total_bill', data=tips, hue='sex', split=True)
Violin Plot Split

Strip Plot

sns.stripplot(x='day', y='total_bill', data=tips, jitter=True, hue='sex', split=True)
Strip Plot

Swarm Plot

sns.swarmplot(x='day', y='total_bill', data=tips)
Swarm Plot

Violing Plot with Swarm Plot

sns.violinplot(x='day', y='total_bill', data=tips)
sns.swarmplot(x='day', y='total_bill', data=tips, color='black')
Violin and Swarm Plot

Factor Plot - Bar

sns.factorplot(x='day', y='total_bill', data=tips, kind='bar')
Facto Plot - Bar

Factor Plot - Violin

sns.factorplot(x='day', y='total_bill', data=tips, kind='violin')
Factor Plot Violin

Use 'flights' sample dataset provided by Seaborn

flights = sns.load_dataset('flights')

View tips dataset

tips.head()
View Tips Dataset Head

View flights dataset

flights.head()
View Flights Dataset Head

Create Correlation of 'tips' dataset

tc = tips.corr()

tc
Correlation

Heat Map

sns.heatmap(tc, annot=True, cmap='coolwarm')
Heat Map

Create Pivot Table of 'flights' dataset

fp = flights.pivot_table(index='month', columns='year', values='passengers')

fp
Pivot table

Heat Map with line arguments

sns.heatmap(fp, cmap='magma', linecolor='white', linewidth=0.1)
Heat map with Line arguments

Cluster Map

sns.clustermap(fp, cmap='coolwarm')
Cluster Map

Cluster Map with Standard Scale

sns.clustermap(fp, cmap='coolwarm', standard_scale=1)
Cluster Map with Standard Scale

Load New Sample Dataset named 'iris'

iris = sns.load_dataset('iris')
iris.head()
Dataset iris

Get unique list of species from iris dataset

iris['species'].unique()
dataset column species

Pair Plot Iris

sns.pairplot(iris)
Pair Plot iris

Pair Grid Iris

g = sns.PairGrid(iris)
g.map(plt.scatter)
Pair Grid Iris

Pair Grid with Upper and Lower

g = sns.PairGrid(iris)
g.map_diag(sns.distplot)
g.map_upper(plt.scatter)
g.map_lower(sns.kdeplot)
Pair Grid with Upper and Lower

FacetGrid - Dist Plot

g = sns.FacetGrid(data=tips, col='time', row='smoker')
g.map(sns.distplot, 'total_bill')
facet grid dist plot

FacetGrid - Scatter Plot

g = sns.FacetGrid(data=tips, col='time', row='smoker')
g.map(plt.scatter, 'total_bill', 'tip')
facetgrid scatter plot

Seaborn LM Plot

sns.lmplot(x='total_bill', y='tip', data=tips)
seaborn lm plot

Seaborn LM Plot with hue

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

Seaborn LM Plot with markers and sizing the markers

sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', markers=['o','v'], scatter_kws={'s':100})
Seaborn lm plot with markers and sizing

Seaborn LM Plot with Column

sns.lmplot(x='total_bill', y='tip', data=tips, col='sex')
seaborn lm with column

Seaborn LM Plot with Column and Rows

sns.lmplot(x='total_bill', y='tip', data=tips, col='sex', row='time')
seaborn lm plot with column and rows

Seaborn LM Plot with Column, Row, and Hue

sns.lmplot(x='total_bill', y='tip', data=tips, col='day', row='time', hue='sex')
]seaborn lm plot with column, row and hue

Seaborn LM Plot with Column and Hue

sns.lmplot(x='total_bill', y='tip', data=tips, col='day', hue='sex')
seaborn lm plot with column and hue

Seaborn LM Plot with Aspect and Size

sns.lmplot(x='total_bill', y='tip', data=tips, col='day', hue='sex', aspect=0.6, size=8)
seaborn aspect and size

Seaborn - Set Style

sns.set_style('whitegrid')
sns.countplot(x='sex', data=tips)
seaborn set style

Seaborn Despine

sns.countplot(x='sex', data=tips)
sns.despine(left=True, bottom=True)
seaborn despine

Seaborn Context

sns.set_context('poster')
sns.countplot(x='sex', data=tips)
seaborn context

Seaborn - Context with Font Scale

sns.set_context('notebook', font_scale=2)
sns.countplot(x='sex', data=tips)
seaborn context font scale

Seaborn - Palette

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


Comments

Popular posts from this blog

Python - List - Append, Count, Extend, Index, Insert, Pop, Remove, Reverse, Sort

🐍 Advance List List is widely used and it's functionalities are heavily useful. Append Adds one element at the end of the list. Syntax list1.append(value) Input l1 = [1, 2, 3] l1.append(4) l1 Output [1, 2, 3, 4] append can be used to add any datatype in a list. It can even add list inside list. Caution: Append does not return anything. It just appends the list. Count .count(value) counts the number of occurrences of an element in the list. Syntax list1.count(value) Input l1 = [1, 2, 3, 4, 3] l1.count(3) Output 2 It returns 0 if the value is not found in the list. Extend .count(value) counts the number of occurrences of an element in the list. Syntax list1.extend(list) Input l1 = [1, 2, 3] l1.extend([4, 5]) Output [1, 2, 3, 4, 5] If we use append, entire list will be added to the first list like one element. Extend, i nstead of considering a list as one element, it joins the two lists one after other. Append works in the following way. Input l1 = [1, 2, 3] l1.append([4, 5]) Output...

Difference between .exec() and .execPopulate() in Mongoose?

Here I answer what is the difference between .exec() and .execPopulate() in Mongoose? .exec() is used with a query while .execPopulate() is used with a document Syntax for .exec() is as follows: Model.query() . populate ( 'field' ) . exec () // returns promise . then ( function ( document ) { console . log ( document ); }); Syntax for .execPopulate() is as follows: fetchedDocument . populate ( 'field' ) . execPopulate () // returns promise . then ( function ( document ) { console . log ( document ); }); When working with individual document use .execPopulate(), for model query use .exec(). Both returns a promise. One can do without .exec() or .execPopulate() but then has to pass a callback in populate.

Python Class to Calculate Distance and Slope of a Line with Coordinates as Input

🐍  Can be run on Jupyter Notebook #CLASS DESIGNED TO CREATE OBJECTS THAT TAKES COORDINATES AND CALCULATES DISTANCE AND SLOPE class Line:     def __init__(self,coor1,coor2):         self.coor1=coor1         self.coor2=coor2 #FUNCTION CALCULATES DISTANCE     def distance(self):         return ((self.coor2[0]-self.coor1[0])**2+(self.coor2[1]-self.coor1[1])**2)**0.5 #FUNCTION CALCULATES SLOPE         def slope(self):         return (self.coor2[1]-self.coor1[1])/(self.coor2[0]-self.coor1[0]) #DEFINING COORDINATES coordinate1 = (3,2) coordinate2 = (8,10) #CREATING OBJECT OF LINE CLASS li = Line(coordinate1,coordinate2) #CALLING DISTANCE FUNCTION li.distance() #CALLING SLOPE FUNCTION li.slope()