Unit 5
Unit 5
~ata Visualization
--Syllabus _____
I______
Importing Matplotlib - Line plots - 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.
Contents
5.1 Importing Matplotlib
5.2 Scatter Plots
5.3 Visualizing Errors
5.4 Density and Contour Plots
5.5 Histogram
5.6 Legend
5.7 Subplots
5.8 Text and Annotation
5.9 Customization
5.10 Three Dimensional Plotting
5.11 Geographic Data
with Basemap
5.12
Visualization with Seaborn
5 3
·1 Two Marks Questions with Answers
(5 - 1)
011111 Vis11011
'ir,f/011
bJ 5, 1 lmpt'rtin g Matplotl ib
• \ L:7:-'.,•:'.:~ ,!' .1 / ,' Hli\~ iil' t":\t ~ 1~,r th,' Pytlh,n prl,gmmm ing. l:mguag.~. It allows 10 niakc
~.. .::~>:: ;~.:~ in fr'\\ lmt'!' ,,t -.' ("fk. \ h,st ,,f thl' l,t h~r p~11hlm pll)II ing Iihrary arc hui Id 011 lop
. "'. . . . \ ~--17:': \,~!~~.
limi1;.,,i h' ~n l'lltput. hut it still prnvicks you wi th the means 10
• :)~:-..1 \°":!':.:.~1_-:.1:-: ,,11 i!' th;:- rr-.:-s.:-n~1t.i,'\n l°,f qnamitativ c intlmnatio n m a graphical form. In
, :. . ___~~ ,, -.,:-...::-. .5.:n-:1 \i !'n:1li~1ti1.'ln:- mm hrgl' and small datasets into visuals tlu1t are easier for
• 0 ,,'-.: L,:3 \l !'l:~i~Hi,'\ns :1n:- 1..·n:-~m'<l when 1..·ommunication, data science, and design collide.
-:).::.:-2 \"::";:.:.tli::'..lt0r.s . .11..'n ~ right 1.-,ffc'r key insights into complicat ed datasets in ways that are
• .-\ ~.!;-:-1 ::: . !''.ff{'ly .1 \l Sual represcnmti(in of muueric data. MatPlotLib supports a large
::1..:.-...:~:- t' J° ~1-;-:1 ..md ..·h:m rypes.
10
• .\ ~2:;~..,:Jit" is .! f'' rul..lr ~1111..,n p:1ck.;,1ge used ro build plots. Matplotlib can also be used
~ "-.:- : D ~:1.,1s ..md mirrurions .
• i.. !=:;- ? :<-'IS ..-ml t""c.' Cre3te-d in ~111\""'lll ,,ith Marplotlib's pyplot library. To build a line plot,
:::-s: ::-::.:,"'.: .\ brpl(xlib. lt is :.1 :-t:3..11d:.1rd cunYent:ion ro import Matplotlib's pyplot library as
- -- - - - - - -
- - - - - - - - - - - -- - - - for kt1o '.\1edge
TEC ~ ', CAL Pu 6UCA TTONSS · an up-tnrvst
-
f oata sc~i:en'.:c:.::e:___ _ _ _ _ _ _(5 - 3)
~__:_ Data Visualization
_ _ _ _ _______ ______ _
dations o
f<Pn "draw" this plot in the background, but we need to bring it to the screen
.
lot w111
fhe plt.P dy after graphing everything we intend to.
, we're rea '
\\'hen . With that, the graph should pop up. If not, sometimes it can pop under, or you
5howO·
, plt. tten an error. Your graph should look like :
111aYhave go
--------- - - - - - - -.........---"--,...;.._--'---r:\....:=~=•@~aJ.i'i~J\
~ r,gur~ 1
6.5
6.0
5.5
5.0
4..5
4.0~-------------~=-------c:,'
1.0 1.5 2 .0 2 5 3 0
~ 0101+1. l,sl~l
• This Window is a matplotlib window, which allows us to see our graph, as well as interact
with it and navigate it
ilnport Ill .
atplotlib.pyplot as plt
ilnt>ort n
Uinpy as np
ic::::: np Un
. space(-1, 1, 50)
n "' 2*}t + 1
y2 - 2••x +1
plt.plot(x, y2)
plt.plot(x, yl ,
pit.show()
3.0
2.5
2.0
1.5
1.0
0.5
0.0
-0.5
-1 .0
impo rtnum py as np
.'
I I CHNICAL PIJ/;Jl/C/\ 7 ®
IONS · n11 t1/J./h111st for knowtocl!J8
,=ou'!!!_afions <}! Data Science (5 - 5) Data Visualization
print (Y)
plt.plot(X, Y)
plt.xlabel('x - axis')
plt.ylabel('y - axis')
#Seta title
plt.title('Draw a line.')
pit.show()
2. transparent can be set to True, which causes the background of the chart to be
transparent.
3. bbox_inches can be set to alter the size of the bounding box (whitespace) around the
output image. In most cases, if no bounding box is desired, using bbox_ inches = 'tight' is
ideal.
4· If bbox_ inches is set to 'tight', then the pad_inches option specifies the amount of
padding around the image.
- lbil:k ncss.
Ou tpu t:
1 2 3
X 4 5
• A grid can be add ed to a Ma tplo tlib
plo t usin g the plt. grid () com man d.
By defa ul, the grid
is turn ed off. To tum on the grid use
:
•
\L__ __ __ __ __ __P_lt_.gn
_ 'd_(T
_ ru
_ e_) _ __
The onl y vali d opt ion s are plt. grid (Tr
_ _ _ _ __ _ _ _ __ _
I
-- I
- --- --- - - - - - -
I, ___ ___ ___ ___ ___ __
[ plt.plot(x a, ya 'r--')
line.
• You can use"-" for a solid line (the default ),"-." for dash-dot lines, or":" for a dotted
Here is an example :
from matplot lib import pyplot as pit
import numpy as np
ya= xa**2
Ya = 3*xa
Pit.show ()
25
20
15
10
TECHNICAL PUBLICAT/ONS®
- an up-thrust for knowledge
_ -- ~ -:; !);:3 S,;:en.::e (5 - 9 Data Visualization
- - - - --
~_,_r,:.::-L' _:_-- -- - - - - - - - - - - - - - - - - - - - - - - - - - -
... ~::::s.
::a,:=~
~~
- - c::"e-rentiy shaped mari.:crs - ish between differen t -groups of
is a grear way to clisrinru
ff your control gTOLrp is all circ!~ and your experime ntal group is all X's the
;::::;·~e=~ pops oUL c Yai to colo:-blind \iewa-s.
• T: o:-e·s way to specify multiple marker s-ryles in a single scanerO call. but we can
:10
s...~u: our data om into groups and plot each marker style separate ly. Here we chopped
or ciara up into three equal groups.
1.5
X
• -•
1.0
0 .5
•
•
-· X
X ,
• •
•
••
xx
• •
0.0 •
X
•
•
•••
X
X X X
- 0 .5 •• ••• X X
- 1.0 • Xx
• •
- 1.5 • X
• •
- 1.00 - 0 .75 - 0 .50 - 0.25 0 .00 0 .25 0.50 0.75 1.00
plt.sho w()
w=4
h=3
d = 70
plt.fig ure(fig size=( w, h), dpi=d )
X = (0, 3, 5)
y = (1, 4, 3 .5)
label x = 1
label_ y = 4
arrow_x = 3
arrow _y = 4
plt.plo t(x, y)
cnunda~
~
0otpt1I : 5 r--- --.- ---- ,.... ---.----- .--- ---,
Maximum---..._
4
00 1 2 3 4 5
Creating a legend
rance and behavior of the plot
• There are several options available for custo mizin g the appea
multiple series and only
legend. By default the legen d always appea rs when there are
lt the legend shows point
appears on mouseover when there is a single series. By defau
e leaves.
values when the mous e is over the graph but not when the mous
line is presented in a table that
• A legend documents the individual elements of a plot. Each
line.
contains a label for it so that peop le can differentiate between each
import matpl otlib.p yplot as plt
import nump y as np
Z = X ** 2
250
-250
- 500
- 750
1000
-10 .0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5
(Il 5.2 Scatter Plots
• A scatter plot is a visual representation of how
two variables relate to each other. we can
use scatter plots to explore the relationship between
two variables, for example by looking
for any c01Telation between them.
• Matplotlib also supports more advanced plots
, such as scatter plots. In this case, the
scatter() function is used to display data values
as a collection of x, y coordinates
represented by standalone dots.
impo rtma tplot lib.p yplo t as plt
# X axis values:
X = 12,3,7,29,8,5, 13,11,22,33]
# Y axis valu es:
y = [4,7,55,43,2,4,11,22,33,44]
plt.s catter(x, y)
pit.s how ()
comparing plt.scatterO and pit.plot() : We can also produce the scatter plot shown above
• using another function within matplotlib.pyplot. Matplotlib ' splt.plot() is a general- purpose
plotting function that will allow user to create various differen t line or marker plots.
, We can achieve the same scatter plot as the one obtained in the section above with the
following call to pit.plot(), using the same data :
l p!t.plot(x, y , "o")
~ It.show()
, In this case, we had to include the marker "o" as a third argument, as otherwis e pit.plot(
)
would plot a line graph. The plot created with this code is identical to the plot created
earlier with pit.scatter().
• Here's a rule of thumb that can use:
a) If we need a basic scatter plot, use pit.plot() , especially if we want to prioritiz e
performance.
b) If we want to customi ze our scatter plot by using more advance d plotting features , use
plt.scatter().
• Example : We can create a simple scatter plot in Python by passing x and y values to
pit.scatter() :
# scatter_ plotting. py
Plt.scatte r(x, y)
Pit.show( )
Output:
1.
2 3 4 5 6 7 8 9
plt.scatter(x_a xisl, y_axisl, color='darkblue ', marker= 'x', label= "item 1")
pit.show()
• The chart displays two data sets. We distinguish between them by the colour of the marker.
1
Adding the error bar in Matplotli b, Python. It's very simple, we j ust have to write the vtt luc
of the error. We use the command :
plt.errorba r(x, y, yerr = 2, capsize = 3)
\\nere :
x == Toe data of the X axis.
y == Toe 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= Toe size of the lower and upper lines of the error bar
• A simple example, where we only plot one point. The error is the IO % on the Y ax is.
importmatp lotlib.pypl ot as plt
x =l
y = 20
Output:
22.0
21 .5
21 .0
20.5
20.0
19.5
19.0
18.5
• We plot using the command "plt.errorbar ( .. .)", giving it the desired characteristics.
importmatplotlib.pyplot as plt
importnumpy as np
x = np.arange(1,8)
y = np.array([20,10,45,32,38,21 ,27])
plt.show()
45 -
40 -
35
I t
30
25
20
15
!
10 !
I I 7
1 2 3 4 5 6 7
~ .
lti le lines in MatplothbErrorbar in Python : The ability to draw numerous lines in
, Mu P . .. , . .
t the same plot ts cnttca1. We 11 draw many errorbars in the same graph by usmg this
altnos
schern__e_._ - -- - - -- -- - - - - - - - - - - - - - - - - - - - - ,
unportnumPY as np
unportmatplotlib.pyplot as plt
fig = plt.figure()
x = np.arange(20)
y = 4 * np.sin(x / 20 * np.pi)
uplims =True,)
uplims = True,
lolims = True, )
Uplims = upperlimits,
10lims = lowerlimits, )
Plt.title('Example')
Plt.show()
Output:
• A contour line or isoline of a function of two variables is a curve along which the
function has a constant value. It is a cross-section of the three-dimensional graph of the
function f(x, y) parallel to the x, y plane.
• Contour lines are used e.g. in geography and meteorology. In cartography, a contour line
joins points of equal height above a given level, such as mean sea level.
• We can also say in a more general way that a contour line of a function with two
variables is a curve which connects points with the same values.
import numpy as np
~ -=--=--=---=----=----=----=---=--=--=--=--=--=--=--=--=--=--=--=--=-~-=--=--=-~-=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-~~~~~~~~~
){, y == np.rneshgrid(xlist, ylist
print(xliSt)
print(ylist)
print(X)
print(Y)
output :
[ - 3. 0. 3. ] Corresponds to the
following co-ordinate
[ - 3. - 1. 1. 3.]
points:
[ [ -3. 0. 3.]
[ -3. 0. 3. ]
(-3,- 3) (0, - 3) (3,-3)
[ -3 . 0. 3. ]
(- 3,-1) (0,-1) (3, - 1)
[ - 3. 0. 3. ] ]
(- 3, 1) (0, 1) (3, 1)
[ [ -3. -3. -3. ]
(- 3, 3) (0, 3) (3, 3)
[ - 1. - 1. - 1. ]
[ 1. 1. 1. ]
[ 3. 3. 3. ]]
Changing the colours and the line style
importmatplotlib.pyplot a s plt
Pit.figure()
fontsize= 10)
Plt.title('Contour Plot')
Plt.xlabel('x (cm)')
Pit.Ylabel('y (cm)')
Pit.show()
• When creating a contour plot, we can also specify the color map. There are different c)a<Jses
of color maps. Matplotlib gives the following guidance:
a) Sequential : Change in lightness and often saturation of color incrementally, often using
a single hue; should be used for representing information that has ordering.
h) Diverging : Change in lightness and possibly saturation of two different colors that
meet in the middle at an unsaturated color; should be used when the information being
plotted has a critical middle value, such as topography or when the data deviates around
zero.
c) Cyclic : Change in lightness of two different colors that meet in the middle and
beginning/end at an unsaturated color; should be used for values that wrap around at the
endpoints, such as phase angle, wind direction, or time of day.
d) Qualitative ·: Often are miscellaneous colors; should be used to represent information
which does not have ordering or relationships.
th
• Th'is d ata h'as b ot h pos1t1ve
· · and negative
· values which zero representing · a node for e
wave fiunction.
. There are three important
· '
display options for contour plots : the undisplaced
shape key, the scale factor, and the contour scale. .
10
. Iace d shape option controls if and how the deformed model 15
a) Th e disp · 5hown. he
. nl " is t
comparison to t~e undeformed (original) geometry. The "Deformed shape o Y
default and provides no basis for comparison. aJl
b) The "Deformed shape with undeformed edge" option overlays the contour plot on
outline of the original model
. tbe
c) The "Deformed shape with undeformed mod l" t· l the contour plot on
. . . · e op 10n over ays
ongmal fimte element model. ~
:,:"tbJt category .
1
100
80
c 60 ..,
::I C:
0 ::I
C.) 0
u
C:
CD
40
20
Bin 2
40~0=-=o:-1
"'"00-=-_._---:-3-:i~!';O:'"'.o:---==---
0 L.1--==-=L .-- 1 - -L-2-o.._o_o~L--2-s'-o--o..-J1---3~0
10 0 0 15 0 0
Daily steps taken
Bin 2 starti ng point
Fig. 5.5.1 : Hist ogra m
• H"1st the frequ ency tlf th~- dutn vah1 cs. The
ograms can disp lay a larg e amo unt of data and
rmin ed hy a histog,rnm . In addi tion . it can
rnedian and distr ibuti on of the data can be dete
show any outli. ers or gaps in the data .
,I
pute nnd d ispla y histn grnm s : pit .hist ()
•j•
• Matplotlib prov ides a dedi cate d func tion to com
for k11owledgo
TECH NICAL PUBL ICA T/ONS® • on 11p-thn isl
Foundations of Data Science (5 - 22) Data Visualization
x = 40 * np.random.randn(50000)
plt.legend()
plt.title(' Histogram')
plt.show()
import numpy as np
y2 = [10,11,12,13,14,15,16,17,18,19]
x = np.arange(10)
fig = plt.figure()
ax= plt.subplot(111)
plt.title('Legend inside')
ax.legend()
plt.show()
8 9
fig,ax==plt.su bplots ()
p
derivation of p
importmatplotlib.pyplotasplt
importnumpyasnp
y2 = [10,11,12,13,14,15,16,17,18,19)
x = np.arange{10)
fig = plt.figure{)
ax= plt.subplot{111)
plt.title('Legend inside')
plt.show()
~
output:
nd
eg:;.:e.:..:.
L-=
20 , - . . - - - - - - - ins
:.....:..:..: .:::::___
=:i:de _ _ _ __
18
16
14
12
10
2 3 4 5 6 7 8 9
ax.plot(x,y)
Output:
A single plot
1.00
0.75
0.50
0.25
0.00
-0.25
-0.50
-0.75 ,.
I
I,
0 2