Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Unit - II Visualization Using Matplotlib

This document provides an overview of data visualization using Matplotlib, covering topics such as importing the library, creating various plot types (line, scatter, histograms), and customizing plots with colors, legends, and annotations. It also discusses error visualization, including the use of error bars, and introduces three-dimensional plotting techniques. The document emphasizes the versatility of Matplotlib across different platforms and its integration with data manipulation libraries like NumPy and Pandas.

Uploaded by

mk4997320
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit - II Visualization Using Matplotlib

This document provides an overview of data visualization using Matplotlib, covering topics such as importing the library, creating various plot types (line, scatter, histograms), and customizing plots with colors, legends, and annotations. It also discusses error visualization, including the use of error bars, and introduces three-dimensional plotting techniques. The document emphasizes the versatility of Matplotlib across different platforms and its integration with data manipulation libraries like NumPy and Pandas.

Uploaded by

mk4997320
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 86

UNIT - II

VISUALIZATION
USING
MATPLOTLIB
Importing Matplotlib – Simple line plots – Simple scatter
plots –

visualizing errors – density and contour plots – Histograms –

legends – colors – subplots – text and annotation –

customization – three dimensional plotting - Geographic

Data with Basemap - Visualization with Seaborn.


T LIB
T PLO
M A
??
M AT P LO T L I B

Matplotlib is a multiplatform data


visualization library built on Numpy
arrays, and designed to work with SciPy
stack.
One of Matplotlib’s most important
features is its ability to play with many
operating systems and graphics
backends.
M AT P LO T L I B

Matplotlib supports dozens of


backends and output types, which means
you can count on it to work regardless of
which operating system you are using or
which output format you wish.
This cross-platform (Windows/mac),
everything-to-everyone (Any type of plots)
approach has been one of the great
G E N E RA L
M AT P LO T L I B TIPS

Before we dive into the details of


creating visualizations with Matplotlib,
there are a few useful things you should
know about using the packages.
or ti n
Im p
T L IB
gT PL O
M A
??
IMPORTIN G
M AT P LO T L I B

Shorthands for Matplotlib


imports import matplotlib
as mpl import
matplotlib.pyplot as plt
e t ti n
S
g y l e s ?
s t
?
SETTING S T Y L E S
We will use the plt.style directive
to choose appropriate styles for our
figures.
We will use classic style, which ensures
the plots we create use the classic
Matplotlib style.
SHOW() O R N O SHOW()? H O W T O
DISPL A Y YO U R P LO T S

1.Plotting from a script

2.Plotting from an IPython Shell

3.Plotting from an IPython Notebook


(Jupyter Notebook)
1 . g
l ototi
m n
P fr
a r i pt ?
s c
1 . P LO TT I N G F R O M A S C R I P T

If you are using Matplotlib from


within the script, the function
plt.show() is used. plt.show() starts an
event loop, looks for all currently
active figure objects, and opens
one or more interactive windows that
1 . P LO TT I N G F R O M A S C R I P T

