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

Two Ways of rendering in React Examples

Two Ways of rendering in React Examples 1 - Render twice HTML < div id = "p1" ></ div > < div id = "p2" ></ div > CSS . person { display : inline - block ; margin : 10px ; border : 1px solid # eee ; box - shadow : 0 2px 2px # ccc ; } JAVASCRIPT (Babel) function Person ( props ) { return ( < div className = ' person "> < h1 >{ props . name }</ h1 > < p > Age :{ props . age }</ p > </ div > ); } ReactDOM . render (< Person name = "VK" age = "29" />, document . querySselector ( '#p1' )); ReactDOM . render (< Person name = "HK" age = "28" />, document . querySselector ( '#p2' )); 2 Render all at once HTML < div id = "app" ></ div > CSS . person { display : inline - block ; margin : 1...

Python - Mathematical Functions - Hexadecimal, Binary, Exponential, Absolute, Round

🐍 Some mathematical functions are in-built in python. Here are some of them. Convert Decimal to Hexadecimal hex(246) Output: '0xf6' hex(512) Output: '0x200' 0x is prefix to all hexadecimal numbers in output. The number after 0x is the hexadecimal value. Convert Decimal to Binary bin(1234) Output: '0b10011010010' bin(246) Output: '0b11110110' bin(512) Output: '0b1000000000' Exponential Function pow() function is similar to num1**num2, but it comes useful when you want to execute (num1**num2)%num3. Passing three arguments to pow(), does this. pow(3,4) Output: 81 This works just like 3**4 pow(3,4,5) Output: 1 But this, works equivalent to (3**4)%5. Either of the two ways can be used. At this point I feel (3**4)%5 is better, as anybody reading the code will understand what is being done immediately. While with pow(2,3,4), the person may have to refer to documentation to understand how this function works. Absolute Value The function abs() is used ...

Binary Tree Traversal in Python - In Order, Pre Order, Post Order

Binary Tree Traversal in Python - In Order, Pre Order, Post Order Run the code here to see output:  https://repl.it/@VinitKhandelwal/iterative-traversal-of-binary-tree # Node Class to Create Object of Nodes class Node (): def __init__ ( self , value , left= None , right= None ): self .value = value self .left = left self .right = right # Creation of a Binary Tree class BinaryTree (): def __init__ ( self , mylist ): self .mylist = mylist self .root=Node ( None ) for value in self .mylist : self .curr= self .root self ._add ( value ) def _add ( self , value ): if self .curr.value == None : self .curr.value = value else : if self .curr.value > value : if self .curr.left == None : self .curr.left = Node ( value ) else : self .curr = self .curr.left self ._add ( value ) else : if self...