Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views12 pages

Unit 5 Part Two

The document discusses various methods for creating plots using Matplotlib, including plt.axes(), figure.add_axes(), and plt.subplots(). It also covers annotation features for labeling important points on graphs, customization of ticks, and the use of 3D plotting and Basemap for geographic data visualization. Additionally, it introduces Seaborn as a high-level interface for statistical graphics, emphasizing its ease of use with Pandas dataframes.

Uploaded by

laya rose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Unit 5 Part Two

The document discusses various methods for creating plots using Matplotlib, including plt.axes(), figure.add_axes(), and plt.subplots(). It also covers annotation features for labeling important points on graphs, customization of ticks, and the use of 3D plotting and Basemap for geographic data visualization. Additionally, it introduces Seaborn as a high-level interface for statistical graphics, emphasizing its ease of use with Pandas dataframes.

Uploaded by

laya rose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Foundations of Data Science (5- 26) Data Visualization

There are 3 different ways (at least) to create plots (called axes) in matplotlib. The y
are:plt.axes(), figure.add_axis() and plt.subplots()
plt.axes) : The most basic method of creating an axes is to use the plt.axes function. It
takes optional argument for figure coordinate system. These numbers represent [bottom
left, width, height] in the figure coordinate system, which ranges from 0 at the bottom left
of the figure to 1 at the top right of the figure.
Plot just one figure with (x.y) coordinates plt.plot(x, y).
By calling subplot(n,m,k), we subdidive the figure into n rows and m columns a specify
that plotting should be done on the subplot number k. Subplots are numbered row by row,
from left to right.
importmatplotlib.pyplotasplt
importnumpyasnP
frommathimportpi
plt.figure(figsize=(8,4))# set dimensions of the figure

x=np.linspace(0,2*pi,100)
foriinrange(1,7):
plt.subplot(2,3,i)# create subplots on a grid with 2 rows and 3 columns

plt.xticks()# set no ticks on x-axis

plt.yticks([)# set no ticks on y-axis

pit. plot(np.sin(x),np.cos(i'x))
plt.title('subplot +'(2,3,+str(i)+)
plt.show()
Output:
subplot(2,3,1) subplot(2,3,2) subplot(2,3,3)

subplot(2,3,4) subplot(2,3,5) subplot(2,3,6)

TECHNICAL PUBLICATIONS- an up-thrust for


knowledge
Sciennce (5-27) Data Visualization
Data
of
undations

Annotation
5.8 Text and
certain
h e n drawing
large and complex plots in Matplotlib, we need a way of labelling
of interest on the graph. To do so, Matplotlib provides us with the
Whe

portion
points of
or points
Aanotation" feature which allows us to plot arrows and text labcls on the graphs togi

them more meaning.

always with annotate().


four important parameters that you must
use
There are

defines the text label. Takes a string as a value.


a) text: This
point to. In other words, the place you
b) xy: The place where you want your arrowhead to
and y.
want to annotate. This is a tuple containing two values, x

coordinates for where you want to text to display.


) xytext: The
properties for the
which define various
d) arrowprops : A dictionary of key-value pairs
arrow, such as color, size and arrowhead type.

Example:
importmatplotlib.pyplot as plt

importnumpy as np

fig, ax =pt.subplots()

x =np.arange(0.0, 5.0, 0.01)

y =np.sin(2*np.pi *x)

# Annotation

ax.annotate('Local Max,
xy = (3.3, 1),

xytext =(3, 1.8),


dict(facecolor
='green,
=
arrowprops

shrink 0.05))

ax.set_ylim(-2, 2)
plt.plot(x, y)
plt.show()
PUBLICATIONS an up-thrust for knowladge
TECHNICAL
Foundations of Data Science (5-28) Data Visualization

Output
2.0- Local max

1.5

1.0

0.5

0.0

-0.5

-1.0

-1.5

-2.0
0 1 2 3 4 5

Example
importplotly.graph_objectsasgo
fig=go.Figure()

