Data Visualization Using Pyplot
Data Visualization Using Pyplot
8
using Pyplot
In Tis Chapter
8.1 What is Data Visualization ?
8.2 Using Pyplot of Matplotlib Library
8.3 Creating Charts with Matplotlibs Library's Pyplot Interface
decisions.
285
286 COMPUTER SCIENCE WITH PYTHONY
A
For instance, a company has to decide about, which advertising solution should it invest
1vest in to
promote its new product" Data visualization is here to help just pull out data of comnan
previous campaigns, and plot a bar chart comparing the pertormance of difterent platforms pany's
ms and
bingo! the right decision is right in front. DATA VISUALIZATION
This chapter is dedicated to get you started with data Data
visualization basically
visualization using Python's usetul tool PyPlot where you refers to the
graphical or visual
shall learn to represent data visually in various forms such representation of information
and data using visual
as line, bar and pie charts etc. So, let's get started. elements
like charts, graphs, and
maps
etc.
S.2 USING PYPLOT OF MATPLOTLIB LIBRARY
For data visualization in Python, the Matplotlib library's Pyplot interface is used.
Ihe matplotlib is a Python library that provides many interfaces
NOTE
and functionality for 2D-graphics similar to MATLAB 's in various
forms. In short, you can call matplotlib as a high quality plotting PyPlot is a collection of
library of Python. It provides both a very quick way to visualize methods within matplotlib
data from Python and publication-quality figures in many formats. library (of Python) which
allow user to construct 2D
The matplotlib library offers many different named collections of plots easily and interactively.
methods; PyPlot is one such interface.
PyPlot is a collection of methods within matplotlib which allow user to construct 2D plots
easily and interactively. PyPlot essentially reproduces plotting functions and behavior of
MATLAB.
For instance, a company has to decide about, which advertising solution should it invest in
promote its new product?" Data visualization is here to help - just pull out data of companv'e
to
previous campaigns, and plot a bar chart comparing the performance of different platforms nd
bingo! the right decision is right in front.
DATA VISUALIZATION
This chapter is dedicated to get you started with data Data visualization basically
visualization using Python's useful tool PyPlot where you refers to the graphical
representation of information
or
visual
shall learn to represent data visualy in various forms such
and data using visual
as line, bar and pie charts etc. So, let's get started.
ike charts, graphs, and
elements
etc.
maps
8.2 USING PYPLOT OF MATPLOTLIB LIBRARY
For data visualization in Python, the Matplotlib library's Pyplot interface is used.
on
MATLAB is a high-performance language for technical computing. It integrates computation, visualizdar
and programming in an easy-to-use environment where problems and solutions are expressed in lau
mathematical notation.
DATA VISUALIZATION USING PYPLOT
Chapter 287
Anaconda Navigator
ie Belp
You may also type
ANACONDA NAVIGATOR matplotlib here to
pgrade tio quickly search it here
Home
e nton ents
Installed Channels updateindex
nvironierits base (root)
iame T, Description
version
Documentation
mistune The fastest markdown parser in pure
python with renderer feature
0.8.3
Importing PyPlot
In order to use pyplot on your computers for data visualization, you need to first import it in
your Python environment by issuing one of the following commands:
With the first command above, you will need to issue every pyplot command as per following
syntax
matplotlib.pyplot.<command
But with the second command above, you have provided pl as the shorthand for
invoke PyPlot's methods as this
matplotlib.pyplot and thus now you
can
pl.plot (X, Y)
Oucan choose any legal identifier in place of pl above.
Home
me
nsa Channes update inde
Projects (beta)
merkuDafe pilementsmhtmiihei eru sete
string for pechon
Learning Scroll down in this
alphabetically sorted list matpiob Pthon 2 piotting rary
Importing PyPlot
In order to use pyplot on your computers for data visualization, you need to first import it in
your Python environment by issuing one of the following commands :
With the first command above, you wil need to issue every Pyplot command as per following
syntax
matplotlib.pyplot.<command>
But with the second command above, you have provided pl as the shorthand for
can invoke PyPloe's methods as this:
atplotlib.pyplot and thus now you
pl.plot(X, Y)
TOu can choose any legal identifier in place of pl above
XIL
But before we proceed with it, I shall recommend you to have an idea ul using NumPu
ing NumPy library
and some of its functions, for these two reasons, specifically :
(7) NumPy offers some other useful functions to create arrays of data, which prova.
while plotting data. useful
(1) NumPy offers many useful functions and routines.
(11) NumPy arrays which are like lists, support vectorized operations, i.e, if you.
function, it is performed on every item (element by element) in the array unlike lida apply
Let's suppose you want to add the number 2 to every item in the list or array and if1u
eg
you
give an expression such as List +2 (where List is a Python list), Python will give rror
but not with NumPy array. For a NumPy array, it will simply add 2 to each element of
the NumPy array.
This feature may prove very useful in ploting. e.g, if you have a NumPy array namelu
X (say, ((1,2,3, 41) ), and you want to compute sin() for each of the values of this ara
for plotting purpose, you just need to write is numpy.sin(X)
For these reasons, I recommend that you should know how to create NumPy arrays and
NumPy functions on them. Following section briefly introduces how to create and use NumBe
arrays.
The above import statement has given np as an alias name for NumPy module. Once imported
with as <name>, you use both names i.e., numpy or np for functions e.g., numpy.array() is same
as np.array().
Array in general refers to a named group of homogeneous NOTE
(of same type) elements. For example, if you store the Please note that if you have
similar details of all sections together such as if you store installed Python through
Anaconda installer then NumPy,
number of students in each section of class X in a school, e.g,
SciPy, Pandas, Matplotlib etc. are
Students array containing 5 entries as [34, 37, 36, 41, 40] then by default installed with Python,
Students is an array that represents number of students in otherwise you need to install
each section of class X. Like lists, you can access individual these separately through PIP
elements by giving index with array name e.g, Students[1] installer of Python.
289
Consider following code
Indices 0 1 2 3
import numpy as np numPy.function Name or np.function Name
e.g, nanpy.array() or np.array( )
List [ 1, 2, 3, 4]
Data al np.array(List)) It will create a
NumnPy array Jrom
the given list
Inside memory print(a1)
In 13]: import numpy as np Jupyter Untitled11
Logcut
File Edt
In [14]: List [1, 2, 3, 4] ew nsert e Kenel idgets Trusted P,thon s
+
In [15]: a1 =np.array (List) Run C Code
In
[2] import numpy 3s np
In [16]: al
Out [16]:array( [1, 2, 3, 41) In [4: List - [1, 2, 3, 4
al np.array(List)
In [17]: print (a1)
1 2 3 4] In 7 : a
Individual elements of above array can be accessed just like you access a list's, i.e.,
Karray-name> [ <index>]
That is, a110] will give
you 1, al[1] will give you 2,..al3] will give you4.
rollowing Fig. 8.1 illustrates the basic terms associated with a NumPy array:
axis= 1
axis 2
10 0 he axes of an The shape of an
010 array describe array is a tuple
00 1 the order of indicating the All elements must be of
number of elements the same dtype (datatype)
1 0 0 indexing into the
array, e.g., axis = 0 along each axis. An
01 0 refers to the first existing array a has The default dtype is float
00 1 index coordinate an attribute a.shape in an NumPy array
1 0 0 axis = 1 the which contains
0 1 0 second, etc. this tuple
Shape (8, 3)
Figure 8.1 Anatomy of a NumPy Array
array
ay and (ii) itemsize that returns the length of each element ot array
in bytes. (see below)
The start, stop and step attribute provide the values for starting value, stopping valueand
step value for a numerical range. Start and step values are optional. When only stop value is
given, the numerical range is generated from zero to stop value with step 1.
The dtype specifies the datatype for the NumPy array.
Consider the following statements: Stop value
Start vale Step value
In [73]: arr5 np.arange(7)
In [77]: arr6 = np.arange (1,7, 2, np.floats2)
In [74]: arr5
Out [74]: array( [0, 1, 2, 3, 4, 5, 6]) In [78]: arr6
Out[78]: array([1., 3., 5.], dtype=float32)
In
arr5.dtype
[75]:
Out[75]: dtype("int32')
(i) Creating arrays with a numerical range using linspace(). Sometimes, you neeu c
enly
spaced elements between two given limits. For this purpose, NumPy provides Imsp
be
function to which you provide, the start value, the end value and number of elements
generated for the ndarray. The syntax for using linspace() function is:
rated>)
arrayname> =
numpy. linspace(<start>, <stop>, <number of values to be genei
2 to 3
For example, following code will create an ndarray with 6 values falling in the rattg
Arri np.linspace(2, 3, 6)
Chgpter 8: DAIA VISUALIZAIICON USING PYPIOT
291
Consider some more examples:
Trigonometric functions
sin(x, /[, out]) Trigonometric sine, element-wise.
cos(x, /[, out]) Cosine element-wise.
tan(x, /[. out]) Compute tangent element-wise.
arcsin(x, /[, out]1) inverse sine, element-wise.
arccos(x, /[. out]) Trigonometric inverse cosine, element-wise.
exp2(x, /[, out]) Calculate 2*p for all p in the input array
Log(x, /[, out]) Natural logarithm, element-wise.
log10(x, /[, out]) Return the base 10 logarithm f the input array, element-wise.
log2 (x, /[, out]) Base-2 logarithm of x.
X array_like sequence
bee below how useful the numpy functions are while plotting. Although you shall learn about
all components of plotting in the coming section, yet following code lines make you appreciate
the use of numpy arrays and functions. (Don't worry if things are not clear right now, they will
In [42]: y np.log(x)
In [43]: pl.plot(x,y)
Out[43]: [<matplotlib.lines. Line2D at 8x8ef79d8>]
6
12
10 1
08
06
04
02
00
10 15 20 25 30 35
40 4550
rectangular bars with heights or lengths proportional to the values that they ie categoricalu epresent.
100
075 1
050 1
025 1 5
000
0 25
-050
)5
-075
-100 00
10 Ban Del Hyd Mum
Aline chart or line graph is a type of chart which A bar chart is a chart that presents categorical data
displays information as a series of data points called with rectangular bars with heights proportional to the
Vidya
Kshama
A pie chart (or a
16.71% 32.2996
circle chart) is a
circular statistical
New
graphic, which is
divided into slices to m 24 22%
26.5
Following section talks about how you can create various chart types using pyplot methods.
You know that graphs and charts play a big and an important role in data visualization and
decision making, Every chart type has a different utility and serves a different purpose.
Let us talk about how you can create line, bar and pre charts using matplotlib.pyplot library of
Python.
As stated earlier make sure to import matplotlib.pyplot library interface and other reauired
1ibraries before you start using any of their functions.
List c containing
values squares of
C =[1, 4, 9, 16]
list a vS values of
list b, then, in simnlacs
Now, it you want to plot a line chart for all values of
form you may write:
statement 1s to be given just once
The import
import matplotlib.pyplot as pl
pl.plot(a, b)
NOTE
And python will show you result as:
Data points are called
markers
In [28]: pl.plot(a, b)
[<matplotlib. lines. Line2D at 8xd277e38>]
Out[28:
But are you satisfied with the result? Shouldn't the axes show
the labels of the axes? Let us say we want to label the x-axis, the NOTE
horizontal axis, as 'Some values' and the y-axis, the vertical axis Before using any PyPlot
as 'Doubled values'. You can set x-axis' and y-axis' labels using function or numpy
functions xlabel() and ylabel() respectively, i.e.,: function, make sure that
these libraries are
imported beforehand.
<matplotlib.pyplot or its alias> .xlabel (<str>)
and
You can control the type of marker i.e.,, dots or crosses or diamonds etc. by specitying desired
marker type from the table below. If you do not specify marker type, then data points will not
be marked specifically on the line chart and its default type will be same as that ofthe line type.
Also, the markersize is to be specified in points and markeredgecolor must be a valid color
15 20 25 0 35 40
Note you can combine the marker type with color code, eg., 'T+ when given for line color marks
these:
the color as 'red' and markertype as plus(+). Consider two following examples combining
Here marker color
Line colorand markerstyle combined separately specified
so marker takes same color as line
15 20 25 30
10
40
10 15 20 25 30 35
COMPUTER SCIENCE WITH PYTHON
296 XI
features like
Line width, Line color, line style
Line marker tyype, size
etc.
specify the color code next to
the data being plotting in plot
To change line color, you can
function as shown below:
<color code>)
matplotlib> .plot (<data1>, [, data2],
You can use color codes as: 'r' for red, 'y' for vellow, 'g tor green, "b for blue etc. (Complete
b = np.sin(x)
Color code blue for ndarray a
plt.plot (x, a, 'b')
Color code ed for ndarray b
plt.plot(x, b, 'r')
plt.show()
The output produced by above script is as shown below
100
075
050
025
000
-0 25
50
-0 75
-1 00 NOT
2 4 6 10 color
Even if you skip the
information in plot(
Please note that even if you skip the Python will plot
multiple
color information in plot(), with
Python will plot multiple lines in the same plot with different lines in the same plot
but these
colors but these colors are decided different colors
internally by Python. colors are
decided
internally by Python.
fer8DATA VISUALIZATION USING PYPLOT
297
Table 8.2 Dijfferent Color Codes
00
#x, a, bare same as
earlier code -0 25
plt.plot(x, a, linewidth = 2) -050
plt.plot (x, b, linewidth = 4) -0.75
2 pts linewidth
-1.00
The result produced by above code is as
shown here :
To change the line style, you can add following additional optional argument in plot( ) function:
linestyle or ls [ 'solid'| dashed', dashdot, dotted']
=
e.g
#X, a, b are same as earlier code Possible line styles are: 'solid'
plt.plot(x, a, linewidth = 2)
for solid line, 'dashed' for
dashed line, 'dotted' for dotted
plt.plot(x, b, linewidth =4, linestyle = 'dashed') line nd
'dashdot for
dashdotted line.
The result produced by above code is as shown here:
100 Linestyle =*'
075 linestyle= dashed"
050
025 Linestyle =''
000
-25
Linestyle ='
-50
15
-1 00 Linestyle ''
10
You can control the type of marker i.e., dos or crosses or damonds etc. by sSpecilying desire.
marker type from the table below. If vou do not specify marker type, then data points will
not
be marked specitically on the line chart and its default type will be same as that of the linetype
Also, the markersize is to be specified in points and markeredgecolor must be a valid color
marker
marker description marker description description
point marker square marker 3 tri left marker
10 15 20 25 30 35 40
Note you can combine the marker type with color code, eg., 't' when given for line color marks
the color as 'red' and markertype as plus(+"). Consider two following examples combining these:
In
In [75]: plt.plot(p, 9, ,linestyle solid') [76]: plt.plot(p, 4, 1inestyle
Out 75]: [<matplotlib.lines.Line2D at 0xbdfc470>] markeredgecolor="b)
Out[76]: [ <matplotlib. lines. Line2D at 8xbd46c58>
8
15 20 25 30 35
40
15 20 5 30 5
298 COMPUTER SCIENCE WITH PYTHON
- XI
1ou can control the type of marker i.e., dots or crosses or dianmoinas etc. by specifying desino
marker type from the table below. If you do not specify marker type, then data points will
not
be marked specifically on the line chart and its default type will be same as that of the line tyno
pe
Also, the markersize is to be specified in points and markeredgecolor must be a valid color
10 15 20 25 30 35 40
Note you can combine the marker type with color code, e.g, 'r+' when given for line colormarks
the color as 'red' and markertype as plus(+). Consider two following examples combiningthese
15 20 25 30 5 40
10 15 20 25 30 35 40
Chopter 8 : ATA VISUALIZATION USIN PYPLOT
299
See, when you do not specity markeredgecolor separately in
color as the line. Also, it
you do not specify the
plot(), the marker takes the samne
linestyle separately along with linecolor- and-
marketstyle-conmbinati0H-string (e.g., 'rt above), Python will only plot the markers and not the
line, (see below). To get the line, specify linestyle argument also as shown above.
In [3]: plt.plot(p,9, 'no")
Out[3]:[<matplotlib.lines.Line2D at 0x8834770>]
20 25 30 35 40
Rest of the arguments affect the look and format of the line chart as given below
color code with markertype color and marker symbol for the line chart
linewidth width of the line in points (a float value)
linestyle can be ['solid' | 'dashed', 'dashdot, 'dotted' or take markertype string|
marker a symbol denoting valid marker style such a s , 'o', *, '*, 'd', and others
etc.
please note that this is still not the complete and full syntax of plott )
Complete syntax coverage of plot() is beyond the scope of the book.
NOTE
If vou are working on a Jupyter Notebook, you can make the plots appear inline
Example 8.1 Create an array in in the notebook by using following magic function :
the %amatplotlib inline
range 1 to 20 with values 1.25
If vou don't use above function, the plots will appear in a pop up window
part. Another array contains the as with
other methods.
Og values of the elements in first
array.
vs second in a line chart.
4)Simply plot the two arrays first
(6) In the plot of first s second arvay, specify the iNS (Cotanng first array's values) title a
Random Values' and y-axis title as "Logarithm Values'.
300 COMPUTER SCIENCE WITH PYTHON
(c) Create a third array that stores the COS zalues offirst array and then plot both Hhe second and
tiird arvays os first array. The Cos values should be plotted with a dashdotted line.
(i) Change the marker type as a circle with blue color in second array.
(e) Only mark the data points as this : second array data points as blue small diamonds, third
arvray data points as black circles.
Note Show commands and their results.
Solution.
import numpy as np In [16]: print(a)
2.25 3.5 4.75 6. 7.25 8.5 9.75 11. 12.25 13.5
I 1. 14.75
Import matplotlib.pyplot as plt 16. 17.25 18.5 19.751
a
np.arange(1, 20, 1.25)
=
In [17]: print(b)
0.81093022 1.25276297 1.55814462 1.79175947 1.98180147
[0.
b np.log(a) 2.14006616 2.27726729 2.39789527 2.50552594 2.68268969 2.69124308
2.77258872 2.84781214 2.91777073 2.98315349]
25
20
15
10
05
00
50 75 10 0 12 5 15.0 17.5 200
05 1
02
In [31]:
pl.ylabel( Squared values ") In [32]: pl.plot(a,b)
Out [31: Text(0,0.5, Squared values")
10T Out[32]: [<matplotlib. lines Line20 at 0xd42b390>
.
81
2 04 08 15 20 25 30 40
No X-axis title only Y-axis title as
only
current statement is considered
See, Python has shown us three ditferent charts, one for each statement we
what issued. But this is not
we wanted. We wanted all these three statemernts to take effect in a
single plot.
On the if you give these three statements, Python
prompt, interactively draws
statement separately. To apply plot for each
multiple
statements on a single plot, do one of the following:
1) Either write a script, store it with py extension and then run it
10 15 30
Seme values
Chopter8 : DAIA VISUALIZAIION USING PYPLOT
301
()
C np.cos(a)
In [21]: c =np.cos (a)
plt.plot(a, b)
plt.plot(a,b) plt.plot (a, c, linestyle- dashdot
plt.show()
plt.plot (a, C, linestyle ="dashdot")
plt.show()
30
00
-05
To see
these graphs
*Bsesueu* a eanan oun 25
20
action
-05
-10
Scan
25 50 75 10.0
OR Code 125 15 0 175 20 0
20
5
0
-05
5
50 5 100 12 5 15 0 175
200