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

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.

Machine Learning — Supervised, Unsupervised, and Reinforcement — Explanation with Example

🤖 Let's take an example of machine learning and see how it can be performed in three different ways — Supervised, Unsupervised, and Reinforcement. We want a program to be able to identify apple in pictures Supervised Learning You will create or use a model that takes a set of pictures of apple and it analyses the commonality in those pictures. Now when you show a new picture to the program, it will identify whether it has an apple or not. It can also provide details on how confident is the program about it. Unsupervised Learning In this method, you create or use a model that goes through some images and tries to group them as per the commonalities it observes such as color, shape, size, partern, etc. And now you can go through the groups and inform the program what to call them. So, you can inform the program about the group that is apple mostly. Next time you show a picture, it can tell if an apple is there or not. Reinforcement Learning Here the model you create or...

269. Alien Dictionary

  Solution This article assumes you already have some confidence with  graph algorithms , such as  breadth-first search  and  depth-first searching . If you're familiar with those, but not with  topological sort  (the topic tag for this problem), don't panic, as you should still be able to make sense of it. It is one of the many more advanced algorithms that keen programmers tend to "invent" themselves before realizing it's already a widely known and used algorithm. There are a couple of approaches to topological sort;  Kahn's Algorithm  and DFS. A few things to keep in mind: The letters  within a word  don't tell us anything about the relative order. For example, the presence of the word  kitten  in the list does  not  tell us that the letter  k  is before the letter  i . The input can contain words followed by their prefix, for example,  abcd  and then  ab . These cases will never ...