fig.add trace(go.Scatter(
x=[0,1,2,3,4,5,6,7,8],
y=[0,1,3,2,4,3,4,6,5]

fig.add_trace(go.Scatter(
x=[0,1,2,3,4,5,6,7,8],
y-10,4,5,1,2,2,3,4,2]

fig.add_annotation(x=2,y=5,
text="Text annotation with
arrow',
showarrow=True,
arrowhead=1)

fig.add_annotation(x=4,y=4,
TECHNICAL PUBLICATIONS an
up-thrust for knowledge
Data
Science (5-29) Data Visuaization
of
Endations

without arrow,
"Text annotation
ext=

s h o w a r r o W = F a l s e ,

yshift 10)

fig.update_layout(showlegend=False)

fig.show()

Output:

Text annotation with arrow

Text annotation without arrow

7 8
0 3 4

5.9 Customization
ticks separate each category. For value
A dck is a short line on an axis. For category axes,
exact point on an axis that
the axis label
AS, ticks mark the major divisions and show the
style the ax1is.
Cnes. Ticks are always the same color and line as

Matplotlib's default tick locators and


Tic
CkS the markers denoting data points
are
on axes.
situations. Position and
generally sufficient in many c o m m o n
Omatters are designed to be
to suit specif+c requirements.
labeof ticks can be explicitly mentioned

TECHNICAL PUBLICATIONS
an up-thrust for knowledge
Foundations of Data Science (5-30) Data Visualization

Fig. 5,9.1 shows ticks.

4000

3000

2000

1000

Ticks
0
88 91 94 97

Fig.5.9.1 : Ticks
Ticks come in two types: major and minor.

a) Major ticks separate the axis into major units. On category axes, themajor ticks are only
ticks available. On value axes, one major tick appears for every major axis division.

b) Minor ticks subdivide the major tick units. They can only appear on value axes. One
minor tick appears for every minor axis division.

By default, major ticks appear for value axes. xticks is a method, which can be used to get
or to set the current tick locations and the labels.

The following
program ereates a plot with both major and minor tick marks, customized to
be thicker and wider than the default, with the major tick marks point into and out of the
plot area.

importumpyasnp

importmatplotlib.pyplotasplt

# A selection of functions on nabcissa points for 0<= x < 1

m=100

rx=np.linspace(0,1,m,endpoint=False)

deftophat(rx):

TECHNICAL PUBLICATIONS -

an
up-thrust for knowledge
of D a t a
Science
(5- 31) Data Visualization
FoUndations

hat function: y = 1 for x <0.5, y=0 for x >= 0.5


Top

ry=np.ones(m)

rylrx>=0.5]=0

returnry

#A dictionary offunctions to choose from

ry=fhalf-sawtooth:lambdarx:x.copy(),
top-hat:tophat,

'sawtooth':lambdarx:2*np.abs(rx-0.5)}

#Repeat the chosen function nrep times

nrep=4

x=np.linspace(0,nrep,nrep*m,endpoint=False)
y=np.tile(ryl'top-hat'](rx),nrep)

fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x.y,k, lw=2)
T
Add a bit of aid visualization
padding around the plotted line to

ax.set_ylim(-0.1,1.1)
ax.set_xlim(x[0]-0.5,x[-1]+0.5)
Customize the tick marks and turn the grid on

ax.minorticks_on()
.ck params(which='major,length=10,width=2,direction=inout")
Cparams(which='minor, length=5,width=2,direction="in')
ax.grid(which="both')
plt.show()
TECHNICAL PUBLICATIONS-an up-thrust for knowledge
Foundations of Data Science
(5-32) Data Visualization

Output

1.0

0.8

0.6

0.4

0.2

0.0

O 1

5.10 Three Dimensional Plotting


Matplotlib is the most popular choice for data visualization. While initially developed for
has
plotting 2-D charts like histograms, bar charts, scatter plots, line plots, etc., Matplotlib
extended its capabilities to offer 3D plotting modules as well.

First import the library:

importmatplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3DD would

The first one is a standard import statement for plotting using matplotlib, whicnyo nabling
see for 2D plotting as well. The second import of the Axes3D class is required for ena

3D projections. It is, otherwise, not used anywhere else.

Create figure and axes

fig = plt.figure(figsize=(4,4))

ax fig.add subplot(111, projection=3d')

TECHNICAL PUBLICATIONS - an up-thrust for knowledge


Science (5- 33) Data Visualization
Data
of
dations
Runda

o u t p u t

1.0

0.8

0.6
0.4

0.2
0.0
1.0
0.8
.0 0.2 0.6
0.4 0.6 0.4
0.8 0.2
1.0 0.0

Example

fig plt.figure(figsize==(8,8))
ax=plt.axes(projection=3d')

ax.grid()

t=np.arange(0,10 np.pi.np.pi/50)
x=np.sin(t)

y=np.cos(t)

ax.plot3D(x,y,t)
ax.set_title(3D Parametric Plot)

#Set axes label

ax.set_xlabel('x,labelpad=20)
ax.set_ylabel('y',labelpad=20)
ax.set_zlabel('t,labelpad=20)

plt.show()

TECHNICAL
P U B L I C A T I O N S - an up-thrust for knowledge
Foundatons of Data Saens (5-34) Data Visualization

Output
3D Parametric plot

10
15 t

20
25
30

5.11 Geographic Data with Basemap


Basemap is a toolkitunder the Python visualization library Matplotlib. Its main functionis
to draw 2D maps. which are important for visualizing spatial data. Basemap itself does not
do any plotting. but provides the ability to transform coordinates into one of 25 different
map projections.
Maplotib can also be used to plot contours, images, vectors, lines or points in transformed
coordinates. Basemap includes the GSSH coastline dataset, as well as datasets from GMI
for rivers. states and national boundaries.
. T h e s e datasets can be used to plot coastlines, rivers and political boundaries on a map at

several different resolutions. Basemap uses the Geometry Engine-Open Source (GEOS)
library at the bottom to clip coastline and boundary features to the desired map projection
area. In addition. basemap provides the ability to read shapefiles.

Basemap cannot be installed using pip install basemap. If Anaconda is installed, you a
install basemap using canda install basemap.
.Example objects in basemap:
a) contour) : Draw contour lines.

b) contourf0 : Draw filled contours.

c)imshow): Draw an image.


d) pcolor): Draw a
pseudocolor plot.
e)pcolormesh): Draw a pseudocolor plot (faster version for regular meshes).
plot0: Draw lines and or markers.

