Lab Sheet 05 - Numpy and Matplotlib
Lab Sheet 05 - Numpy and Matplotlib
General Program Lab 05: Numpy and Matplotlib Nov 09, 2020
1 Numpy
1.1 Introduction
1. Numpy (Numerical Python) is the most basic and a powerful package for working with multi-dimensional
data in Python.
2. You can create and manipulate multi-dimensional objects using numpy. It also provides functionalities
for vector element-wise operations and operations related to high dimensional vectors (arrays, matrices,
tensors etc.)
3. Let’s start exploring the functionalities of numpy library using the Python terminal. Since this is not
an inbuilt module in Python, you have to install it by issuing the pip install numpy command in the
command line interface (CMD). [You won’t need this step for the lab machines.]
4. As any usual module, first, you have to import the module (library) using the import command.
1 >>> import numpy as np
5. Since we have already used Python lists to create matrices and lists, let’s identify key differences between
Python lists and numpy arrays.
• numpy arrays (lists) can handle vectorized operations. To create a numpy array, we use the array()
method.
1 >>> ls = [1 ,2 ,3 ,4]
2 >>> ls +1
3 TypeError : can only concatenate list ( not " int ") to list
4 >>> np_ls = np . array ( ls )
5 >>> np_ls + 2
6 array ([3 , 4 , 5 , 6])
7 >>> 1/ np_ls
8 array ([0.33333333 , 0.25 , 0.2 , 0.16666667])
• Unlike Python lists, items inside the numpy array should be of the same type. If not, the library
itself will convert the items to a unified data type. This conversion might vary depending on the
data types in the given list. [Below, in the example ’< U 21’ is for Unicode strings]
1 >>> ls = [1 ,2 , True ,1]
2 >>> np_arr = np . array ( ls )
3 >>> np_arr
4 array ([1 , 2 , 1 , 1])
5 >>> ls = [1 ,2 ," Hello " ,1]
6 >>> np_arr = np . array ( ls )
7 >>> np_arr
8 array ([ ‘1 ’ , ‘2 ’ , ‘ Hello ’ , ‘1 ’] , dtype = ’ < U21 ’)
• Once a numpy array is created, you can’t add additional items to the array. However, in Python
lists, we can use the append() function to add new items to the existing array. Adding a new element
to a numpy array, can be done in 3 steps. First, you to convert it to a Python list, then add an item
and lastly convert it again to a numpy array.
1 >>> np_arr = np . array ([1 ,2 ,3 ,4])
2 >>> new_arr = np_arr . tolist ()
3 >>> new_arr . append (9)
4 [1 ,2 ,3 ,4 ,9]
5 >>> np_arr = np . array ( new_arr )
6 array ([1 ,2 ,3 ,4 ,9])
6. Numpy has it’s own set of inbuilt methods to create matrices and vectors. The size of the desired
matrix/array is given as a tuple object (r × c).
1 >>> np . ones ((3 ,3) )
2 [[1. 1. 1.]
3 [1. 1. 1.]
4 [1. 1. 1.]]
5 >>> np . zeros ((3 ,3) )
6 [[0. 0. 0.]
7 [0. 0. 0.]
8 [0. 0. 0.]]
7. Furthermore, we can use np.arange() method to create arrays/lists which is same as the inbuilt function
range(). The only difference is arange() method will return a numpy.ndarray object instead of a Python
list object.
1 >>> np . arange (0 ,19 ,1)
2 [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18]
3 >>> type ( np . arange (1 ,5 ,1) )
4 < class ‘ numpy . ndarray ’ >
8. After creating arrays, we can use the np.reshape() method to reshape it as we want. In here, the dimensions
are specified using the second argument (a tuple).
1 >>> np . reshape ( np . arange (0 ,20 ,1) ,(4 ,5) )
2 [[ 0 1 2 3 4]
3 [ 5 6 7 8 9]
4 [10 11 12 13 14]
5 [15 16 17 18 19]]
2. You can extract specific portions on an array using indexing starting with 0, something similar to how
you would do with Python lists slicing. For this, we use the : operator.
1 # All rows , second column
2 >>> matrix [: ,2]
3 matrix ([[ 3] ,
4 [ -3] ,
5 [ 8]])
6 # All rows , one column
7 >>> matrix [: ,:1]
8 matrix ([[ 1] ,
9 [ -1] ,
10 [ 6]])
11 # All rows , two columns
12 >>> matrix [: ,:2]
13 matrix ([[ 1 , 2] ,
14 [ -1 , -2] ,
15 [ 6 , 7]])
2. We can use the np.dot() function to compute the dot product of two matrices.
1 >>> mat1 = np . array ([[1 ,2] ,[ -1 , -2]])
2 >>> mat2 = np . array ([[4 ,5] ,[ -8 , -9]])
3 >>> np . dot ( mat1 , mat2 )
4 [[ -12 -13]
5 [ 12 13]]
3. To calculate the determinant of a matrix, we use the det() method provided in the numpy.linalg module.
1 >>> np . linalg . det ( mat2 )
2 3.9999
4. Often in Linear Algebra, we have to compute the inverse of a matrix. For this numpy.linalg module
provides inv() method.
1 >>> np . linalg . inv ( mat2 )
2 [[ -2.25 -1.25]
3 [ 2. 1. ]]
5. Moving on, we can use the solve() function to solve a system of linear equations provided in the form of
Ax = b.
x+y+z =6 2y + 5z = 4 2x + 5y − z = 27
6. We can use the same programming structure (with for loops) to iterate through a numpy matrix.
1 # GP106 Lab
2 # numpy with for loop
3
7. Numpy also provides methods for statistical calculations. Following code segment describes the use of
mean() method. For this function we have to provide an extra argument called axis. This argument
specifies whether we are considering the row-wise mean or the column-wise mean in the matrix. By
default, it will compute the mean of the whole matrix.
1 # GP106 Lab
2 # Numpy with statistical methods
3
10 print ( mat1 )
11 print ( " Mean value of all items %.2 f " % np . mean ( mat1 ) )
12 print ( " Mean value of columns " , np . mean ( mat1 , axis =0) )
13 print ( " Mean value of rows " , np . mean ( mat1 , axis =1) )
8. Numpy polynomial and roots modules provides a set of useful functions for solving higher order polyno-
mials.
• To find the roots of a polynomial we use numpy.roots() function. The coefficients for the polynomial
should be passed as a Python list. Let’s find the roots of x3 + 3x + 1
1 >>> import numpy as np
2 >>> coeff = [1 ,0 ,3 ,1]
3 >>> np . roots ( coeff )
4 array ([ -3.27+0. j , 0.13+0.94 j , 0.13 -0.94 j ])
• To evaluate a polynomial at a given point, we use the polyval() function. As arguments, you have to
provide the evaluation point and the coefficient vector. For instance, if we want to evaluate x2 +4x+9
at 2 and 1, then the calculation looks like this,
9. A complete documentation of numpy library can be found in the official link: https://numpy.org/ or
simply, you can use the help() function.
Linear 2D Graphs
1. To plot a linear graph, we are using several helper functions provided in matplotlib.
• plot(x,y,color) : Function used to plot a graph. (x,y can be numpy arrays or a Python lists)
• title(str,fontsize) : Title of the graph.
• xlabel(str,fontsize) : X axis label of the graph.
• ylabel(str,fontsize) : Y axis label of the graph.
• grid() : Add a grid to the graph (optional).
2. Let’s plot the sin(x) where x is between [0, 3π]. For this step, first you have to import some libraries,
and then you have to use above functions.
3. We use linspace(start,end,n) function from numpy to generate n number of spaces between start to end.
4. Figure 1 shows the rendered plot. After rendering, you can save it using the save button.
2. Figure 2 shows a figure containing multiple plots (sin(x) and cos(x)). To achieve this task we can call the
same plot() function again.
3. Since we are plotting two series, we have to add a legend to the graph. As shown in the following code
segment, we use legend([str]) to add a legend to the graph. You have to make sure that the header (graph
legend text) list is in the same order as the plotting order.
1 # GP106 Linear Plots
2
3 # import libraries
4 import matplotlib . pyplot as plt
5 import numpy as np
6
4. Additionally, matplotlib provides options to format the plotting line. Here, we can use the -or to specify
a continuous red line segment with o pattern (scatter). Further, we can use - - to specify a dashed line
plot.
Bar Charts
1. To plot a bar chart matplotlib provides a function called bar(). This function has several mandatory
arguments, and the same function can be used to modify the bar chart as well (alignment, width etc).
2. Additionally, we use xticks() function to specify the x labels of the graph. As same as the plot() function,
wev have to provide the x location of the bar. Then the next argument is a tuple describing the independent
variable (i.e x).
3. The following code segment can be used to render a bar chart shown in Figure 3.
1 # GP106 Bar Plots
2
3 # import libraries
4 from matplotlib . ticker import FuncFormatter
5 import matplotlib . pyplot as plt
6 import numpy as np
7
8 # x locations of bars
9 x = np . arange (7)
10
19 plt . show ()
4. Edit the same code providing the width argument (enter the argument as width=0.8 ).
Pie Charts
1. Matplotlib provides pie() method to plot pie charts. Following code segment describes how to plot a pie
chart using matplotlib.
2. Here, the pie() method multiple arguments as shown in the code segment, and it will return some objects
which will be used as arguments to the legend() function.
1 # GP106 Pie chart example
2 import matplotlib . pyplot as plt
3
4 # plot labels
5 labels = [ " Too Easy " , " Easy " , " Moderate " , " Difficult " , " Very Difficult " ]
6
7 # data ( statistics )
8 sizes = [4.6 , 57.0 , 21.7 , 27.1 ,0.6]
9
10 # colours
11 colors = [ " yellowgreen " , " gold " , " lightskyblue " , " lightcoral " , " b " ]
12
13 # plot setup
14 patches , texts = plt . pie ( sizes , colors = colors , shadow = True , startangle
=90)
15 plt . legend ( patches , labels , loc = " best " )
16 plt . axis ( " equal " )
17 plt . title ( " GP106 Labs " , fontsize =14)
18 plt . show ()
Sub-Plots
1. Often, you have to plot multiple graphs in the same figure. We can plot each graph on top of the first
graph (as shown in Multiple Series section) or we can plot it as a separate graph.
2. For this we use the subplot(r,c,loc) method. The r argument specifies the number of rows in the subplot,
c specifies the number columns and loc specifies the location of the graph/plot.
3. For instance, if you want to plot 3 graphs, we can specify the subplot() pattern as subplot(3,1,loc) or
either subplot(1,3,loc). As you can see, it is kind of a matrix of graphs where the loc parameter indicate
the plotting location.
4. The following code segment describes how to plot multiple graphs in the same figure using subplot()
1 import matplotlib . pyplot as plt
2 import numpy as np
3
Figure 5: Subplots
Additional Information
1. Colours other than basics:
matplotlib provides a basic set of colours which can be specified using a single letter (b: blue, g: green,
21
18 # plot
19 plt . plot (x ,y , " - og " ) , plt . plot (x , y_es_1 , " b " ) , plt . plot (x , y_es_2 , " r " )
20 plt . legend ([ " Data Points " , " 1 st Order " , " 2 nd Order " ])
21 plt . xlabel ( " X " ) , plt . ylabel ( " Y " )
22 plt . grid ()
23