import matplotlib.pyplot
as plt import numpy as np
x = np.linspace(0, 10,
100) plt.plot(x,
np.sin(x))
plt.plot(x,
np. l i n s p a c e ( 0 , 10 , creates
100 ) an array o f 100
ev en l y space
d
v a l u e s from 0 t o 10 .
np. sin( x) and np. cos( x)
feul e
nmc tei onnt sin ct h
ael c array.
ulat e the sine
and c o s i n e v a l u e s o f e a c h

plot() d i s ptl a
plt. show() ion
y s itshues e d
plot. to
tt i n g
. P l o
2
r
f aomn
y t h
eol n
l ?
P
I S h
?
e n ce
i ff e r i p t
D e e n
n s c r
e
bpy twt ho
n dy t h o n
aIP ?
he ll?
S
DIFFERENC E B E T W E E N P Y T H O N
S C R I P T AND I PY T H O N SHELL
A script contains a list of commands to
execute in order.
It runs from start to finish and display some
output. On the contrary,with IPython, you
generally write one command at a
time and you get the results
instantly.
This is a completely different way of
2 . P LO TT I N G F R O M A N I PY T H O N
SHELL
IPython is built to work well
with Matplotlib if you specify
Matplotlib mode.
To enable this mode, you can use
%matplotlib command after starting ipython:
%matplotlib
import matplotlib.pyplot as plt
Plot command will cause a figure window to
open and further commands can be run to
2 . P LO TT I N G F R O M A N I PY T H O N
SHELL
Some changes or modifications will not dra
w
automatically: to force an update, use
plt.draw(). plt.show() is not required.
tt i n g
. P l o
3
r
f aomn
y t h
boon
o k
I
NP o te
?
3 . P LO TT I N G F R O M A N I PY T H O N
N O T E BO O K
IPython Notebook is a browser based
interactive data analysis tool that can
combine narrative code, graphics, HTML
elements and much more i nto a single
document.
Plotting can be done with %matplotlib
command as similar to IPython shell.
t i o n s
O p
fo r d i n g
m b e d n
e i c
cst l y i
ra p ihr e
g d b oo k
thno e t e
%matplotlib notebook - will lead
to interactive plots

embedded within the notebook

%matplotlib inline - will lead to static images

(non interative plots) of your plot

embedded in the notebook


a v i n g
S e s
fi gut or
fi l e ??
g ( ' fi l
av e fi
p l t . s . e x t ' )
n a m e
e
D u a
f a c e s
tle r
in t l ib
na
iM t p l o
??
Dual interfaces in
Matplotlib
MATLAB style interface

Object Oriented Interface


m p l e
S i
n el ot s ?
li p
?
Simple Line Plots
#Importing the required libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#Reading and pre-processing the data
# Load the dataset into a Pandas DataFrame
df = pd.read_csv("HistoricalPrices.csv")

# Rename the column to remove an additional space


df = df.rename(columns = {' Open': 'Open', ' High': 'High', ' Low': 'Low', ' Close':
'Close'})

# Convert the date column to datetime


df['Date'] = pd.to_datetime(df['Date'])

# Sort the dataset in the ascending order of date


df = df.sort_values(by = 'Date')
Creating a Basic Line Plot in
Matplotlib
# Extract the date and close price columns
dates = df['Date']
closing_price = df['Close']

# Create a line plot


plt.plot(dates, closing_price)

# Show the plot


plt.show()
Simple Scatter Plots

% matplotlibinline
import matplotlib. pyplot
as plt
plt. style. use(' seaborn-
whitegrid')
import numpy as np
x = np.linspace(0, 10, 30)
y = np.sin(x)
plt.plot(x, y, 'o',
color='black');
. p l o t
p l t
. p l o t ?
s
vax .
?
import
matplotlib. pyplot

as plt
f i g = p l t . f i g u re ( )
i m p o r tn u m p ya s n p
x = np. l i n s p a c e ( 0 ,
10 , 1 0 0 0 )
plt. plot( x, np.
s i n ( x));
f ig = plt. f
igure() a x = p l t .
axes()

x = np. l i n s p a c e ( 0 ,
10 , 1 0 0 0 )
a x . p l o t ( x , n p . s i n ( x));

plt.plot() is a part of pyplot


interface
Adjusting the Plot: Line
Colors
and Styles
The plt.plot() function takes additional arguments
To adjust the color, you can use the color keyword, which
accepts a string argument representing virtually any
imaginable color.
plt.plot(x, np.sin(x - 0), # specify
color='blue') nameplt.plot(x, color by #
np.sin(x - 1), color='g') plt.plot(x, short color
np.sin(x - 2),
plt.plot(x, color='0.75')
np.sin(x - 3), color='#FFDD44') # Hex code code
(RRGGBB from 00 to FF) # Grayscale
between
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB tuple, values 0
0 to 1 & 1
If no color is
s p e c i fi e d , M a t p l o t l i b
will automatically
c y c l e through a set
of default c o l o r s for
multiple l
ines.
Adjusting the Plot: Line
Styles
The line style can be adjusted using the linestyle
keyword. plt.plot(x, x + 0, linestyle='solid')
plt.plot(x, x + 1,
linestyle='dashed') plt.plot(x, x
+ 2, linestyle='dashdot')
plt.plot(x, x + 3,
linestyle='dotted');
# For short, you can use the following
codes: plt.plot(x, x + 4, linestyle='-') #
solid plt.plot(x, x + 5, linestyle='--') #
Adjusting the Plot: Line
Colors
The linestyle and color codes and Styles
can be combined into a
single non- keyword
argument to the plt.plot()
function:
plt.plot(x, x + 0, '-g') # solid
green plt.plot(x, x + 1, '--c') #
dashed cyan plt.plot(x, x + 2,
'-.k') # dashdot black
plt.plot(x, x + 3, ':r'); # dotted
Adjusting the Plot: Axes
Limits
To adjust axis limits
-
use the plt.xlim()
and plt.ylim()
methods plt.plot(x,
np.sin(x))
plt.xlim(-1, 11)
Adjusting the Plot: Axes
Limits
plt.plot(x, Output???????
np.sin(x)) ?
plt.xlim(10, 0)

plt.ylim(1.2, -1.2);
Adjusting the Plot: Axes
Limits

plt.plot(x, np.sin(x)) Output???????


plt.axis([-1, 11, -1.5, ?
1.5]);
Adjusting the Plot: Axes
Limits
plt.plot(x, np.sin(x))

plt.axis('tight');

plt.axis('tight') - adjusts

the axis limits


Labeling Plots

plt.plot(x, np.sin(x))

plt.title("A Sine

Curve")

plt.xlabel("x")

plt.ylabel("sin(x)");
plt.plot(x, np.sin(x),
'-g',
label='sin(x)')
plt.plot(x,
np.cos(x), ':b',
label='cos(x)')
plt.axis('equal')
plt.legend();
plt.legend()
Aside: Matplotlib
Gotchas
“Matplotlib Gotchas" refer to common issues or
pitfalls that users might encounter when working with
Matplotlib. plt.xlabel() → ax.set_xlabel()
plt.ylabel() →

ax.set_ylabel() plt.xlim()
→ ax.set_xlim()
plt.ylim() →

ax.set_ylim() plt.title() →

ax.set_title()
ax = plt.axes()
ax.plot(x,
np.sin(x))
ax.set(xlim=(0,
10),
ylim=(-2,
2),xlabel='x',
ylabel='sin(x)', title='A
Visualizing
Errors
• Any statistical data inevitably contains errors
• An error in statistics is simply a difference between the
measured value and the actual value
• Plotting errors on your charts is a way to convey more
information.
• A graphical representation of errors or uncertainties in the
measurement is called an error bar.
• Error bars are included in Matplotlib line plots and graphs.
Visualizing

Errors
It's a line (a cap line, sometimes) drawn from the data
point.
• The line's length shows us how precise the
measurement is.
• A short error bar means that the values are
concentrated, and the data is reliable.
• A long error bar indicates that the values are spread
out, and the data is likely to contain errors.
• In visualization, showing these errors effectively can
make a plot convey much more complete information.
Adding Error bar in
Matplotlib
plt.errorbar(x, y, yerr = 2, capsize=3)

x = The data of the X axis.


Y = The data of the Y axis.
yerr = The error value of the Y axis. Each point has its own error
value.
xerr = The error value of the X axis.
capsize = The size of the lower and upper lines of the error bar
%matplotlib inline
import matplotlib.pyplot
as plt
plt.style.use('seaborn-
whitegrid')
import numpy as np
x = np.linspace(0, 10,
50)
dy = 0.8
y = np.sin(x) + dy *
np.random.randn(50)
plt.errorbar(x, y,
yerr=dy, fmt='.k');
Basic
x = np.linspace(0, 10,
Errorbars
50): Creates an array of 50
values evenly spaced between 0 and 10.
dy = 0.8: Specifies the error or uncertainty in the y-
values.
y = np.sin(x) + dy * np.random.randn(50): Calculates
y-values using the sine function and adds random
noise with a standard deviation of dy.
plt.errorbar(x, y, yerr=dy, fmt='.k'): Plots the data
points (x, y) with error bars (yerr=dy) using black dots
Parameters of the
Errorbar
a) yerr - error value in each point.
b) linestyle, here it indicate that we will not plot a line.
c) fmt, is the type of marker, in this case is a point ("o") blue ("b").
d) Capsize - the size of the lower and upper lines of the error bar.
e) Ecolor - the color of the error bar. The default color is the marker
color.
n si t y
D e
n t o u
&Co
rPlo t s ?
?
DENSITY AND
C O N T O U R P LO T S
To three-dimensional data
display in
dimensions using two or shape
contours (outline
of something) or color-coded regions.
Three Matplotlib functions that can
be helpful for this task:
1.plot.contour - for contour plots
2.plot.contourf - for filled contour
plots
3.plot.imshow - showing images
VISUALIZING A
T H R E E DIMENSIONAL
z=f(x,y)
FU N C T I O N
A contour plot can be created with
the plt.contour function.
It takes three arguments: a grid of x
values, a grid of y values, and a grid of z
values.
The x and y values represent positions
on the plot, and the z values will be
VISUALIZING A
T H R E E DIMENSIONAL
FU N C T I O N
np.meshgrid function, plt.contour(X, Y,
which builds two- Z,
dimensional grids from colors='black')
one-dimensional arrays ;
x = np.linspace(0, 5,
50)
y = np.linspace(0, 5,
40) X, Y =
plt.contour(X, Y, Z, 20,
cmap='RdGy');

RdGy - Red-
Gray
Matplotli has a wide range of
b colormaps which you can easily
available browsein
IPython by doing a tab completion on the
,plt.cm module:
plt.cm.<TAB>
The spaces between the lines may be a
bit distracting.
We can change this by switchingto a
filled contour plot using the plt.contourf()
function
plt.contourf(X, Y, Z,
plt.colorbar 20, cmap='RdGy')
plt.colorbar();
() whic
command, h
automatical
creates an
ly
additional axis
with labeled
color information
for the plot.
t o g r a
H i s
, i n g s
Bmi n n ?
d n s i t y
aDn e
?
HISTOGRAMS,
BINNINGS
%matplotlib inline & DENSITY
import numpy as
np
import
matplotlib.pyplot as plt
plt.style.use('seaborn-
white')
data =
In the context of a histogram, "bins"
refer to the intervals into which the
data range is divided.
If you have a dataset of exam scores
ranging from 0 to 100, and you
choose 10 bins, the histogram will
group the scores into 10 intervals,
such as 0-10, 11-20, 21-30, and so on
HISTOGRAMS,
BINNINGS
& DENSITY
plt.hist(data, bins=30,
normed=True,alpha=0.5,
his
ttype='stepfilled',color=
'ste
TWO
DIMENSIONAL
HISTOGRAMS AND
We create histograms in one
BINNINGS
dimension by dividing the number-line
into bins, we can also create histograms
in two-dimensions by dividing points
among two-dimensional bins.
To plot a two-dimensional histogram is to
plt.hist2d(x, y,
bins=30,
cmap='Blues')
cb = plt.colorbar()
cb.set_label('counts
in bin')

A 2D histogram, also known


as a density heatmap
HEXAGONAL
BINNINGS
The two- plt.hexbin(x, y,
dimensional gridsize=30,
cmap='Blues')
histogram creates cb =
a tesselation of plt.colorbar(label='count
in bin')
squares across the
axes.
Another natural shape
for such a tesselation
is the regular
Legends
• A legend is an area describing the elements of
the graph.
• In the Matplotlib library, there’s a function
called legend() which is used to place a
legend on the axis.
• The legend function has an attribute called loc
which is used to denote the location of the
legend.
Legend
Legend
CUSTOMIZIN G
C O LO R B A R S
The simplest colorbar can
be created with the
plt.colorbar function.
The colormap can be
specified using the cmap
argument to theplotting
function that is
creating the visualization.
plt.imshow(I, cmap='gray');
C H O O S I N G T H E C O LO R M A P
Three different categories of colormaps:
1.Sequential colormaps: These are made up
of one continuous sequence of colors.
2.Divergent colormaps: These usually
contain two
distinct colors.
3.Qualitative colormaps: These mix colors
w r i t t
Ha n d
i gi t s ?
n
e D
?
S U B P LO T S

Subplots: groups of smaller


axes that can exist together
within a single figure.
These subplots might be insets, grids of
plots, or more complicated layouts.
G E O G R A P H I CD AT A
WITH
BASEMAP
One common type of visualization in data
science is that of geographic data.
Matplotlib's main tool for this type of
visualization is the Basemap toolkit, which
is one of several Matplotlib toolkits
which lives under the mpl_toolkits
G E O G R A P H I CD AT A
WITH
Installation of Basemap BASEMAP
$ conda install
basemap IMPORTING
LIBRARIES
%matplotlib
inline import
numpy as np
G E O G R A P H I CD AT A
WITH
plt.figure(figsize=(8, 8)) BASEMAP
m = Basemap(projection='ortho',
resolution=None, lat_0=50, lon_0=-
100)m.bluemarble(scale=0.5); projection='ortho'
specifies an orthographic
projection, which displays a globe-like plot.
t o po
E e ??
im a g
G E O G R A P H I CD AT A
WITH
BASEMAP
For example, we can use a different map projection,
zoom- in to North America and plot the location of
Seattle.
We'll use an etopo image (which shows
topographical features both on land and under the
ocean) as the map background
G E O G R A P H I CD AT A
WITH
fig = plt.figure(figsize=(8, 8)) BASEMAP
m =
Basemap(projection='lcc',
resolution=None,width=8E6,
height=8E6,lat_0=45, lon_0=-100,)
m.etopo(scale=0.5, alpha=0.5)
# Map (long, lat) to (x, y) for
plotting x, y = m(-122.3, 47.6)
plt.plot(x, y, 'ok',
markersize=5) plt.text(x, y, '
G E O G R A P H I CD AT A
WITH
BASEMAP
Basemap package implements dozen
several projections such

1.Map Projections
2.Cylindrical projections
3.Pseudo-cylindrical
projections
4.Perspective projections
5.Conic projections
DRAWIN G A M A P
B A C K G R O U N D
Physical boundaries and bodies of water
1. drawcoastlines(): Draw continental coast
lines 2. drawlsmask(): Draw a mask
between the land andsea, for use with
projecting images on
one or the other
3.drawmapboundary(): Drawthe
map boundary, including
the fill color for oceans.
4.drawrivers(): Draw rivers on the map
5.fillcontinents(): Fill the continents with a
given color; optionally fill lakes

You might also like