TECHNICAL PUBLICATIONS- up-thrust


an for knowiedge
of Data
S c i e n cee
(5- 35) Data Visualization
Foundations

scatter(): Draw points with markers.

uiver(): Draw vectors.(draw vector map, 3D is surface map)


h )quiver():I

a barbs): Draw wind barbs (draw wind plume map)


drawgreatcircle): Draw a great circle (draws a great circle route)

Car example, if we wanted to show all the different types of endangered plants within a
regio we would use a base map showing roads, provincial and state boundaries,
Naterways and elevation. Onto this base map, we could add layers that show the location of
different categories of endangered plants. One added layer could be trees, another layer
could be mosses and lichens, another layer could be grasses.

Basemap basic usage

import warnings

warnings.filterwarnings(ignore')
frommpl_toolkits.basemap import Basemap
importmatplotlib.pyplot as plt

map Basemap()

map.drawcoastlines()
# plt.show)

plt.savefig(test.png)
Output

TECHNICAL PUBLICATIONS an up-thrust for knowledge


Foundations of Data Science (5- 36) Data Visualization

5.12 Visualization with Seaborn


Seaborm is a Python data visualization library based on Matplotlib. It provides a hi
level
interface for drawing attractive and informative statistical graphics. Seaborn is an.
open-
source Python library.
Seaborn helps you explore and understand your data. Its plotting functions operate on
dataframes and arrays containing whole datasets and internally perform the
y
semantic mapping and statistical aggregation to produce informative plots.
.Its dataset-oriented, declarative API. User should focus on what the different elements of

your plots mean. rather than on the details of how to draw them.

Keys features:
a) Seaborm is a statistical plotting library
b) It has beautiful default styles

c) It also is designed to work very well with Pandas dataframe objects.


Seaborn works easily with dataframes and the Pandas library. The graphs created can also
be customized easily.
Functionality thatseaborn offers:
a) A dataset-oriented API for examining relationships between multiple variables
b) Convenient views onto the overall structure of complex datasets
c) Specialized support for using categorical variables to show observations or aggregate
statistics
d) Options for visualizing univariate or bivariate distributions and for comparing them

between subsets of data


e) Automatic estimation and plotting of linear regression models for different kinds
dependent variables
lex
High-level abstractions for structuring multi-plot grids that let you easily buildcom
visualizations
g) Concise control over matplotlib figure styling with several built-in themes
h) Tools for choosing color palettes that faithfully reveal patterns in your daa.

Plot a
Scatter Plot in Seaborn:
importmatplotlib.pyplot as plt
importseaborn as sns

import pandas as pd

TECHNICAL PUBLICATIONS an
up-thrust for knowledge
of Data
Science
(5-37) Data Visualization
Foundations

df = pd.read_csv('worldHappiness2016.csv)

df, "Economy (GDP per Capita)", y


snsacatterplot
(data = x =
=
"Happiness Score")
plt.show()

Output

0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75


Economy (GDP per Capita)

5.12.1 Difference between Matplotlib and Seaborn

Parameters Matplotlib Seaborn


Use cases Matplotlib plots various Seaborn is the extended version of
graphs using Numpy and Matplotlib which uses Matplotlib along
Pandas. with Numpy and Pandas for plotting.
Syntax complity It uses comparatively It uses comparatively simple syntax.
complex and lengthy syntax.

Multiple figures We can open multiple figures Seaborn automates the creation of

at a time. multiple figures which sometimes leads

toout of memoryissue.
Flexibility It is highly customizable and Seaborn avoids a ton of boilerplate by

powerful. providing default themes which are


commonly used.

TECHNICAL PUBLICATIONS- an up-thrust for knowledge

You might also like