Book - Uddin Et Al 2023
Book - Uddin Et Al 2023
Editors Written by
Professor Dr Yubin Li Md. Jalal Uddin
Professor Dr Golam Dastagir Nishat Rayhana Eshita
Md. Asif Newaz
Naiem Sheikh
Afifa Talukder
Aysha Akter
Md. Habibur Rahman
Md. Babul Miah
1
https://researchsociety20.org/about-us/
ISBN: 978-984-35-3614-3
i
Author affiliations
Aysha Akter
Nishat Rayhana Eshita Environmental Science Discipline,
Department of Environmental Sciences, Khulna University, Bangladesh
Jahangirnagar University, Bangladesh
E-mail: ayshaku11@gmail.com
E-mail: nishateshita@gmail.com
Md. Habibur Rahman
Md. Asif Newaz Institute of Disaster Management and
Research Consultant, Remote Sensing Vulnerability Studies, University of
Division, Center for Environmental and Dhaka, Bangladesh
Geographic Information Services
(CEGIS), Bangladesh E-mail:
habiburrahmanidmvs710@gmail.com
E-mail: asifku24@gmail.com
Naiem Sheikh
Department of Geography and Md. Babul Miah
Environment, Jahangirnagar University, MS in Geo-information Science and
Bangladesh Earth Observation, Patuakhali Science
and Technology University (PSTU).
E-mail: sheikh.46@geography-juniv
edu.bd E-mail: babul.bsmrstu16@gmail.com
1
Table of Contents
Chapter 1: Arithmetic Operators in Python ....................................................................... 4
1.1 Introduction to an arithmetic operator ..................................................................... 4
1.2 Operand ................................................................................................................... 4
1.3 Variables and arithmetic operations ........................................................................ 5
1.4 Practice with real data.............................................................................................14
Chapter 2: Comparison (relational) and logical operators in Python ................................17
1. Comparison operators ...............................................................................................17
2. Logical operators ......................................................................................................26
Chapter 3: Bitwise, assignment, and membership operators in Python ............................32
1. Bitwise operators ......................................................................................................32
Example of Bitwise operators for real data ..................................................................33
2. Assignment and special operators (identity and membership) in Python .................37
Chapter 4: Application of input, range, string, index operator, slicing operator,
concatenation, and delete in Python ..................................................................................41
1. Use of “input()” function ..........................................................................................41
2. String in python ........................................................................................................42
3. Indexing ....................................................................................................................42
4. Slicing Operator........................................................................................................43
5. Concatenation ...........................................................................................................43
6. Repeat the String ......................................................................................................44
7. Convert string to upper case .....................................................................................44
8. Converting String to lowercase ................................................................................44
9. Capitalizing the first letter of every word .................................................................44
10. Replacing String .....................................................................................................45
11. Split String ..............................................................................................................45
12. Join String...............................................................................................................45
13. Delete String ...........................................................................................................45
14. Range ......................................................................................................................46
Chapter 5: Lists, tuple, dictionary, and set methods in Python .........................................47
2
1. Dictionary in Python .................................................................................................47
2. List in Python ...........................................................................................................55
3. Set in Python ............................................................................................................59
4. Tuple in Python ........................................................................................................63
Chapter 6: Looping (for, while) and conditional statement (if, else) in Python ................67
1. Loop in Python .........................................................................................................67
2. Conditional Statements .............................................................................................75
3. Break and continue statements in Python .................................................................77
Chapter 7: Functions in Python .........................................................................................80
1. Functions in Python ..................................................................................................80
2. Wind speed calculation function ..............................................................................82
Chapter 8: Data analysis with Pandas ...............................................................................87
Chapter 9: Data visualization in Python – temporal & time series plots .........................100
Chapter 10: Mapping in Python with Cartopy, Xarray, and NetCDF4 ...........................118
3
Chapter 1: Arithmetic Operators in Python
+ Addition a+b
- Subtraction a–b
* Multiplication a*b
/ Division a/b
% Modulus a%b
** Exponentiation a ** b
// Floor division a // b
1.2 Operand
In Python, operators are special symbols that are used for computation. The values on
which an operator acts are called operands. Here is an example.
a = 10
b = 20
4
a+b
30
In this case, the ‘+’ operator adds the operands ‘a and b’ together. An operand can be either
a value or a variable that references an object.
a = 10
b = 20
a+b-5
25
The creation of variables in Python is simple. We just need to write the name of the variable
with the assigned value by using the equal sign (“=”), as shown below.
a=5
b=6
Here, ‘a and b’ are variables with assigned values using equal signs. These variables will
be used for the next calculation of operators.
b) Addition by Syntax
The syntax in Python means how program will be written and interpreted.
For the addition of two numbers in python, we will use the “+” operator and then the print
function will be used to get the final output. We will define another new variable “c” with
an assigned formula of addition for two variable values. The syntaxes are given below:
a=5
5
b=6
c=a+b
print('Addition by Syntax = ', c)
In Python, anything inside the quotes is a string. We can use either single quotes or
double quotes to define strings that can be numeric or non-numeric data.
c) Addition by Function
In the previous section, we learned how to perform addition by the operator. Now, we will
perform the same operation by using the function. This function is contained in a library
named operator. For the addition of values, the operator has added a function. First, we
need to import the operator. After importing the library, we will call the add function from
the library by “operator.add()”. The syntaxes are given below:
import operator
a=5
b=6
d = operator.add(a, b)
Here, the operator library has been imported. Variables a and b are assigned with values.
Variable d is assigned for adding the values of a and b by “operator.add()” function.
d) Subtraction by Syntax
In Python, “–” is the subtraction operator. It is used to subtract the second value from the
first value. The syntaxes of subtraction are given below:
a=5
b=6
subs = a-b
e) Subtraction by Function
We will perform subtraction by the “operator.sub(a,b)” function. This function returns the
difference between the given arguments.
import operator
a=5
b=6
subs_1 = operator.sub(a, b)
Here, variable “subs_1” is assigned for calculating the difference between two values of
variables by using “operator.sub(a,b)” function.
f) Multiplication by Syntax
In Python, “*” is the multiplication operator. It is used to find the product of two values of
two variables.
Example:
a=5
b=6
mul = a*b
Variable “mul” is assigned with the multiplication of the values of a and b variables by
using asterix (*) sign.
g) Multiplication by Function
7
We can complete multiplication of two values using operator.mul () function from operator
library instead of asterix (*) sign operator. This function returns product of the given
arguments. Example:
a=5
b=6
mul_1 = operator.mul (a, b)
Here, variables a and b are being multiplied in assigned variable mul_1 by using
operator.mul () function.
Output:
h) Division by Syntax
The division operator in Python is “/”. It is used to find the quotient when the first operand
is divided by the second. An example of this operation is given below:
a=5
b=6
div = a/b
print('division by Syntax = ',div)
Here, variable div is assigned with the a/b. That means it will express the quotient of two
variables a and b.
Output:
i) Division by Function
The function “truediv()” is used to divide two values. This function returns the division of
the given arguments. It will express the actual result of division.
8
Example:
a=5
b=6
div_1 = operator.truediv(a, b)
Output:
The floor division in python is determined by “//”. It is used to find the whole number of
the quotient when the first operand is divided by the second.
Example 1:
a=5
b=6
fldiv = a//b
Output:
Example 2:
p = 6/5
p
Output:
Example 3
p = 6//5
9
p
Output:
a=5
b=6
fldiv_1 = operator.floordiv(a, b)
Output:
The modulus operator in Python is “%”. When the first operand is divided by the second,
it is used to calculate the remainder. The operands can be either integers or floats.
Example 1:
a=5
b=6
mod = a%b
Output:
10
Example 2:
q = 6%5
q
Output:
We can find out the remainder of a division between two operands by using the
operator.mod(a,b) function. This function resides in the operator library.
a=5
b=6
mod_1 = operator.mod(a, b)
Output:
In Python, “**” is the exponentiation operator. It is used to raise the first operand to the
power of the second.
a=5
b=6
exp = a**b # a**b (5*5*5*5*5*5 = 15625)
Example:
a=5
b=6
exp_1 = operator.pow(a, b)
print('Exponent by Function = ',exp_1)
Output:
Let’s take a simple example to understand all the arithmetic operators by syntax in a python
program by including all of them in a single program below:
a=5
b=6
c =a+b
subs = a-b
mul = a*b
div = a/b
fldiv = a//b
modu = a%b
exp = a**b
12
print('Addition by Syntax = ',c)
print('Subtraction by Syntax = ',subs)
print('Multiplication by Syntax = ',mul)
print('Division by Syntax = ',div)
print('Floor division by Syntax = ',fldiv)
print('Reminder by Syntax = ',modu)
print('Power or exponent by Syntax = ',exp)
Outputs:
Let’s take a simple example to understand all the arithmetic operators by function in a
Python program by including all of them in a single program below:
# define variables
a=5
b=6
d = operator.add(a, b)
e = operator.sub(a, b)
f = operator.mul(a, b)
g = operator.truediv(a, b)
13
h = operator.floordiv(a, b)
i = operator.mod(a, b)
j = operator.pow(a, b)
Output:
In pandas library, read_csv function is used for csv file and read_excel is used for excel
files. Essential commands are given below:
14
Syntax
import pandas as pd
dataset = pd.read_csv('class-1_arithmetic_operators.csv')
dataset.head()
Here, pd is the short form of imported pandas’ library. The variable dataset is defined with
an imported CSV file which represents year, min_temp and max_temp. In Python, by
default head() function represents data of the first five rows with all columns.
Output:
If we want to know how many rows and column are being existed in the data sheet, we can
use the shape() function. In Python, shape () function represents a number of all columns
and rows.
dataset.shape
Output:
After using the shape function, we can easily understand that thirty-eight rows and three
columns are being existed in the data file. Now, we will define two variables from the data
file for average temperature calculation.
min_tem = dataset.min_temp
max_tem = dataset.max_temp
Here, “\n” indicates new line creation. The head function represents the first five rows of
averaged data by default.
Output:
16
Chapter 2: Comparison (relational) and logical operators in Python
1. Comparison operators
A comparison or relational operator in python compares two operands' values and returns
True or False depending on whether the condition is met. We normally use this operators
in decision-making. Less than, greater than, less than or equal to, greater than or equal to,
equal to, and not equal to are examples of comparison operators (Table 2.1).
== Equal to a == b
!= Not equal to a != b
Now, let's see the examples of all the relational operators one by one for more
understanding.
The greater than (>) an operator checks whether the value of the left operand is greater than
the value of the right operand. The output of greater than is ‘True’ if the relation is correct
otherwise the output will be False.
17
a=5
b=6
print('If a is greater than b =',a>b)
Output:
Here, the output of the print function is ‘False’ because the value of ‘a’ is less than the
value of ‘b’, so the relation is wrong.
The less than operator is used for checking if the value of the left operand is less than the
value of the right operand. The output of < operator is ‘True’ if the relation is correct
otherwise the output will be ‘False’.
a=5
b=6
print('If a is less than b =',a<b)
Hence, the output will be ‘True’ because the value of ‘a’ is less than the value
of ‘b’. Therefore, the relation is correct.
Output:
The equal to operator is used for checking if the value of two operands is equal. The output
of equal to operator is ‘True’ if the relation is correct otherwise the output will be ‘False’.
a=5
b=6
18
print('If a is equal to b =',a==b)
Output:
We observe that the output of the print function is ‘False’ because the values of the
variable ‘a’ and ‘b’ are not the same, so the relationship is ‘False’.
In Python, not equal operator is denoted by “!=”, and it is being used for checking if the
value of the left operand is not equal to the value of the right operand. The output of the
not equal operator is ‘True’ if this relation is correct otherwise the output will be ‘False’.
Syntax
a=5
b=6
print('If a is not equal to b =',a!=b)
Output:
In the above-mentioned example, the output of the print function is ‘True’ because the
value of variable ‘a’ is not equal to the value of variable ‘b’. Consequently, the relation is
correct.
Greater than or equal to operator in python is used for checking if the value of the left
operand is greater than or equal to the value of the right operand. The output
of ‘>=’ operator is ‘True’ if this relation is correct otherwise the output will be ‘False’.
a=5
b=6
19
print('If a is greater than or equal to b =',a>=b)
Output:
In this example, the output of print is ‘False’ because the value of variable ‘a’ is neither
greater nor equal to the value of variable ‘b’. Thus, the relationship is ‘False’.
In Python, less than or equal to operator is denoted by “<=” and used for checking if the
value of the left operand is less than or equal to the value of the right operand. The output
of this relational operator is ‘True’ if the mentioned relation is correct, otherwise, the
output will be False.
a=5
b=6
print('If a is less than or equal to b =',a<=b)
Here, the output of the print function will be ‘True’ because the value of variable ‘a’ is not
equal but less than the value of variable ‘b’. Consequently, the relation will be correct.
Output:
Let’s take a simple example to understand all the comparison or relational operators by
syntax in a python program by including all of them in a single program below:
# Define variable
a=5
b=6
20
print('If a is greater than b =',a>b)
Output:
Let’s take simple examples which are previously mentioned to understand all the
comparison or relational operators by operator function in Python program by including all
of them in a single program below.
a = 5 # define a variable
b=6
Output:
We observe that the syntax operator and functional operator gave the same types of
Boolean results for decision-making.
We will monitor drought (water scarcity) for Rangpur station in Bangladesh from 1994 to
1995. The example has been adapted from Uddin et al. (2020). Source:
https://link.springer.com/article/10.1007/s12517-020-05302-0
We have an excel file of data for analysis with a comparison operator for drought
monitoring. Pandas is one of the most popular and favourite library packages in Python
programming language for data file import and analysis. Pandas allow importing data from
various file formats such as comma-separated values, JSON, SQL database tables or
queries, Microsoft Excel, etc. To read the data file, we need to import the ‘pandas’ library
first. In the pandas' library, the read_csv function is used for CSV files, and read_excel is
used for excel files.
import pandas as pd
dataset = pd.read_excel('drought_data.xlsx')
dataset.head()
22
Here, the ‘pd’ is the short form of pandas library which is very helpful in data analysis.
Variable “dataset” is assigned with data file using the ‘pandas’ library. Then, the ‘head()’
function will represent the first five rows of data with all columns.
Output:
From the above-mentioned summary of data, we observe that data has two columns named
“Year and month” and another one is “SPI”. The first five rows only showed in the output,
which starts from zero because Python is zero-based programming language.
Now, we want to open the specific column and rows for analysis from the original data
file. For this reason, we need to create a new variable “SPI” is assigned with a specific
name of column (SPI) from the data file by using the name of the data file variable. The
slicing operator will be used for selecting specific rows. The implementation of the idea is
given below.
SPI.head()
Here, we perceive that variable SPI is assigned with SPI values with specific rows from
the original data file. In addition, the ‘head ()’ function represents a snap of data. After
running this program, the output will be:
23
Comparison or relational operators by Syntax and Function
Now, we will use comparison or relational operators by syntax and function for decision-
making. For extreme drought, values of SPI must be less than or equal to 2. Implementation
of this is given below:
# Comparison by function
import operator as op
Threshold = -2
print('Extreme drought = \n',op.le(SPI,Threshold))
Here, “\n” indicates one line feed. That means new line creation.
Output:
24
Now, we can easily interpret the locations where extreme drought is occurring.
For monitoring extreme drought, we can create a data frame where only extreme drought
time will have existed.
Syntax
extreme_drought = dataset[dataset['SPI']<=-2]
extreme_drought.head()
Output:
25
This is the data frame of extreme drought time, where the SPI value is less than or equal to
2.
2. Logical operators
A logical expression is a statement, which can either be true or false. In the previous
example, the mathematical expression a<b means that ‘a’ is less than ‘b’, and values of ‘a’
and ‘b’ where a≥b are not permitted. For more information, you can visit
https://stackoverflow.com/questions/21415661/logical-operators-for-boolean-indexing-
in-pandas.
Now, let's see the examples of all the logical operators one by one for more understanding.
a=5
print("a and a is = ", a<10 and a>0)
26
print("a and a is = ", a>10 and a>0)
Output:
We can also use the numpy module for the logical_and operator.
print("not a is = ",np.logical_not(a>10))
Output:
Output:
27
This output indicates the types of wet climates.
The values of SPI less than 1, less than or equal to -2 (SPI<1 or, SPI<=-2), indicates a dry
climate.
Output:
28
These results suggest that the weather type is dry.
For not extreme wet climates, the SPI value must be less than 2 (SPI<2).
Output:
29
For monitoring wet climate (1<SPI>2), we can create a data frame where only wet climate
time will have existed.
df_wet_climate.head()
Output:
For monitoring dry climate (1>SPI>=-2), we can create a data frame where only dry climate
time will have existed.
30
df_dry_climate = dataset[np.logical_and(dataset['SPI'].lt(1), dataset['SPI'].le(-2))]
df_dry_climate.head()
Output:
31
Chapter 3: Bitwise, assignment, and membership operators in Python
1. Bitwise operators
In Python, Bitwise operators work only on integers and format of binary- prefix 0b
included.
Python's "and", "or" and "not" logical operators are designed to work with "scalars".
However, "&", "|" and "~" Bitwise operators are designed to work with both "scalars" and
"vectors".
& Bitwise AND: Sets each bit to 1 if both bits are 1 a&b
| Bitwise OR: Sets each bit to 1 if one of the two bits is 1 a|b
^ Bitwise XOR: Sets each bit to 1 if only one of two bits is 1 a^b
Bitwise right shift: Shift right by pushing copies of the
>> leftmost bit in from the left, and letting the rightmost bits fall a>>
off
Bitwise left shift: Shift left by pushing zeros in from the right
<< a<<
and letting the leftmost bits fall off
b = 12
32
Output:
print("a | b =", a | b)
Output:
import operator as op
Output:
import numpy as np
dataset = pd.read_excel('drought_data.xlsx')
dataset.head()
Output:
33
SPI = dataset.SPI[133:156] # 1994-1995
SPI.head()
Output:
34
Output:
Output:
Output:
35
print("dry climate = \n",op.__or__(SPI.lt(1), SPI.le(-2)))
Output:
36
2. Assignment and special operators (identity and membership) in Python
The most common assignment operator is the equal sign “=”.For example, a = 15 assigns
the value of the integer 15 to the variable a.
a) Assignment operators
a = 15
b = 10
c=5
d=3
e=4
f = 15
g=7
a += 10 # a + 10
print ("a =",a)
b *= 10 # b * 10
print ("b =",b)
c /= 10 # c / 10
print ("c =",c)
d %= 10 # d % 10
print ("d =",d)
e **= 10 # e ** 10
print ("e =",e)
f //= 10 # f // 10
print ("f =",f)
g -= 10 # g - 10
37
print ("g =",g)
Output:
b) Special operators
Python language offers some special types of operators like the identity operator or the
membership operator.
Identity operators check if two values or variables are located on the same part of the
memory. While membership operators test whether a value or variable is found in a
sequence. For more information, you may visit at https://www.programiz.com/python-
programming/operators.
Syntax of Identity operators: "is" and "is not"
a = ["Jalal", "Ripa"]
b = ["Jalal", "Ripa"]
print(a is b) # Though a and b have the same content, a is not the same object as b.
c = 'Jalal'
d = 'Jalal'
print(c is d) # returns True because c and d are located on the same part of the memory.
print(a is not b)
Output:
38
Membership operators ("in" and "not in")
In Python, ‘in’ and ‘not in’ are the membership operators in Python. They are used to test
whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
Syntax
a = ["Jalal", "Ripa"]
b = ["Jalal", "Ripa"]
print("Jalal" in a)
print("Jalal" not in a)
Output:
Syntax
male = "Jalal"
female = "Ripa"
name_list = ["Jalal","Ripa","Jara","Zahura"]
Output:
Syntax
if (female in name_list):
print("female is exist in name_list")
else:
39
print("female is NOT exist in name_list")
Output:
40
Chapter 4: Application of input, range, string, index operator, slicing operator,
concatenation, and delete in Python
x = input("Enter your name: ") # The input() method converts into a string and returns it.
print("Hello, " + x)
print(type(x))
Output:
However, it is also possible to get an integer output from ‘input()’ function. To get an
integer output, we need to modify the code. The code is given below:
print(type(y))
Output:
41
2. String in python
The string data type in python is a sequence of characters. These characters are different
symbols. It may include alphabets, numbers or anything, but the condition is the string
must be written in quotation marks. For example ‘Hello’, “Hello Jalal”, ““Hello Jalal””
print(str_)
print(str1)
print(str2)
Output:
3. Indexing
The indexing in python is a way of calling a particular element by its position. By indexing,
one can access any element of choice directly. Python starts counting from zero, so the first
element is indexed as 0 then 1, 2 and so on.
01234 56789
He l l o j ala l
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
H e l l o J a l a l
Output:
4. Slicing Operator
The slicing operator is used to call a certain slice of a data set. “:” is used for slicing. Codes
for slicing are given below:
print(str_[0:3])
Output:
Hel
5. Concatenation
Concatenation is a way of adding two or more different strings together. The “+” operator
is used to connect strings. An example is given below:
print(con1)
Output:
43
6. Repeat the String
To print a string multiple times, the “*” operator is used. If we want to repeat a string thrice,
the codes will be the following:
print(str_*3)
Output:
"Jalal".upper()
Output:
'JALAL'
"Jalal".lower()
Output:
'jalal'
"hello jalal".title()
Output:
'Hello Jalal'
44
10. Replacing String
To replace one string with another, the ‘.replace()’ function is used herein.
Output:
'Hello jalal'
"hello jalal".split()
Output:
['hello', 'jalal']
Output:
'hello jalal'
The ‘del str_3’ removes the variable ‘str_3’. Now, if we want to print str_3, there will show
an error.
print(str_3)
45
Output:
14. Range
To create a range (start, stop, and step), a sequence of numbers is used.
Start refers to the number from where the counting starts, stop refers to the number where
the sequence ends and step refers to the difference between numbers. Codes are given
below:
range_of_data = range(1,10)
print(range_of_data)
Output:
range(1, 10)
46
Chapter 5: Lists, tuple, dictionary, and set methods in Python
1. Dictionary in Python
In Python, dictionaries are used to store data. It is an unordered collection of items. There
are certain features of a dictionary. They are given below:
Example of a dictionary:
print(mydict)
Output:
dict_with_funt = dict(name = "Jalal", wife = "Ripa Jalal", kids = "Jara Jalal", year_of_birth
= "1990", good_man = True, favourite_colors = ["white", "green", "red"])
print(dict_with_funt)
Output:
47
1.2. Nested dictionary
A nested dictionary refers to a dictionary, which includes another dictionary. It is a
dictionary within a dictionary which already exists.
An example can make the concept clearer:
training = {
"Research Society" : {
"participants level":"all",
"year" : 2020
},
"IMSA" : {
"participants level":"all",
"year" : 2020
},
"participants level":"all",
"year" : 2020
48
}
print(training)
Output:
Research_Society = {
"participants level":"all",
"year" : 2020
IMSA = {
"participants level":"all",
49
"year" : 2020
CUSS_RS = {
"participants level":"all",
"year" : 2020
training = {
"IMSA" : IMSA,
print(training)
Output:
"participants level":"all",
"year" : 2019,
"year" : 2020
Output:
In the output, we can see that year 2019 is omitted, only the year 2020 is printed.
Output:
'dict'
<class 'dict'>
51
print(dict_with_funt["name"])
Output:
Jalal
print(len(dict_with_funt))
Output:
6
print(new_dict)
Output:
print(new_dict1)
Output:
print(new_dict)
Output:
print(new_dict)
Output:
print(get_value)
Output:
Jara Jalal
Output:
print(keys)
Output:
print(values)
Output:
print(new_dict)
Output:
54
1.16. Removing the last elements from the dictionary
The following codes are used to remove the last elements from the dictionary.
new_dict.popitem()
print(new_dict)
Output:
print(new_dict)
Output:
{}
2. List in Python
A list in Python is a special data structure with a changeable and ordered sequence of
elements. The list is written in square brackets and items are separated by a comma.
Example:
list_item = ['jalal','Ripa', 2, 4, 6]
print( list_item)
Output:
['jalal','Ripa', 2, 4, 6]
0 1 2 3 4
['jalal','Ripa', 2, 4, 6]
55
Here, the index of ‘jalal’ is 0, the index of ‘Ripa’ is 1, the index of 2 is 2, the index of 4 is
3, and the index of 6 is 4. Reverse indexing is also applicable for the list items.
Output:
print(list_item)
Output:
['jalal','Ripa', 2, 4, 6,1]
Here, in ‘list_item’ list, there were [‘jalal’, ‘Ripa’, 2, 4, 6] elements. After using
‘.append(1)’, it is showing ['jalal','Ripa', 2, 4, 6,1].
print(list_item)
Output:
['jalal','Ripa', ’Tamim’, 2, 4, 6, 1]
print(list_item)
Output:
['jalal','Ripa', 2, 4, 6, 1]
print(list_item)
Output:
['jalal','Ripa', 2, 4, 6]
print(list_item)
57
Output:
[]
numbers.sort()
print(numbers)
Output:
[1, 2, 3, 4, 5]
print(numbers)
Output:
[5, 4, 3, 2, 1]
print(new_number)
Output:
[5, 4, 3, 2, 1]
58
3. Set in Python
Set is used to store multiple items in a single variable. It is created with a special built-in
function ‘set()’. The set is written in a curly bracket. Items are unordered and not indexed.
Items are unchangeable, and no duplicate elements are allowed. A set cannot have a list or
dictionaries.
Example:
myset = {"Name","Jalal","year of birth", 1990, "good man",True,"favourite_colors",
("white", "green", "red")}
Output:
From the output, we can see that the items in the set are not in a similar order as the input.
This is why sets are unordered and do not have any index.
Output:
print(myset)
59
Output:
{“Ripa” , “Jalal”}
myset3 = myset1.union(myset2)
Output:
Output:
60
myset5 = myset3.intersection(myset4)
Output:
myset3.add("Jara")
print("myset = ",myset3)
Output:
myset3.remove("Jalal")
Output:
{"Ripa", "year of birth"}
myset3.discard("Ripa")
print(myset3)
Output:
61
{“year of birth’}
Another function ‘pop()’ is also used to remove the item. The ‘pop()’ function removes the
last item from the list but as the set is not ordered, we cannot say which element will be
deleted after using ‘pop()’ function.
myset4 = {"Jalal","Ripa","year of birth","Jimi"}
myset4.pop()
Output:
symmetric_difference() function returns a new set which contains all the elements except
those elements that are common in both myset3 and myset4. The codes are the following:
myset5 = myset3.intersection(myset4)
Output:
Myset = {“Ripa” , “Jalal”}
From the output, we can see that myset5 only contains those elements which were common
in both myset3 and myset4.
3.9. Clear data
To clear the set completely, the ‘.clear()’ function is used. The codes are following
myset4 = {"Jalal","Ripa","year of birth"}
myset4.clear()
del myset4
Output:
4. Tuple in Python
A tuple is also used to store multiple items in a single variable where the items are
unchangeable and ordered. Tuples are created in the first bracket, and items are separated
by a comma.
Example:
tuple_ = ('Hello', 'Jalal')
print(tuple_)
Output:
(‘Hello’, ‘Jalal’)
63
print(tuple3)
Output:
('Jalal','Ripa', 2, 4, 6)
print(tuple4)
Output:
('Jalal','Ripa', [2, 4, 6])
Output:
print(tuple3[0:2])
Output:
('Jalal','Ripa', 2)
64
4.5. Concatenation
Concatenation is used to join multiple strings together. The ‘+’ is used for concatenation.
Codes are given below:
Let, two tuples tuple_ and tuple2 which we want to join.
tuple_ = 'Hello', 'Jalal'
tuple2 = (1,2,3,4)
print(con)
Output:
('Hello', 'Jalal', 1, 2, 3, 4)
Output:
('Hello', 'Jalal', 'Hello', 'Jalal', 'Hello', 'Jalal')
4.7. Delete
We cannot delete items from the list rather we can delete the entire tuple. The ‘del’ function
is used to delete any tuple.
Codes are given below:
del tuple_
print(tuple_)
Output:
65
4.8. Length of tuple
To know the length of the tuple, the ‘len()’ function is used. The codes are the following:
tuple5 = ('Hello', 'Jalal','1','Jalal','3')
print(len(tuple5))
Output:
5
Output:
print(tuple5.index('3'))
Output:
66
Chapter 6: Looping (for, while) and conditional statement (if, else) in Python
1. Loop in Python
Sometimes we need to do the same types of work again and again. This repetition of work
is more time-consuming and harder. Normally, we do not like this type of work. However,
Python programming language has a built-in function named “loop”. With the help of
“loop”, we can conduct the same types of work again and again. This is not time-consuming
and harder. There are two ways to create loops in Python: with the for-loop and the while-
loop.
print(i)
Here, we used ‘range()’ function that returns an immutable sequence of numbers between
the given range. It is used when a user has to do an operation a particular number of times.
Therefore, to generate a sequence of numbers, we can use the ‘range()’ function.
We can also define the start, stop and step size of ‘range(start, stop, step)’ function:
67
Example 1:
The ‘range(20)’ function will generate 0 to 19 and ‘for loop’ print each number from this
sequence individually. In addition, on each iteration, the variable “i” takes the value of the
item inside the sequence.
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
From this output, we observe that the iteration of numbers starts from 0 and ends at 19
because Python is 0 based and the ends point will be always n-1. However, the total count
number of series is 20.
Example 2:
If we want to print 1 to 20 that means iteration starts from 1, ends at 19 and step size by
default 1, so we write that:
for j in range(1,20):
print(j)
Here, 1 is the starting point of the sequence of the number mentioned in the range function.
Therefore, the range function generates a sequence of numbers from 1 to 19. Then, the
variable “j” takes the value of the item inside the sequence individually and prints it
repeatedly.
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Example 3:
If we want to create a sequence of numbers from 1 to 20 that means iteration starts from 1,
and ends at 20 but increments by 2 instead of 1, so we write and print that:
for k in range(1,20,2):
print(k)
68
Output:
1 3 5 7 9 11 13 15 17 19
So, we observe that iteration is incremented by two steps. As a result, 2, 4, 6… value was
not shown in this sequence.
From above syntaxes, we regard that the total number of items in “list_item” variable has
been determined by “len” function which returns the length of a string or items. Then, the
values of length are inputted into “range” function. As a result, the range function creates
index values of list items. After that, “for” loop with variable “i” execute individual items
of the list gradually.
Output:
So, items on the list are printed individually. However, we can get the same result from the
list of data without using “len” and “range” functions. Consequently, we can write the
syntax again:
list_item = ['jalal','Ripa', 2, 4, 6]
69
for i in list_item:
print(i)
From syntaxes, we observed that every item of “list_item” variable (list of data) has been
passed through “for” loop as the variable “i” item. So, we need to run these commands by
“print” function, and the final result will be as same as before.
print(i)
Here, “Jalal” is considered a string. The variable “i” receives the character directly from
the string. Since this loop does not need the index value of each character, this loop format
is very simple and preferred.
Output:
From this result, we can understand that all characters of string are printed individually
through “for” loop.
Output:
We can access key and value pairs of all the items in the dictionary. To conduct this
operation, “for” loop and “items()” functions of the dictionary will be used here. However,
the “items()” method that returns a representation of the dictionary's contents as tuples. To
iterate over the keys and values of “mydict” dictionary, we just need to ‘unpack’ the two
items embedded in the tuple as shown below:
[Note: all values are converted to strings by “str()” function.]
for key, value in mydict.items():
print(key + " : " + str(value))
Output:
71
So, we have understood how we can easily print all keys and values of a dictionary by
using “for” loop with ‘items()’ function.
for i in list_item1:
for j in list_item2:
print(i,j)
From above syntaxes, we observe that we have two lists of data. “list_item1” consider an
outer loop, and “list_item2” consider an inner loop.
The outer ‘for’ loop have a list of item (list_item1) to iterate over the four items.
The inner ‘for’ loop will execute four times for each outer number
In the body of the inner loop, we will print the multiplication of the outer string
and inner string.
The inner loop is nothing but a body of an outer loop.
Output:
72
Another syntax of using a nested ‘for’ loop in Python:
a = [1,2,3,4,5]
b = [1,2,3,4,5]
for i in a:
for j in b:
c = i**2 + j**2
print(c)
From above syntaxes, we perceive that we have one inner loop but assign a new variable
“c” with a new formula. So,
The outer ‘for’ loop uses variable “a” (list of integers) to iterate over the one to
five numbers.
The inner ‘for’ loop will execute five times for each outer number
In the body of the inner loop, we will print the multiplication of the square of the
outer number and the square of an inner number
Output:
73
In python, the list “append()” method is used for appending and adding elements to the end
of the list. Consequently, the syntaxes for adding of result to the list are:
a = [1,2,3,4,5]
b = [1,2,3,4,5]
result = []
for i in a:
for j in b:
c = i**2 + j**2
result.append(c)
print(result)
Here, we have assigned a new variable of the list named “result”. Consequently, the final
output of loop iteration will be added to the list variable (result) by “append()” function.
Output:
else:
print("i is no longer less than 10")
74
From above syntaxes, we observe that this looping operation will be continued until 9 and
increments of number are 1 (i +=1). But when the increased number is greater than or equal
to 10, the condition is not satisfied and executed to the else condition.
Output:
2. Conditional Statements
The hard situations come in real life when we need to make some decisions and based on
these decisions we decide what we should do next. Similar scenarios exist in programming
when we must make decisions and then execute the following block of code depending on
those decisions. Decision-making statements in programming languages decide the
direction of the flow of program execution.
2.1. ‘if’ statement
The ‘if’ statement is used to decide whether a certain statement or block of statements will
be executed or not.
2.2. ‘if-else’
The ‘else’ statement with ‘if’ statement is used to execute a block of code when the
condition is false.
2.3. ‘if-elif-else’
The following example shows the application of the ‘if-elif-else’ statement.
a = 10
if a > 0:
print("a is the positive number")
elif a <0:
75
print("a is the negative number")
else:
print("a is equal zero")
From these syntaxes, we realize that we have a variable with assigned values 10 (a=10).
When we run it, we will see that “a is the positive number” because the value of the variable
satisfied the first condition and execute its output. Otherwise, it will check below
mentioned conditions and execute its result, respectively.
Output:
if a > 0:
print("a is the positive number")
if a>5: # nested if
print("a is the positive number")
Hence, we have an assigned value of 10 with variable “a”. At first, if the value of the
variable is satisfied with the first condition, then it will execute otherwise not. After
satisfying the first condition, it will check the second condition. If the second condition is
satisfied, it will execute its output otherwise not. Here, we observe that the value of the
variable satisfies the first condition and executes its output, and then it also satisfies the
nested condition and executes its output.
Output:
76
3. Break and continue statements in Python
Loops are used to automate and repeat processes more effectively. However, there may be
occasions when you wish to exit the loop entirely, skip an iteration, or ignore the condition
altogether. Loop control statements are useful for this. Loop control statements change
execution from their normal sequence. Python provides break and continues statements to
handle such situations and to have good control of your loop.
for i in list_item1:
print(i)
if i == 'Ripa':
break
Output:
If we want to stop the iteration before “Ripa”, we need to change the placement of the print
function. Under the loop, we need to place conditions with break statements, then have to
place print function.
77
list_item1 = ['Jalal','Ripa', 'Jara']
for i in list_item1:
if i == 'Ripa':
break
print(i)
Output:
for i in list_item1:
if i == 'Ripa':
continue
print(i)
Output:
Jalal
78
Jara
Example 2:
Considering the situation in which we need to develop a program that prints numbers from
1 to 10 but not 6. It is mentioned that you must do this with a loop and that you may only
use one loop at a time.
We can execute a loop from 1 to 10 and compare the value of the iterator with 6 at each
step. If it equals 6, we will use the continue statement to skip to the next iteration without
publishing anything else; otherwise, we will print the value.
The implementation of the aforesaid concept is shown below:
i=1
if i == 6:
break
i += 1 # increments by 1
Output:
1
2
3
4
5
6
79
Chapter 7: Functions in Python
1. Functions in Python
Python functions are a collection of related statements that execute a computational,
logical, or evaluative activity. The idea is to aggregate together some commonly or
repeatedly done tasks and create a function, rather than writing the same code over and
over for different inputs — we can call the function and reuse the code contained within it.
Functions can be both built-in and user-defined. It helps the program to be concise, non-
repetitive, and organized. For more information about the built-in function can be found
online at https://docs.python.org/3/library/functions.html.
"""function description"""
statement(s)
return expression
80
1.4. The return expression
The function return expression is used to leave a function and return the provided value or
data item to the function caller.
return [expression_list]
"""
sum_data = 0
for i in num:
sum_data = sum_data + i
avg_data = sum_data / len(num)
return avg_data
The name of this above-mentioned defined function is “avg”. So, we can call the function
“avg”. The descriptions of the function mentioned how we can calculate the average value
of data. In the section of statements, step-by-step calculation procedures is added. Here,
we have assigned a variable “sum_data”, where the summation of all data will be assigned.
Then, the loop plays an important role in the iteration of individual values from inputted
values and is added with the previous value. In addition, variable “i” represent individual
81
inputted value. After summation, all values will be divided by counting the total inputted
numbers with the help of “len()” function. After completion of the division, the final result
will be displayed. Return is the last part of the defined function which will leave the
program by giving the output. The implementation of this defined function is given below:
data = [1, 2, 3, 4, 5, 6]
avg_cal = avg(data)
print(avg_cal)
Here, a variable assigned named “data” with a list of numbers and the average of these
numbers would be calculated by using the predefined function “avg()”.
Output:
3.5
Now, we crosscheck this averaged value with the built-in function of Python. To determine
the average value, first, we need to import the library of the “mean” function from the
“statistics” module. The calculation procedures are given below:
from statistics import mean
avg_cal = mean(data)
print(avg_cal)
Here, the built-in “mean” function is used to calculate the average value of provided data.
The output result of the built-in function and defined function are equal.
Output:
3.5
Parameters
----------
u : int or float
v : int or float
Returns
------
speed : int or float
"""
z = (u*u) + (v*v)
import numpy as np
speed = np.sqrt(z)
return speed
Here, the name of the defined function is “windspeed”. From the function description, we
observe the types of parameters (u and v) are being used here. Variable “z” is assigned with
the formula of (u2+v2). After that, speed would be executed by square root over the “z”
variable. Then, the function “return” will leave the function and return the provided value.
If we install “MetPy” library, we do not need to develop a function. We can install this
library by running the code: “pip install metpy --user”. More information can be found
online at
83
https://unidata.github.io/MetPy/latest/userguide/installguide.html and
https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.wind_speed.html.
Now, we can use the built-in function “wind_speed” for determining wind speed. For your
kind information, the source codes of the “wind_speed” function are given below:
import numpy as np
Parameters
----------
u : `pint.Quantity`
Wind component in the X (East-West) direction
v : `pint.Quantity`
Wind component in the Y (North-South) direction
Returns
-------
wind speed: `pint.Quantity`
The speed of the wind
See Also
--------
wind_components
"""
speed = np.sqrt(u * u + v * v)
return speed
Below is the implementation of the defined and built-in function of wind speed calculation.
84
At first, we will use a defined function named “windspeed” for the determination of wind
speed. After that, we will utilize the built-in function.
U = 10
V = 20
winspd = windspeed(U,V)
print(winspd)
Here, U and V variables are assigned with a scalar value of wind speed. After inputting the
values of variables into a defined function by calling “windspeed()”, the final output is
mentioned below.
Output:
22.360679774997898
The same result will also be executed by the implementation of the built-in function
“wind_speed()”. So, the codes are given below:
winspd = wind_speed(U,V)
print(winspd)
Output:
22.360679774997898
So, we observe that the final results of wind speed are equal in defined and built-in
functions. However, what can we do if the provided data is a vector? Previous calculation
of wind speed was conducted on scalar data. We have mentioned below how to measure
the wind speed of vector data by calling a defined function.
data1 = [1, 2, 3, 4, 5, 6]
data2 = [1, 2, 3, 4, 5, 6]
wspeed = []
for i in data1:
85
for j in data2:
winspd = windspeed(i,j)
wspeed.append(winspd)
Here, we have two lists of vector data with assigned variables such as data1 and data2. To
save the output result, we needed to take a new variable named “wspeed” which is a list
type but empty. For interpreting all data in the calculation, we have assigned “data1” in the
outer “for” loop and “data2” in the inner “for” loop. Variable “i” will take individual value
from data1 and variable “j” will take individual value from data2. Then, the values of “i”
and “j” will be put into a defined function named “winspeed()”. The output of this
calculation will be added to a variable named “wspeed” which is the list type.
Output:
86
Chapter 8: Data analysis with Pandas
[N.B. ‘Numpy’ should be imported for array management and ‘pandas’ for excel, .csv files,
and similar types of data handling and management.]
We get six values from 0 to 5 as Python is zero-based programming language. After that,
we can create a list of ‘x’ by the following commands using ‘list()’ function/ using square
brackets:
y = list(x)
Output:
87
A series can be formed by ‘Series()’ function of the pandas library. The series for ‘y’ list
can be created by the given command:
ser = pd.Series(y)
print(ser)
Output:
[N.B. The first column is for the line/ row numbers and the second column is for the created
series.]
Now, we can give a name instead of the row/line numbers by the ‘index’ argument in
‘Series()’ function:
ser1 = pd.Series(y, index = ["Jalal", "Babul", "Tamim", "Emdad", "Hazera", "Nazia"])
print(ser1)
Output:
print(ser1)
88
Output:
"Research Society" : {
"participants level":"all",
"year" : 2020
},
"IMSA" : {
"participants level":"all",
"year" : 2020
},
89
"course name" : "Statistics",
"participants level":"all",
"year" : 2020
print(training)
[N.B. Three dictionaries named ‘Research Society’, ‘IMSA’, and ‘CUSS and RS’ contain
several courses’ names, the level of participants eligible for the courses, and the year of
occurrence.]
Output:
We can make a data frame of the ‘training’ dictionary (nested) by running the following
commands using the ‘DataFrame()’ function of the pandas library:
df = pd.DataFrame(training)
df
Output:
90
"participants level":["Undergrade","Masters","PhD"],
print(Research_Society)
[N.B. The dictionary named ‘Research_Society’ contains several courses’ names provided,
and the level of participants eligible for the respective courses.]
Output:
We can make a data frame for the ‘Research_Society’ dictionary by running the following
commands using the ‘DataFrame()’ function of the pandas library:
df1 = pd.DataFrame(Research_Society)
df1
Output:
Now, we can give the name of the row/line numbers of the data frame by the ‘index’
argument:
df1 = pd.DataFrame(Research_Society,index = [2019,2020,2021])
df1
Output:
91
We can make a tuple consisting of two lists with numeric and string variables by the
following commands:
data2 = ([1,2,3,4],["A","B","C","D"])
data2
Output:
data2
Output:
We can also add names for the rows and columns using the ‘index’ and ‘columns’ argument
in DataFrame() function for a new tuple named ‘data3’:
data3 = ([1,2,3,4],["A","B","C","D"])
data3
data
The output will contain the column's name as students’ names, and rows will be indexed
as rank and grade of the students, respectively.
Output:
92
This data frame can be saved as excel or .csv files by using the ‘to_excel()’, and ‘to_csv()’
commands:
data3.to_excel("class_pandas.xlsx")
data3.to_csv("class_pandas.csv")
We can open an excel or .csv file using the ‘read_excel()’ or ‘read_csv()’ function of the
pandas library by using the following commands:
import pandas as pd
import numpy as np
data = pd.read_excel("data.xlsx")
data
We will see a dataset in the output consisting of a year, pet (potential evapotranspiration),
and di for climatic water balance monitoring. By default, Python prints only the first sheet
of excel/ .csv files after using the ‘read_excel()’ or ‘read_csv()’ functions from the pandas
library.
93
We need to use the ‘sheet_name’ argument for importing a particular sheet other than the
first one from excel or .csv file. In that case, we can imitate the following commands to
print a specific sheet of excel or .csv file:
data1 = pd.read_excel("data.xlsx",sheet_name = "pet")
data1
Output:
We can observe the information of the only first five rows of the dataset by using the
‘head()’ function:
94
data1.head()
Output:
The size (row and column) numbers of the dataset can also be obtained by using the ‘shape’
function:
data1.shape
The output will show the shape of the dataset with row and column numbers.
Output:
95
1.4. Use of .loc and .iloc functions
We can use ‘.loc[]’ and ‘.iloc[]’ functions to access data in any dataset. .loc (location)
function locates data by the index name (i.e., selecting by label) and .iloc(index location)
function locates any data by numerical index (i.e., selecting by position). We can print only
the first six years' ‘pet’ values by using the ‘.loc’ function or by providing the labels of the
rows and columns:
data1.loc[0:5,['year','pet']]
The output will provide the first six ‘year’, and ‘pet’ values, where ‘0:5’ means rows 1 to
6.
Output:
On the other hand, we can use the ‘.iloc’ function to locate the data using numerical index
values:
data1.iloc[1]
The output will print the value of the second row and second column, as python is 0 based
language.
Output:
We can get the value of the second column of the first row by the following code:
data1.iloc[0,1]
96
[N.B. Here, the command can be expressed as: dataset.iloc[row or index number, column
number]
The output will give only the value of the second column in the first row.
Output:
We can access a range of datasets by using the slicing operator [:] inside the ‘.iloc’ function:
data1.iloc[0:6,0:6]
[N.B. Here, the command can be expressed as: dataset.iloc[row or index range, column
range]]
The output will print the first six-row and column values from the dataset.
Output:
new_data.head()
We can merge ‘data’ and ‘data1’ where data has two common columns ‘year’, and ‘pet’ as
data1. But data has one new column named ‘di’ than data1. So, the new dataset will merge
and consist of three columns named ‘year’, ‘pet’, and ‘di’. We can observe the first five
rows of the ‘new_data’ using the ‘head()’ function.
Output:
97
1.6. Data concatenation
We can use ‘concat()’ function of the pandas' library to combine two datasets. However,
we need to make a list of the datasets to be combined inside the ‘concat()’ function using
the indexing operator (square bracket):
new_data1 = pd.concat([data,data1])
new_data1.head()
The output will give us the results in Boolean values (False, or True).
98
Output:
We can remove the duplicate values found using the ‘drop_duplicates()’ function:
data.drop_duplicates(subset=['pet','di'])
The output will print the dataset after removing the duplicate values.
Output:
[N.B. There was no duplicate value in the ‘data’ dataset. Hence, the original dataset was
printed in the output after using the ‘drop_duplicates()’ function.]
99
Chapter 9: Data visualization in Python – temporal & time series plots
[N.B. ‘Pandas’ library should be imported for excel, .csv files, and similar types of data
handling and management. ‘Pyplot’ module of the ‘Matplotlib’ library is required for data
visualization and plotting.]
We can open the dataset ‘data.xlsx’ file using the ‘read_excel()’ function of the pandas'
library by using the following commands:
data = pd.read_excel("data.xlsx", sheet_name = "pet")
data.head()
We will see a dataset in the output consisting of a year, pet (potential evapotranspiration)
values for climatic water balance monitoring. By default, python prints only the first sheet
of excel/ .csv files after using the ‘read_excel()’ or ‘read_csv()’ functions from the pandas'
library. However, we need to use the ‘sheet_name’ argument for importing a particular
sheet other than the first one from excel or .csv file. We can also observe the information
of the only first five rows of the dataset by using the ‘head()’ function.
The output will print the first five rows of the sheet named ‘pet’ from the ‘data.xlsx’ excel
file.
Output:
100
1.1. Line chart and time series plot
Any dataset relating to time (e.g., second, minute, half-hourly, hourly, weekly, monthly,
and annually, etc.) can be called time-series data. Line charts can be used to visualize the
time series data. We can create a line chart of the ‘year’ and ‘pet’ variables in the ‘data.xlsx’
dataset by using the ‘plot()’ function from the ‘pyplot’ module of the ‘matplotlib’ library:
plt.plot('year', 'pet', data=data, color = 'red')
We can also create a similar type of line chart with blue circle markers by using the
following commands:
plt.plot(data['year'],data['pet'],'b--o')
101
Figure 9.2. Line chart with markers
We can add line width and marker size with the previous command:
plt.plot(data['year'],data['pet'],'b--o', linewidth=2, markersize=10)
Output:
Figure 9.3. Line chart with line width and marker size modifications
The 'b--o' code can be written in detail as the following command to get a similar line chart
with green line colour instead of blue:
plt.plot(data['year'],data['pet'], color='green', marker='o', linestyle='dashed', linewidth=2,
markersize=10)
Output:
102
Figure 9.4. Line chart with modifications in colour
We will open the ‘pet_di’ sheet of the dataset ‘data.xlsx’ using the ‘sheet_name’ argument
in the ‘read_excel()’ function of the ‘pandas’ library with the following commands:
data1 = pd.read_excel("data.xlsx", sheet_name = "pet_di")
data1.head()
If we want to plot both ‘pet’ and ‘di’ variables in the y-axis against the ‘year’ in the x-axis,
we can run the following code:
plt.plot(data1['year'],data1['pet'], 'b--^', data1['year'],data1['di'], 'r-o')
103
Figure 9.5. Line charts with two variables
We need to determine the width and height of the figures (in inches) before plotting and
exporting. We have used a minimum figure dpi (80) for plotting. However, maximum
journals require a standard figure resolution of 300 dpi. So, we can save the figures in high
a resolution (300 dpi) by the following commands:
plt.figure(figsize=(6,4), dpi= 80)
plt.xlabel("Year")
plt.ylabel("PET (mm)")
plt.title("Yearly PET")
plt.savefig('Yearly_pet.png',dpi=300)
Before saving the plot, we added the name of the x and y axes by using ‘xlabel()’ and
‘ylabel()’ functions. ‘title()’ function has been used for providing a title for the plot.
Output:
[N.B. The following websites can provide more information regarding plots and figures
with ‘matplotlib.pyplot’ library in python:
104
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html ]
pet_di.head()
Output:
We can get a scatter plot by placing both ‘pet’ and ‘di’ variables in the y-axis against the
‘year’ in the x-axis using the ‘scatter()’ function of the ‘matplotlib.pyplot’ library by
running the following code:
plt.scatter(pet_di['year'], pet_di['pet'], color='green', label = 'PET')
plt.xlabel("Year")
plt.legend()
[N.B. The command for bar plotting with labels can be expressed as follows –
plt.bar(dataset(‘xvariable1’), dataset(‘yvariable1’), color = ‘name’, label = ‘label’
105
plt.bar(dataset(‘xvariable1’), dataset(‘yvariable2’), color = ‘name’, label = ‘label’
]
Output:
mark = [95,90,85,70,65]
plt.bar(student_name,mark)
plt.xlabel('Name of students')
plt.ylabel('Exam score')
plt.show()
Output:
106
Figure 9.8. Single bar plot
x = np.arange(4)
107
plt.legend()
[N.B. The commands for adding ticks in the middle position for each group or year in the
x-axis can be expressed as follows:
plt.xticks([group position number 1+ 0.25, group position number 2 + 0.25, group
position number 3 + 0.25, group position number 4 + 0.25],[label1, label2, label3,
label4])
Here, 0.25 after the position number in the ticks defines the positions of the ticks in each
group.]
Output:
plt.bar(x, Mathematics, color = 'r', width = 0.25, bottom = English, label = 'Mathematics')
108
plt.legend()
[N.B. The commands for adding ticks in the middle position for each group or year in the
x-axis can be expressed as follows:
plt.xticks([group position number 1, group position number 2, group position number 3,
group position number 4],[label1, label2, label3, label4])
Or simply,
plt.xticks([ticks],[labels]) ]
Output:
1.6. Histogram
The histogram is used mostly for statistical analysis purposes, which require bins,
cumulative, probability density function, frequency distribution etc. We can make a list of
scores for five participants and then create a histogram using the ‘hist()’ function and by
providing some specific bins using the ‘bins’ argument with the following commands:
score = [60, 75, 80, 30,17, 20, 50, 45,70, 50, 75, 30]
plt.xlabel('Exam score')
plt.ylabel('Number of participants')
109
[N.B. Here, the command for ticks’ position in the y-axis can be expressed as:
plt.yticks([ticks’ positions], [labels]) ]
Output:
mark = [95,90,85,70,65]
plt.show()
[N.B. Here, the command for pie chart along with the labelling of each proportionate
percentage inside each respective segment can be expressed as:
plt.pie(data, labels = label_name, autopct='fmt%pct')
Here, ‘fmt’ is equal to formatting (like % % as value), ‘.1’ indicates one decimal value will
be displayed and % indicates the symbol of percentage as a string.]
Output:
110
Figure 9.12. Pie chart
1.8. Subplot
Subplots are also known as panel plots where we can make a figure consisting of more than
one plot. Subplots can be created using the ‘subplot()’ function with four arguments as
‘subplot(nrows, ncols, index, **kwargs)’, where nrows, ncols and index are the number of
rows, number of columns, and position of the plot, respectively. By default, the arguments
are taken as (1, 1, 1) in python. The index starts at 1 in the upper left corner and increases
to the right. We can make two subplots of ‘year’ vs ‘pet’ and ‘year’ vs ‘di’ from the
‘data.xlsx’ dataset by the following commands:
plt.subplot(211)
plt.subplot(212)
111
Figure 9.13. Subplots
These same subplots can be created by using some alternative commands as follows:
fig, (ax1, ax2) = plt.subplots(2, 1)
fig.subplots_adjust(hspace=0.5)
linewidth=2, markersize=10)
ax1.set_xlabel('Year')
ax1.set_ylabel('PET (mm)')
linewidth=2, markersize=10)
ax2.set_xlabel('Year')
ax2.set_ylabel('DI (mm)')
plt.savefig("PET_DI.png",dpi=300)
plt.show()
112
fig.subplots_adjust(hspace=0.5)
This command is to make a little extra horizontal space between the subplots.]
Output:
We need to visualize sometimes two variables on the y-axis against the x-axis variable in
the same plot. However, the example in the line charts and scatter plot was not
comprehensible enough. Variables with different ranges or different units can be displayed
against one variable (x-axis) in the same plot using both the left and right y-axis of the plot.
In that case, we can use ‘twinx()’ function to make and return a second axis that share the
x-axis:
fig, ax1 = plt.subplots(1, 1, figsize=(10,4), dpi= 80)
ax1.set_xlabel('Year')
ax1.set_ylabel('PET (mm)')
ax1.legend(loc='upper center')
ax2 = ax1.twinx()
ax2.set_ylabel('DI (mm)')
ax2.legend(loc='upper right')
fig.tight_layout()
plt.savefig("secondary_Y_axis.png",dpi=300)
plt.show()
[N.B. Here, the command for secondary axis subplots can be expressed as:
ax2 = ax1.twinx()
The command is for plot line 2 on the right y-axis. This means a secondary y-axis will be
created alongside the primary y-axis against the same x-axis.
The following websites can provide more information regarding subplots with
‘matplotlib.pyplot’ library in python:
https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.subplot.html ]
The output will give a subplot with the right and left y-axes of two variables against the x-
axis variable (primary and secondary axes).
Output:
114
and Y. We can open a dataset named ‘MSW.xlsx’ which contains three-dimensional data
with latitude (°N), longitude (°E), and maximum sustained wind speed (m s-1). We can
import the necessary libraries and the dataset by the following commands:
import pandas as pd
import numpy as np
msw = pd.read_excel("MSW.xlsx")
msw.head()
We can observe the first five rows of the dataset in the output as follows.
Output:
The shape of the dataset (rows and column numbers) can be observed by the following
command:
msw.shape
Output:
Before contour plotting, the dataset variables need to be converted into two dimensions
using ‘meshgrid()’ function:
latitude = msw['Latitude (°N)']
115
lat, lon = np.meshgrid(latitude, longitude)
plt.colorbar()
plt.xlabel('longitude')
plt.ylabel('latitude')
plt.xlim(140,152)
plt.ylim(8,36)
[N.B. Here, the command for contour plotting can be expressed as:
plt.contourf(X, Y, Z)
At first, ‘latitude’ and ‘longitude’ will be converted to two-dimension. Then, ‘MSW’ will
also be converted into 2D using ‘meshgrid()’ function from ‘numpy’ library. ‘Numpy’
library is required for array management.]
The output will also contain the color bar, xlabel, ylabel, x-axis limit, and y-axis limit as
per the above-mentioned commands.
Output:
116
argument in ‘plt.axes()’ function. Projection should be entered as ‘3d’ in ‘plt.axes()’
function:
from mpl_toolkits import mplot3d
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_xlabel('longitude')
ax.set_ylabel('latitude')
plt.show()
[N.B. Here, the command for 3D contour plotting can be expressed as:
ax.contour3D(lon, lat, MSW_ms, 50, cmap='viridis')
Where, lon, lat, MSW_ms = X, Y, Z. ]
Output:
117
Chapter 10: Mapping in Python with Cartopy, Xarray, and NetCDF4
Install netcdf4:
Step 1: run Anaconda as Administrator
Step 2: we need to use the following command if we use an anaconda
conda install -c anaconda netcdf4
Install cartopy:
Step 1: run Anaconda as Administrator
Step 2: we need to use the following command if we use an anaconda
conda install -c conda-forge cartopy
Install xarray:
Step 1: run Anaconda as Administrator
Step 2: we need to use the following command if we use an anaconda
conda install -c anaconda xarray
After installation the abovementioned packages, we need to open the Jupyter Notebook
and follow the given steps:
The following commands are for importing necessary libraries:
118
[N.B. ‘Dataset’ from netcdf4 and ‘xarray’ library will be required to read the NetCDF (.nc)
data and variables. ‘Pandas’ library should be imported for excel, .csv files, and similar
types of data handling and management. ‘Pyplot’ module of the ‘Matplotlib’ library is
required for data visualization and plotting. ‘Numpy’ should be imported for array
management. The ‘Netcdf’ module from ‘scipy.io’ library can also be used for NetCDF
(.nc file) data management, e.g. masking the data. We will require ‘cartopy.crs’ library for
mapping with appropriate projection system and geospatial analyses plotting, where crs
means ‘Coordinate Reference Systems’.]
print(data.variables.keys())
The ‘Dataset’ module read the dataset as a dictionary, consisting of keys and variables.
Hence, we can print the variables’ names by the above-mentioned codes.
The output will print the short names for the variables as the keys of the dictionary.
Output:
We can use a loop to print the details about all the variables of the dataset:
for var in data.variables.values():
print(var)
Output:
119
[N.B. The dataset can be described as follows:
The long name for ‘gwgt’ is ‘gaussian weights’ with no units and a dimension
of 64
The long name for ‘lat’ is ‘latitude’ with ‘degrees_north’ units and a
dimension of 64
120
The long name for ‘lon’ is ‘longitude’ with ‘degrees_east’ units and a
dimension of 128
The long name for ‘time’ is ‘Year-Month’ with ‘yyyymm’ units and a
dimension of 216
The long name for ‘prc’ is ‘Precipitation’ with ‘mm/day’ units and three
dimensions of (time, lat, lon) or (216, 64, 128)
Here, ‘Precipitation (prc)’ is the main variable which consists of three-dimensional data
with time (18 years), latitude and longitude. ]
We can also use ‘xarray’ library to print our dataset by using the ‘open_dataset()’ function:
ds = xr.open_dataset('precipitation.nc')
ds
Output:
121
The output will give details information about only one ‘prc’ variable of the
‘precipitation.nc’ dataset.
Output:
1.3. How to know about the minimum and maximum latitude and longitude for mapping
We need to know the maximum minimum latitude and longitude of the variables in the
dataset before further analysis. Hence, we can follow the given codes:
print(“min_lat = ”, min(data.variables[‘lat’]))
print(“max_lat = ”, max(data.variables[‘lat’]))
print(“min_lon = ”, min(data.variables[‘lon’]))
print(“max_lon = ”, max(data.variables[‘lon’]))
Output:
[N.B. The dataset covers the global data where latitude limits are from -87.86 to + 87.86
(around 90°) and longitude ranges from 0 to 357.19 (around 360°). Hence, this represents
a global dataset.]
We can access all the data from each variable using indexing and slicing operators as ‘[:]’.
Now, we need to convert the ‘prc’ variable into two-dimension (2D) over time for mapping.
Therefore, we will average our precipitation data over the ‘time’ variable (axis = 0) for 2D
conversion:
lat = data.variables[‘lat’][:]
122
lon = data.variables[‘lon’][:]
time = data.variables[‘time’][:]
prc = data.variables[‘prc’][:]
print(mean_rainfall)
Output:
plt.xlim(60,100)
plt.ylim(5,30)
plt.xlabel('Longitude')
plt.ylabel('Latitude')
prc = dataset.variables['prc'][0, :, :]
lats = dataset.variables['lat'][:]
lons = dataset.variables['lon'][:]
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
124
ax.gridlines(draw_labels=True)
plt.show()
Output:
We can get a regional map by using ‘set_extent()’ function along with the previously
mentioned codes as follows:
dataset = netcdf.netcdf_file('precipitation.nc', maskandscale = True, mmap =
False)
prc = dataset.variables['prc'][0, :, :]
lats = dataset.variables['lat'][:]
lons = dataset.variables['lon'][:]
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
ax.gridlines(draw_labels=True)
125
plt.show()
[N.B. The commands for the ‘set_extent()’ function to get a regional map can be expressed
as follows:
ax.set_extent([x0, x1, y0, y1])
Here, x0 and x1 are the lower and upper limit of longitude, and yo, y1 are the lower and
upper limit of the latitude range. ]
The regional map will be visualized in the output.
Output:
[N.B. The following websites can provide more information regarding transform and
projection keywords with lists:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
https://scitools.org.uk/cartopy/docs/v0.15/crs/projections.html ]
ax = plt.axes(projection=ccrs.PlateCarree())
126
plot = plt.contourf(lons, lats, prc, cmap = 'YlOrRd', transform = ccrs.PlateCarree())
ax.coastlines()
gl = ax.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.LAND, edgecolor='black')
ax.add_feature(cartopy.feature.LAKES, edgecolor='black')
ax.add_feature(cartopy.feature.RIVERS)
plt.show()
[N.B. Here, the command for extra features like color bar and gridline label can be
expressed as:
plt.colorbar(plot, ax=ax, shrink=0.8)
gl.top_labels = False #for removing top gridline labels
gl.right_labels = False #for removing top gridline labels ]
The output of the regional map with color bar, ocean, land, lakes, rivers features and no
top-right gridline labels will be as follows.
Output:
127
Figure 10.4. Regional map with modifications using cartopy
The same features can be added by using ‘xarray’ library with the following similar codes:
ds = xr.open_dataset('precipitation.nc')
prc = ds.prc[0, : , : ]
ax1.coastlines()
gl = ax1.gridlines(draw_labels=True)
gl.top_labels = False
gl.right_labels = False
gl.right_labels = False
plt.title('')
plt.show()
The output of the regional map with color bar, coastline features and no top-right gridline
labels using ‘xarray’ library will be as follows.
128
Output:
[N.B. Here, the commands for extra features like color bar and removing gridline labels
are quite similar to the codes used in ‘cartopy’ library to get those features:
prc.plot(ax=ax1, cmap = 'YlOrRd', transform=ccrs.PlateCarree(), cbar_kwargs={'shrink':
0.8}
The following websites can provide more information regarding adding features while
spatial mapping in python:
https://rabernat.github.io/research_computing_2018/maps-with-cartopy.html ]
129