Python Unit 4
Python Unit 4
Python Unit 4
Live Demo
#!/usr/bin/python
• raw_input
• input
#!/usr/bin/python
This prompts you to enter any string and it would display same string on the screen. When I typed "Hello
Python!", its output is like this −
237
#!/usr/bin/python
This would produce the following result against the entered input −
Python provides basic functions and methods necessary to manipulate files by default. You can do most of the
file manipulation using a file object.
Syntax
• file_name − The file_name argument is a string value that contains the name of the file that you want
to access.
• access_mode − The access_mode determines the mode in which the file has to be opened, i.e., read,
write, append, etc. A complete list of possible values is given below in the table. This is optional
parameter and the default file access mode is read (r).
• buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line
buffering is performed while accessing a file. If you specify the buffering value as an integer greater
than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is
the system default(default behavior).
r
1 Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default
mode.
rb
2 Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file.
This is the default mode.
238
r+
3
Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+
4 Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of
the file.
w
5 Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a
new file for writing.
wb
6 Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not
exist, creates a new file for writing.
w+
7 Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does
not exist, creates a new file for reading and writing.
wb+
8 Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists.
If the file does not exist, creates a new file for reading and writing.
a
9 Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is
in the append mode. If the file does not exist, it creates a new file for writing.
ab
10 Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists.
That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+
Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists.
11
The file opens in the append mode. If the file does not exist, it creates a new file for reading and
writing.
ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of the file
12
if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for
reading and writing.
file.closed
1
Returns true if file is closed, false otherwise.
239
file.mode
2
Returns access mode with which file was opened.
file.name
3
Returns name of the file.
file.softspace
4
Returns false if space explicitly required with print, true otherwise.
Example
Live Demo
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good
practice to use the close() method to close a file.
Syntax
fileObject.close()
Example
Live Demo
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
240
# Close opend file
fo.close()
The write() method does not add a newline character ('\n') to the end of the string −
Syntax
fileObject.write(string)
Here, passed parameter is the content to be written into the opened file.
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n")
The above method would create foo.txt file and would write given content in that file and finally it would close
that file. If you would open this file, it would have following content.
Syntax
241
fileObject.read([count])
Here, passed parameter is the number of bytes to be read from the opened file. This method starts reading
from the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until the
end of file.
Example
Let's take a file foo.txt, which we created above.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()
File Positions
The tell() method tells you the current position within the file; in other words, the next read or write will occur
at that many bytes from the beginning of the file.
The seek(offset[, from]) method changes the current file position. The offset argument indicates the number
of bytes to be moved. The from argument specifies the reference position from where the bytes are to be
moved.
If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current
position as the reference position and if it is set to 2 then the end of the file would be taken as the reference
position.
Example
Let us take a file foo.txt, which we created above.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10)
print "Read String is : ", str
242
# Reposition pointer at the beginning once again
position = fo.seek(0, 0);
str = fo.read(10)
print "Again read String is : ", str
# Close opend file
fo.close()
To use this module you need to import it first and then you can call any related functions.
Syntax
os.rename(current_file_name, new_file_name)
Example
Following is the example to rename an existing file test1.txt −
#!/usr/bin/python
import os
Syntax
os.remove(file_name)
Example
243
Following is the example to delete an existing file test2.txt −
#!/usr/bin/python
import os
Directories in Python
All files are contained within various directories, and Python has no problem handling these too.
The os module has several methods that help you create, remove, and change directories.
Syntax
os.mkdir("newdir")
Example
Following is the example to create a directory test in the current directory −
#!/usr/bin/python
import os
Syntax
os.chdir("newdir")
Example
Following is the example to go into "/home/newdir" directory −
#!/usr/bin/python
import os
Syntax
os.getcwd()
Example
Following is the example to give current directory −
#!/usr/bin/python
import os
Syntax
os.rmdir('dirname')
Example
Following is the example to remove "/tmp/test" directory. It is required to give fully qualified name of the
directory, otherwise it would search for that directory in the current directory.
#!/usr/bin/python
import os
Examples
# Open a file for writing and insert some records
>>> f = open('test.txt', 'w')
>>> f.write('apple\n')
>>> f.write('orange\n')
>>> f.write('pear\n')
>>> f.close() # Always close the file
# Check the contents of the file created
# Open the file created for reading and read line(s) using readline() and readlines()
>>> f = open('test.txt', 'r')
>>> f.readline() # Read next line into a string
'apple\n'
>>> f.readlines() # Read all (next) lines into a list of strings
['orange\n', 'pear\n']
>>> f.readline() # Return an empty string after EOF
''
>>> f.close()
# Open the file for reading and read the entire file via read()
>>> f = open('test.txt', 'r')
>>> f.read() # Read entire file into a string
'apple\norange\npear\n'
>>> f.close()
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
file_copy: Copy file line-by-line from source to destination
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage: file_copy <src> <dest>
"""
import sys
import os
def main():
# Check and retrieve command-line arguments
if len(sys.argv) != 3:
print(__doc__)
sys.exit(1) # Return a non-zero value to indicate abnormal termination
fileIn = sys.argv[1]
fileOut = sys.argv[2]
if __name__ == '__main__':
247
main()
3. Text Processing
For simple text string operations such as string search and replacement, you can use the built-in string functions
(e.g., str.replace(old, new)). For complex pattern search and replacement, you need to master regular expression (regex).
Uppercase/Lowercase
• s.upper(), s.lower() -> str: Return a copy of string s converted to uppercase and lowercase, respectively.
• s.isupper(), s.islower() -> bool: Check if the string is uppercase/lowercase, respectively.
Find
• s.find(key_str, [start], [end]) -> int|-1: Return the lowest index in slice s[start:end] (default to entire string);
or -1 if not found.
• s.index(key_str, [start], [end]) -> int|ValueError: Similar to find(), but raises ValueError if not found.
• s.startswith(key_str, [start], [end]), s.endsswith(key_str, [start], [end]) -> bool: Check is the string begins
or ends with key_str.
For examples,
>>> s = '/test/in.txt'
>>> s.find('in')
6
>>> s[0 : s.find('in')] + 'out.txt'
'/test/out.txt'
# You could use str.replace() described below
For examples,
>>> s = 'hello hello hello, world'
>>> help(s.replace)
>>> s.replace('ll', '**')
'he**o he**o he**o, world'
>>> s.replace('ll', '**', 2)
'he**o he**o hello, world'
Python Modules
In this tutorial, we will explain how to construct and import custom Python modules. Additionally, we may
import or integrate Python's built-in modules via various methods.
Simplification: A module often concentrates on one comparatively small area of the overall problem instead
of the full task. We will have a more manageable design problem to think about if we are only concentrating
on one module. Program development is now simpler and much less vulnerable to mistakes.u
Flexibility: Modules are frequently used to establish conceptual separations between various problem areas.
It is less likely that changes to one module would influence other portions of the program if modules are
constructed in a fashion that reduces interconnectedness. (We might even be capable of editing a module
despite being familiar with the program beyond it.) It increases the likelihood that a group of numerous
developers will be able to collaborate on a big project.
Reusability: Functions created in a particular module may be readily accessed by different sections of the
assignment (through a suitably established api). As a result, duplicate code is no longer necessary.
Scope: Modules often declare a distinct namespace to prevent identifier clashes in various parts of a program.
In Python, modularization of the code is encouraged through the use of functions, modules, and packages.
249
o Python itself allows for the creation of modules.
o Similar to the re (regular expression) module, a module can be primarily written in C programming
language and then dynamically inserted at run-time.
o A built-in module, such as the itertools module, is inherently included in the interpreter.
We employ modules to divide complicated programs into smaller, more understandable pieces. Modules also
allow for the reuse of code.
Rather than duplicating their definitions into several applications, we may define our most frequently used
functions in a separate module and then import the complete module.
Let's construct a module. Save the file as example_module.py after entering the following.
Example:
1. # Here, we are creating a simple Python program to show how to create a module.
2. # defining a function in the module to reuse it
3. def square( number ):
4. # here, the above function will square the number passed as the input
5. result = number ** 2
6. return result # here, we are returning the result of the function
Here, a module called example_module contains the definition of the function square(). The function returns
the square of a given number.
For this, we make use of the import Python keyword. In the Python window, we add the next to import
keyword, the name of the module we need to import. We will import the module we defined earlier
example_module.
Syntax:
1. import example_module
The functions that we defined in the example_module are not immediately imported into the present program.
Only the name of the module, i.e., example_ module, is imported here.
We may use the dot operator to use the functions using the module name. For instance:
Example:
1. # here, we are calling the module square method and passing the value 4
250
2. result = example_module.square( 4 )
3. print("By using the module square of number is: ", result )
Output:
There are several standard modules for Python. The complete list of Python standard modules is available.
The list can be seen using the help command.
Similar to how we imported our module, a user-defined module, we can use an import statement to import
other standard modules.
Code
1. # Here, we are creating a simple Python program to show how to import a standard module
2. # Here, we are import the math module which is a standard module
3. import math
4. print( "The value of euler's number is", math.e )
5. # here, we are printing the euler's number from the math module
Output:
Code
1. # Here, we are creating a simple Python program to show how to import a module and rename it
2. # Here, we are import the math module and give a different name to it
3. import math as mt # here, we are importing the math module as mt
4. print( "The value of euler's number is", mt.e )
5. # here, we are printing the euler's number from the math module
Output:
251
The math module is now named mt in this program. In some situations, it might help us type faster in case of
modules having long names.
Please take note that now the scope of our program does not include the term math. Thus, mt.pi is the proper
implementation of the module, whereas math.pi is invalid.
Code
1. # Here, we are creating a simple Python program to show how to import specific
2. # objects from a module
3. # Here, we are import euler's number from the math module using the from keyword
4. from math import e
5. # here, the e value represents the euler's number
6. print( "The value of euler's number is", e )
Output:
Only the e constant from the math module was imported in this case.
We avoid using the dot (.) operator in these scenarios. As follows, we may import many attributes at the same
time:
Code
1. # Here, we are creating a simple Python program to show how to import multiple
2. # objects from a module
3. from math import e, tau
4. print( "The value of tau constant is: ", tau )
5. print( "The value of the euler's number is: ", e )
Output:
Syntax:
Code
Output:
The module is initially looked for in the current working directory. Python then explores every directory in
the shell parameter PYTHONPATH if the module cannot be located in the current directory. A list of folders
makes up the environment variable known as PYTHONPATH. Python examines the installation-dependent
set of folders set up when Python is downloaded if that also fails.
Code
Output:
Code
1. # Here, we are creating a simple Python program to print the directory of a module
2. print( "List of functions:\n ", dir( str ), end=", " )
Output:
List of functions:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',
'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Both local and global namespace variables can be accessed by a Python statement. When two variables with
the same name are local and global, the local variable takes the role of the global variable. There is a separate
local namespace for every function. The scoping rule for class methods is the same as for regular functions.
Python determines if parameters are local or global based on reasonable predictions. Any variable that is
allocated a value in a method is regarded as being local.
Therefore, we must use the global statement before we may provide a value to a global variable inside of a
function. Python is informed that Var_Name is a global variable by the line global Var_Name. Python stops
looking for the variable inside the local namespace.
We declare the variable Number, for instance, within the global namespace. Since we provide a Number a
value inside the function, Python considers a Number to be a local variable. UnboundLocalError will be the
outcome if we try to access the value of the local variable without or before declaring it global.
Code
1. Number = 204
2. def AddNumber(): # here, we are defining a function with the name Add Number
3. # Here, we are accessing the global namespace
4. global Number
5. Number = Number + 200
6. print("The number is:", Number)
7. # here, we are printing the number after performing the addition
8. AddNumber() # here, we are calling the function
9. print("The number is:", Number)
254
Output:
Python - Packages
We organize a large number of files in different folders and subfolders based on some criteria, so
that we can find and manage them easily. In the same way, a package in Python takes the concept
of the modular approach to next logical level. As you know, a module can contain multiple objects,
such as classes, functions, etc. A package can contain one or more relevant modules. Physically, a
package is actually a folder containing one or more module files.
greet.py
Copy
def SayHello(name):
print("Hello ", name)
functions.py
Copy
def sum(x,y):
return x+y
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
255
That's it. We have created our package called mypackage. The following is a folder structure:
Package
Folder Structure
Using Packages
Importing a Module from a Package
Now, to test our package, navigate the command prompt to the MyApp folder and invoke the Python
prompt from there.
D:\MyApp>python
Import the functions module from the mypackage package and call its power() function.
>>> functions.power(3,2)
256
>>> from mypackage.functions import sum
>>> sum(10,20)
30
>>> average(10,12)
2.2M
76
Entity Framework Core Quiz
NOW
PLAYING
__init__.py
The package folder contains a special file called __init__.py , which stores the package's content. It
serves two purposes:
1. The Python interpreter recognizes a folder as the package if it contains __init__.py file.
2. __init__.py exposes specified resources from its modules to be imported.
An empty __init__.py file makes all functions from the above modules available when this package
is imported. Note that __init__.py is essential for the folder to be recognized by Python as a package.
You can optionally define functions from individual modules to be made available.
Note:
We shall also create another Python script in the MyApp folder and import the mypackage package in it. It
should be at the same level of the package to be imported.
The __init__.py file is normally kept empty. However, it can also be used to choose specific functions
from modules in the package folder and make them available for import. Modify __init__.py as
below:
__init__.py
Copy
from .functions import average, power
from .greet import SayHello
The specified functions can now be imported in the interpreter session or another executable script.
257
Create test.py in the MyApp folder to test mypackage.
test.py
Copy
from mypackage import power, average, SayHello
SayHello()
x=power(3,2)
print("power(3,2) : ", x)
Note that functions power() and SayHello() are imported from the package and not from their
respective modules, as done earlier. The output of the above script is:
D:\MyApp>python test.py
Hello world
power(3,2) : 9
Save the following code as setup.py in the parent folder MyApp. The script calls the setup() function
from the setuptools module. The setup() function takes various arguments such as name, version,
author, list of dependencies, etc. The zip_safe argument defines whether the package is installed in
compressed mode or regular mode.
Example: setup.py
Copy
from setuptools import setup
setup(name='mypackage',
version='0.1',
description='Testing installation of Package',
url='#',
author='auth',
author_email='author@email.com',
license='MIT',
packages=['mypackage'],
zip_safe=False)
Now execute the following command to install mypackage using the pip utility. Ensure that the
command prompt is in the parent folder, in this case D:\MyApp.
258
D:\MyApp>pip install mypackage
Processing d:\MyApp
Now mypackage is available for system-wide use and can be imported in any script or interpreter.
D:\>python
>>>mypackage.average(10,20)
15.0
>>>mypackage.power(10,2)
100
Arrays in Numpy
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive
integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of integers giving the
size of the array along each dimension is known as shape of the array. An array class in Numpy is called
as ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using
nested Python Lists.
Creating a Numpy Array
Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the Array.
259
Arrays can also be created with the use of various data types such as lists, tuples, etc. The type of the resultant
array is deduced from the type of the elements in the sequences.
Note: Type of array can be explicitly defined while creating the array.
# Python program for
# Creation of Arrays
import numpy as np
# Initial Array
arr = np.array([[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]])
print("Initial Array: ")
print(arr)
260
# Printing a range of Array
# with the use of slicing method
sliced_arr = arr[:2, ::2]
print ("Array with first 2 rows and"
" alternate columns(0 and 2):\n", sliced_arr)
# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3],
[3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
"(1, 2), (0, 1), (3, 0):\n", Index_arr)
Run on IDE
Output:
Initial Array:
[[-1. 2. 0. 4. ]
[ 4. -0.5 6. 0. ]
[ 2.6 0. 7. 8. ]
[ 3. -7. 4. 2. ]]
Array with first 2 rows and alternate columns(0 and 2):
[[-1. 0.]
[ 4. 6.]]
Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
[0. 6. 2. 3.]
# Defining Array 1
a = np.array([[1, 2],
[3, 4]])
# Defining Array 2
b = np.array([[4, 3],
[2, 1]])
261
print ("\nSubtracting 2 from each element:", b - 2)
Array sum:
[[5 5]
[5 5]]
Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive
integers. Every ndarray has an associated data type (dtype) object. This data type object (dtype) provides
information about the layout of the array. The values of an ndarray are stored in a buffer which can be thought
of as a contiguous block of memory bytes which can be interpreted by the dtype object. Numpy provides a large
set of numeric datatypes that can be used to construct arrays. At the time of Array creation, Numpy tries to guess
a datatype, but functions that construct arrays usually also include an optional argument to explicitly specify the
datatype.
# Integer datatype
# guessed by Numpy
x = np.array([1, 2])
print("Integer Datatype: ")
print(x.dtype)
# Float datatype
# guessed by Numpy
x = np.array([1.0, 2.0])
print("\nFloat Datatype: ")
print(x.dtype)
# Forced Datatype
x = np.array([1, 2], dtype = np.int64)
print("\nForcing a Datatype: ")
print(x.dtype)
Run on IDE
Output:
Integer Datatype:
int64
Float Datatype:
float64
Forcing a Datatype:
int64
# First Array
arr1 = np.array([[4, 7], [2, 6]],
dtype = np.float64)
# Second Array
arr2 = np.array([[3, 6], [2, 8]],
dtype = np.float64)
# Transpose of Array
# using In-built function 'T'
Trans_arr = arr1.T
print("\nTranspose of Array: ")
print(Trans_arr)
Run on IDE
Output:
Addition of Two Arrays:
[[ 7. 13.]
[ 4. 14.]]
Transpose of Array:
[[4. 2.]
[7. 6.]]
Methods in Numpy
264
all()
any()
take()
put()
apply_along_axis()
apply_over_axes()
argmin()
argmax()
nanargmin()
nanargmax()
amax()
amin()
insert()
delete()
append()
around()
flip()
fliplr()
flipud()
265
triu()
tril()
tri()
empty()
empty_like()
zeros()
zeros_like()
ones()
ones_like()
full_like()
diag()
diagflat()
diag_indices()
asmatrix()
bmat()
eye()
roll()
identity()
arange()
266
place()
extract()
compress()
rot90()
tile()
reshape()
ravel()
isinf()
isrealobj()
isscalar()
isneginf()
isposinf()
iscomplex()
isnan()
iscomplexobj()
isreal()
isfinite()
isfortran()
exp()
267
exp2()
fix()
hypot()
absolute()
ceil()
floor()
degrees()
radians()
npv()
fv()
pv()
power()
float_power()
log()
log1()
log2()
log10()
dot()
vdot()
268
trunc()
divide()
floor_divide()
true_divide()
random.rand()
random.randn()
ndarray.flat()
expm1()
bincount()
rint()
equal()
not_equal()
less()
less_equal()
greater()
greater_equal()
prod()
square()
cbrt()
269
logical_or()
logical_and()
logical_not()
logical_xor()
array_equal()
array_equiv()
sin()
cos()
tan()
sinh()
cosh()
tanh()
arcsin()
arccos()
arctan()
arctan2()
Python Pandas
270
The term "Pandas" refers to an open-source library for manipulating high-performance data in Python. This
instructional exercise is intended for the two novices and experts.
It was created in 2008 by Wes McKinney and is used for data analysis in Python. Pandas is an open-source
library that provides high-performance data manipulation in Python. All of the basic and advanced concepts
of Pandas, such as Numpy, data operation, and time series, are covered in our tutorial.
Pandas Introduction
The name of Pandas is gotten from the word Board Information, and that implies an Econometrics from Multi-
faceted information. It was created in 2008 by Wes McKinney and is used for data analysis in Python.
Processing, such as restructuring, cleaning, merging, etc., is necessary for data analysis. Numpy, Scipy,
Cython, and Panda are just a few of the fast data processing tools available. Yet, we incline toward Pandas
since working with Pandas is quick, basic and more expressive than different apparatuses.
Since Pandas is built on top of the Numpy bundle, it is expected that Numpy will work with Pandas.
Before Pandas, Python was able for information planning, however it just offered restricted help for
information investigation. As a result, Pandas entered the picture and enhanced data analysis capabilities.
Regardless of the source of the data, it can carry out the five crucial steps that are necessary for processing
and analyzing it: load, manipulate, prepare, model, and analyze.
271
o It incorporates with different libraries like SciPy, and scikit-learn.
o Performs quickly, and the Cython can be used to accelerate it even further.
Benefits of Pandas
The following are the advantages of pandas overusing other languages:
Representation of Data: Through its DataFrame and Series, it presents the data in a manner that is appropriate
for data analysis.
Clear code: Pandas' clear API lets you concentrate on the most important part of the code. In this way, it gives
clear and brief code to the client.
DataFrame and Series are the two data structures that Pandas provides for processing data. These data
structures are discussed below:
1) Series
A one-dimensional array capable of storing a variety of data types is how it is defined. The term "index" refers
to the row labels of a series. We can without much of a stretch believer the rundown, tuple, and word reference
into series utilizing "series' technique. Multiple columns cannot be included in a Series. Only one parameter
exists:
Before creating a Series, Firstly, we have to import the numpy module and then use array() function in the
program.
1. import pandas as pd
2. import numpy as np
3. info = np.array(['P','a','n','d','a','s'])
4. a = pd.Series(info)
5. print(a)
Output
0 P
1 a
2 n
3 d
4 a
5 s
dtype: object
Explanation: In this code, firstly, we have imported the pandas and numpy library with the pd and np alias.
Then, we have taken a variable named "info" that consist of an array of some values. We have called
the info variable through a Series method and defined it in an "a" variable. The Series has printed by calling
the print(a) method.
272
Python Pandas DataFrame
It is a generally utilized information design of pandas and works with a two-layered exhibit with named
tomahawks (lines and segments). As a standard method for storing data, DataFrame has two distinct indexes-
row index and column index. It has the following characteristics:
It can be thought of as a series structure dictionary with indexed rows and columns. It is referred to as
"columns" for rows and "index" for columns.
1. import pandas as pd
2. # a list of strings
3. x = ['Python', 'Pandas']
4.
5. # Calling DataFrame constructor on list
6. df = pd.DataFrame(x)
7. print(df)
Output
0
0 Python
1 Pandas
This framework uses a famous tag line:The web framework for perfectionists with deadlines.
By using Django, we can build web applications in very less time. Django is designed in such a manner that it
handles much of configure things automatically, so we can focus on application development only.
History
Django was design and developed by Lawrence journal world in 2003 and publicly released under BSD license
in July 2005. Currently, DSF (Django Software Foundation) maintains its development and release cycle.
Django was released on 21, July 2005. Its current stable version is 2.0.3 which was released on 6 March, 2018.
273
Django Version History
0.90 16 Nov
2005
274
2.0 Dec First Python 3-only release,
2017 Simplified URL routing syntax,
Mobile friendly admin.
Popularity
Django is widely accepted and used by various well-known sites such as:
o Instagram
o Mozilla
o Disqus
o Pinterest
o Bitbucket
o The Washington Times
Features of Django
o Rapid Development
o Secure
o Scalable
o Fully loaded
o Versatile
o Open Source
o Vast and Supported Community
Rapid Development
Django was designed with the intention to make a framework which takes less time to build web application.
The project implementation phase is a very time taken but Django creates it rapidly.
Secure
Django takes security seriously and helps developers to avoid many common security mistakes, such as SQL
injection, cross-site scripting, cross-site request forgery etc. Its user authentication system provides a secure
way to manage user accounts and passwords.
Scalable
Django is scalable in nature and has ability to quickly and flexibly switch from small to large scale application
project.
275
Fully loaded
Django includes various helping task modules and libraries which can be used to handle common Web
development tasks. Django takes care of user authentication, content administration, site maps, RSS feeds etc.
Versatile
Django is versatile in nature which allows it to build applications for different-different domains. Now a days,
Companies are using Django to build various types of applications like: content management systems, social
networks sites or scientific computing platforms etc.
Open Source
Django is an open source web application framework. It is publicly available without cost. It can be downloaded
with source code from the public repository. Open source reduces the total cost of the application development.
•
•
• Sign In
• Home
• Saved Videos
• Courses
•
Data Structures and Algorithms
ML & Data Science
Web Development
Languages
Interview Corner
CS Subjects
Jobs
Practice
Contests
• GBlog
• Puzzles
• What's New ?
Change Language
• 90% Refund @Courses
• Free Python 3 Tutorial
• Data Types
• Control Flow
• Functions
• List
• String
276
• Set
• Tuple
• Dictionary
• Oops
• Exception Handling
• Python Programs
• Python Projects
• Python Interview Questions
• Python MCQ
• NumPy
• Pandas
• Python Database
• Data Science With Python
• Machine Learning with Python
• Django
• Flask
• R
Related Articles
• Explore Our Geeks Community
• Share Your Experience
• Python Itertools
• Python - Itertools.count()
• Python - Itertools.cycle()
• Python - itertools.repeat()
• Python - Itertools.accumulate()
• Python - Itertools.chain()
• Python - Itertools.chain.from_iterable()
• Python - Itertools.compress()
• Python - Itertools.dropwhile()
• Python - Itertools.filterfalse()
• Python - Itertools.islice()
• Python - Itertools.starmap()
• Python - Itertools.takewhile()
• Python - Itertools.tee()
• Python - Itertools.zip_longest()
• Python - Itertools.Product()
• Python - Itertools.Permutations()
• Python - Itertools.Combinations_with_replacement()
• Coding for EveryoneCourse
Python Itertools
Read
Courses
Practice
Jobs
••
•
277
Python’s Itertool is a module that provides various functions that work on iterators to produce
complex iterators. This module works as a fast, memory-efficient tool that is used either by
themselves or in combination to form iterator algebra.
For example, let’s suppose there are two lists and you want to multiply their elements. There
can be several ways of achieving this. One can be using the naive approach i.e by iterating
through the elements of both the list simultaneously and multiplying them. And another
approach can be using the map function i.e by passing the mul operator as the first parameter
to the map function and Lists as the second and third parameter to this function. Let’s see the
time taken by each approach.
• Python3
import operator
import time
# Defining lists
L1 = [1, 2, 3]
L2 = [2, 3, 4]
# Calculating result
a, b, c = map(operator.mul, L1, L2)
278
t2 = time.time()
print("\nTime taken by for loop: %.6f" % (t2 - t1))
Output:
Result: 2 6 12
Time taken by map function: 0.000005
Result: 2 6 12
Time taken by for loop: 0.000014
• Python
output :
In the above example, it can be seen that the time taken by the map function is approximately
half than the time taken by for loop. This shows that itertools are fast, memory-efficient tools.
Different types of iterators provided by this module are:
• Infinite iterators
• Combinatoric iterators
• Terminating iterators
Infinite iterators
Iterator in Python is any Python type that can be used with a ‘for in loop’. Python lists, tuples,
dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator
object has to exhaust, sometimes it can be infinite. Such types of iterators are known
as Infinite iterators.
Python provides three types of infinite iterators:
count(start, step): This iterator starts printing from the “start” number and prints
infinitely. If steps are mentioned, the numbers are skipped else step is 1 by default. See the
below example for its use with for in loop.
279
Example:
• Python3
import itertools
# for in loop
for i in itertools.count(5, 5):
if i == 35:
break
else:
print(i, end=" ")
Output:
5 10 15 20 25 30
cycle(iterable): This iterator prints all values in order from the passed container. It
restarts printing from the beginning again when all elements are printed in a cyclic
manner.
Example 1:
• Python3
import itertools
count = 0
# for in loop
for i in itertools.cycle('AB'):
if count > 7:
break
else:
print(i, end=" ")
count += 1
Output:
ABABABAB
Example 2: Using the next function.
• Python3
280
import itertools
# defining iterator
iterators = itertools.cycle(l)
# for in loop
for i in range(6):
Combinatoric iterators
Output:
Geeks for Geeks Geeks for Geeks
repeat(val, num): This iterator repeatedly prints the passed value an infinite number of times.
If the optional keyword num is mentioned, then it repeatedly prints num number of times.
Example:
• Python3
Output:
Printing the numbers repeatedly :
[25, 25, 25, 25]
Combinatoric iterators
The recursive generators that are used to simplify combinatorial constructs such as
permutations, combinations, and Cartesian products are called combinatoric iterators.
In Python there are 4 combinatoric iterators:
Product(): This tool computes the cartesian product of input iterables. To compute the
product of an iterable with itself, we use the optional repeat keyword argument to specify the
number of repetitions. The output of this function is tuples in sorted order.
Example:
• Python3
281
# import the product function from itertools module
from itertools import product
Output:
The cartesian product using repeat:
[(1, 1), (1, 2), (2, 1), (2, 2)]
282
Output:
All the permutations of the given list is:
[(1, 'geeks'), ('geeks', 1)]
Output:
All the combination of list in sorted order(without replacement) is:
[('A', 2)]
Output:
All the combination of string in sorted order(with replacement) is:
[('A', 'A'), ('A', 'B'), ('B', 'B')]
Terminating iterators
Terminating iterators are used to work on the short input sequences and produce the output
based on the functionality of the method used.
Different types of terminating iterators are:
accumulate(iter, func): This iterator takes two arguments, iterable target and the function
which would be followed at each iteration of value in target. If no function is passed, addition
takes place by default. If the input iterable is empty, the output iterable will also be empty.
Example:
• Python3
284
import itertools
import operator
# initializing list 1
li1 = [1, 4, 5, 7]
# using accumulate()
# prints the successive summation of elements
print("The sum after each iteration is : ", end="")
print(list(itertools.accumulate(li1)))
# using accumulate()
# prints the successive multiplication of elements
print("The product after each iteration is : ", end="")
print(list(itertools.accumulate(li1, operator.mul)))
# using accumulate()
# prints the successive summation of elements
print("The sum after each iteration is : ", end="")
print(list(itertools.accumulate(li1)))
# using accumulate()
# prints the successive multiplication of elements
print("The product after each iteration is : ", end="")
print(list(itertools.accumulate(li1, operator.mul)))
Output:
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140
chain(iter1, iter2..): This function is used to print all the values in iterable targets one after
another mentioned in its arguments.
Example:
• Python3
import itertools
# initializing list 1
li1 = [1, 4, 5, 7]
# initializing list 2
li2 = [1, 6, 5, 9]
285
# initializing list 3
li3 = [8, 10, 5, 4]
Output:
All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
• chain.from_iterable(): This function is implemented similarly as a chain() but
the argument here is a list of lists or any other iterable container.
Example:
• Python3
import itertools
# initializing list 1
li1 = [1, 4, 5, 7]
# initializing list 2
li2 = [1, 6, 5, 9]
# initializing list 3
li3 = [8, 10, 5, 4]
Output:
All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
compress(iter, selector): This iterator selectively picks the values to print from the passed
container according to the boolean list value passed as other arguments. The arguments
corresponding to boolean true are printed else all are skipped.
Example:
• Python3
286
# Python code to demonstrate the working of
# and compress()
import itertools
Output:
The compressed values in string are : ['G', 'F', 'G']
• dropwhile(func, seq): This iterator starts printing the characters only after the
func. in argument returns false for the first time.
Example:
• Python3
import itertools
# initializing list
li = [2, 4, 5, 7, 8]
Output:
The values after condition returns false : [5, 7, 8]
filterfalse(func, seq): As the name suggests, this iterator prints only values that return false
for the passed function.
Example:
• Python3
import itertools
287
# initializing list
li = [2, 4, 5, 7, 8]
Output:
The values that return false to function are : [5, 7]
islice(iterable, start, stop, step): This iterator selectively prints the values mentioned in its
iterable container passed as argument. This iterator takes 4 arguments, iterable container,
starting pos., ending position and step.
Example:
• Python3
import itertools
# initializing list
li = [2, 4, 5, 7, 8, 10, 20]
Output:
The sliced list values are : [4, 7, 10]
starmap(func., tuple list): This iterator takes a function and tuple list as argument and returns
the value according to the function from each tuple of the list.
Example:
• Python3
import itertools
288
# using starmap() for selection value acc. to function
# selects min of all tuple values
print ("The values acc. to function are : ", end ="")
print (list(itertools.starmap(min, li)))
Output:
The values acc. to function are : [1, 1, 4, 1]
• takewhile(func, iterable): This iterator is the opposite of dropwhile(), it prints
the values till the function returns false for 1st time.
Example:
• Python3
import itertools
# initializing list
li = [2, 4, 6, 7, 8, 10, 20]
Output:
The list values till 1st false value are : [2, 4, 6]
• tee(iterator, count):- This iterator splits the container into a number of
iterators mentioned in the argument.
Example:
• Python3
import itertools
# initializing list
li = [2, 4, 6, 7, 8, 10, 20]
289
it = itertools.tee(iti, 3)
Output:
The iterators are :
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
zip_longest( iterable1, iterable2, fillval): This iterator prints the values of iterables
alternatively in sequence. If one of the iterables is printed fully, the remaining values are filled
by the values assigned to fillvalue.
Example:
• Python3
import itertools
Output:
The combined values of iterables is :
('G', 'e') ('e', 'k') ('s', 'f') ('o', 'r') ('G', 'e') ('e', 'k') ('s', '_')
SciPy
SciPy in Python is an open-source library used for solving mathematical, scientific, engineering, and
technical problems. It allows users to manipulate the data and visualize the data using a wide range of high-
level Python commands. SciPy is built on the Python NumPy extention. SciPy is also pronounced as “Sigh
Pi.”
Sub-packages of SciPy:
• File input/output – scipy.io
• Special Function – scipy.special
• Linear Algebra Operation – scipy.linalg
290
• Interpolation – scipy.interpolate
• Optimization and fit – scipy.optimize
• Statistics and random numbers – scipy.stats
• Numerical Integration – scipy.integrate
• Fast Fourier transforms – scipy.fftpack
• Signal Processing – scipy.signal
• Image manipulation – scipy.ndimage
Numpy VS SciPy
Numpy
• Numpy is written in C and use for mathematical or numeric calculation.
• It is faster than other Python Libraries
• Numpy is the most useful library for Data Science to perform basic calculations.
• Numpy contains nothing but array data type which performs the most basic operation like sorting,
shaping, indexing, etc.
SciPy
• SciPy is built in top of the NumPy
• SciPy module in Python is a fully-featured version of Linear Algebra while Numpy contains only a
few features.
• Most new Data Science features are available in Scipy rather than Numpy.
291
Before we start learning SciPy Python, you need to know basic functionality as well as different types of an
array of NumPy
The standard way to import SciPy modules and Numpy:
from scipy import special #same for other modules
import numpy as np
File Input / Output package
Scipy, I/O package, has a wide range of functions for work with different files format which are Matlab, Arff,
Wave, Matrix Market, IDL, NetCDF, TXT, CSV and binary format.
Let us take one file format Python SciPy example as which are regularly used in MatLab:
import numpy as np
from scipy import io as sio
array = np.ones((4, 4))
sio.savemat('example.mat', {'ar': array})
data = sio.loadmat(‘example.mat', struct_as_record=True)
data['ar']
Output:
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
Code Explanation
• Line 1 & 2: Import the essential SciPy library in Python with I/O package and Numpy.
• Line 3: Create 4 x 4, dimensional one’s array
• Line 4: Store array in example.mat file.
• Line 5: Get data from example.mat file
• Line 6: Print output.
Special Function package
• scipy.special package contains numerous functions of mathematical physics.
• SciPy special function includes Cubic Root, Exponential, Log sum Exponential, Lambert, Permutation
and Combinations, Gamma, Bessel, hypergeometric, Kelvin, beta, parabolic cylinder, Relative Error
Exponential, etc..
• For one line description all of these function, type in Python console:
help(scipy.special)
Output:
292
NAME
scipy.special
DESCRIPTION
========================================
Special functions (:mod:`scipy.special`)
========================================
.. module:: scipy.special
Nearly all of the functions below are universal functions and follow
broadcasting and automatic array-looping rules. Exceptions are noted.
Cubic Root Function
Cubic Root function finds the cube root of values.
Syntax:
scipy.special.cbrt(x)
Example:
from scipy.special import cbrt
#Find cubic root of 27 & 64 using cbrt() function
cb = cbrt([27, 64])
#print value of cb
print(cb)
Output: array([3., 4.])
Exponential Function:
Exponential function computes the 10**x element-wise.
Example:
from scipy.special import exp10
#define exp10 function and pass value in its
exp = exp10([1,10])
print(exp)
Output: [1.e+01 1.e+10]
Permutations & Combinations
293
SciPy also gives functionality to calculate Permutations and Combinations.
Combinations – scipy.special.comb(N,k)
Example:
from scipy.special import comb
#find combinations of 5, 2 values using comb(N, k)
com = comb(5, 2, exact = False, repetition=True)
print(com)
Output: 15.0
Permutations –
scipy.special.perm(N,k)
Example:
from scipy.special import perm
#find permutation of 5, 2 using perm (N, k) function
per = perm(5, 2, exact = True)
print(per)
Output: 20
Log Sum Exponential Function
Log Sum Exponential computes the log of sum exponential input element.
Syntax :
scipy.special.logsumexp(x)
Bessel Function
Nth integer order calculation function
Syntax :
scipy.special.jn()
Linear Algebra with SciPy
• Linear Algebra of SciPy is an implementation of BLAS and ATLAS LAPACK libraries.
• Performance of Linear Algebra is very fast compared to BLAS and LAPACK.
• Linear algebra routine accepts two-dimensional array object and output is also a two-dimensional
array.
Now let’s do some test with scipy.linalg,
Calculating determinant of a two-dimensional matrix,
from scipy import linalg
import numpy as np
294
#define square matrix
two_d_array = np.array([ [4,5], [3,2] ])
#pass values to det() function
linalg.det( two_d_array )
Output: -7.0
Inverse Matrix –
scipy.linalg.inv()
Inverse Matrix of Scipy calculates the inverse of any square matrix.
Let’s see,
from scipy import linalg
import numpy as np
# define square matrix
two_d_array = np.array([ [4,5], [3,2] ])
#pass value to function inv()
linalg.inv( two_d_array )
Output:
array( [[-0.28571429, 0.71428571],
[ 0.42857143, -0.57142857]] )
Eigenvalues and Eigenvector
scipy.linalg.eig()
• The most common problem in linear algebra is eigenvalues and eigenvector which can be easily solved
using eig()function.
• Now lets we find the Eigenvalue of (X) and correspond eigenvector of a two-dimensional square
matrix.
Example
from scipy import linalg
import numpy as np
#define two dimensional array
arr = np.array([[5,4],[6,3]])
#pass value into function
eg_val, eg_vect = linalg.eig(arr)
#get eigenvalues
print(eg_val)
295
#get eigenvectors
print(eg_vect)
Output:
[ 9.+0.j -1.+0.j] #eigenvalues
[ [ 0.70710678 -0.5547002 ] #eigenvectors
[ 0.70710678 0.83205029] ]
Discrete Fourier Transform – scipy.fftpack
• DFT is a mathematical technique which is used in converting spatial data into frequency data.
• FFT (Fast Fourier Transformation) is an algorithm for computing DFT
• FFT is applied to a multidimensional array.
• Frequency defines the number of signal or wavelength in particular time period.
•
scipy.interpolate Interpolation
296
Matplotlib (Python Plotting Library)
Human minds are more adaptive for the visual representation of data rather than textual data. We can easily understand
things when they are visualized. It is better to represent the data through the graph where we can analyze the data more
efficiently and make the specific decision to data analysis. Before learning the matplotlib, we need to understand data
Data Visualization
Graphics provides an excellent approach for exploring the data, which is essential for presenting results. Data visualization is
a new term. It expresses the idea that involves more than just representing data in the graphical form (instead of using
textual form).
This can be very helpful when discovering and getting to know a dataset and can help with classifying patterns, corrupt data, o
much more. With a little domain knowledge, data visualizations can be used to express and demonstrate key relationships in p
The static does indeed focus on quantitative description and estimations of data. It provides an important set of tools for
There are five key plots that are used for data visualization.
297
There are five phases which are essential to make the decision for the organization:
Visualize: We analyze the raw data, which means it makes complex data more accessible, understandable, and more
usable. Tabular data representation is used where the user will look up a specific measurement, while the chart of several
types is used to show patterns or relationships in the data for one or more variables.
Analysis: Data analysis is defined as cleaning, inspecting, transforming, and modelling data to derive useful information.
Whenever we make a decision for the business or in daily life, is by past experience. What will happen to choose a
particular decision, it is nothing but analyzing our past. That may be affected in the future, so the proper analysis is
Document Insight: Document insight is the process where the useful data or information is organized in the document in
Transform Data Set: Standard data is used to make the decision more effectively.
298
Data visualization can perform below tasks:
helps decision-makers to see the relationship between multi-dimensional data sets. It offers new ways to analyses data
through the use of maps, fever charts, and other rich graphical representations.
Visual data discovery is more likely to find the information that the organization needs and then end up with being more
299
The ability to make these types of correlations enables the executives to identify the root cause of the problem and act
Suppose a food company is looking their monthly customer data, and the data is presented with bar charts, which shows
that the company's score has dropped by five points in the previous months in that particular region; the data suggest that
Having an idea about the customer's sentiments and other data discloses an emerging opportunity for the company
of websites are required to take benefit of location-specific information, which is already present in the customer details.
Matplotlib is a Python library which is defined as a multi-platform data visualization library built on Numpy array. It can be
used in python scripts, shell, web application, and other graphical user interface toolkit.
ADVERTISEMENT
The John D. Hunter originally conceived the matplotlib in 2002. It has an active development community and is distributed
under a BSD-style license. Its first version was released in 2003, and the latest version 3.1.1 is released on 1 July 2019.
Matplotlib 2.0.x supports Python versions 2.7 to 3.6 till 23 June 2007. Python3 support started with
Matplotlib 1.2. Matplotlib 1.4 is the last version that supports Python 2.6.
There are various toolkits available that are used to enhance the functionality of the matplotlib. Some of these tools are
downloaded separately, others can be shifted with the matplotlib source code but have external dependencies.
Bashmap: It is a map plotting toolkit with several map projections, coastlines, and political boundaries.
Cartopy: It is a mapping library consisting of object-oriented map projection definitions, and arbitrary point, line, polygon,
300
and image transformation abilities.
Excel tools: Matplotlib provides the facility to utilities for exchanging data with Microsoft Excel.
Natgrid: It is an interface to the Natgrid library for irregular gridding of the spaced data.
Matplotlib Architecture
There are three different layers in the architecture of the matplotlib which are the following:
Backend Layer
Artist layer
Scripting layer
Backend layer
The backend layer is the bottom layer of the figure, which consists of the implementation of the various functions that are
necessary for plotting. There are three essential classes from the backend layer FigureCanvas(The surface on which the
figure will be drawn), Renderer(The class that takes care of the drawing on the surface), and Event(It handle the mouse
Artist Layer
The artist layer is the second layer in the architecture. It is responsible for the various plotting functions, like axis, which
Scripting layer
The scripting layer is the topmost layer on which most of our code will run. The methods in the scripting layer, almost
automatically take care of the other layers, and all we need to care about is the current state (figure & subplot).
301
Figure: It is a whole figure which may hold one or more axes (plots). We can think of a Figure as a canvas that holds plots.
Axes: A Figure can contain several Axes. It consists of two or three (in the case of 3D) Axis objects. Each Axes is comprised
Axis: Axises are the number of line like objects and responsible for generating the graph limits.
Artist: An artist is the all which we see on the graph like Text objects, Line2D objects, and collection objects. Most Artists
Installing Matplotlib
Before start working with the Matplotlib or its plotting functions first, it needs to be installed. The installation of matplotlib
is dependent on the distribution that is installed on your computer. These installation methods are following:
Visit the official site of Anaconda and click on the Download Button
302
Choose download according to your Python interpreter configuration.
303
Matplotlib can be installed using with the Anaconda Prompt by typing command. To install matplotlib, open Anaconda
The python package manager pip is also used to install matplotlib. Open the command prompt window, and type the
following command:
terminal.
import matplotlib
matplotlib.__version__
'3.1.1'
plt.plot([1,2,3],[4,5,1])
plt.show()
Output:
304
It takes only three lines to plot a simple graph using the Python matplotlib. We can add titles, labels to our chart which are
created by Python matplotlib library to make it more meaningful. The example is the following:
x = [5, 2, 7]
y = [1, 10, 4]
plt.plot(x, y)
plt.title('Line graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
Output:
pyplot functions are used to make some changes to figure such as create a figure, creates a plotting area in a figure, plots
some lines in a plotting area, decorates the plot including labels, etc.
It is good to use when we want to plot something quickly without instantiating any figure or Axes.
While working with matplotlib.pyplot, some states are stored across function calls so that it keeps track of the things like
current figure and plotting area, and these plotting functions are directed to the current axes.
The pyplot module provide the plot() function which is frequently use to plot a graph. Let's have a look on the simple
example:
plt.plot([1,2,3,4,5])
plt.ylabel("y axis")
plt.xlabel('x axis')
plt.show()
Output:
In the above program, it plots the graph x-axis ranges from 0-4 and the y-axis from 1-5. If we provide a single list to the
plot(), matplotlib assumes it is a sequence of y values, and automatically generates the x values. Since we know that python
index starts at 0, the default x vector has the same length as y but starts at 0. Hence the x data are [0, 1, 2, 3, 4].
We can pass the arbitrary number of arguments to the plot(). For example, to plot x versus y, we can do this following way:
plt.plot([1,2,3,4,5],[1,4,9,16,25])
plt.ylabel("y axis")
plt.xlabel('x axis')
plt.show()
306
Output:
format string is 'b-'which is the solid blue as you can observe in the above plotted graph. Let's consider the following
plt.axis([0, 6, 0, 20])
plt.show()
Output:
307
'-g' Green solid line
Character Color
'b' Blue
'g' Green
'r' Red
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
marks= [87,50,98]
plt.figure(figsize=(9,3))
plt.subplot(131)
plt.bar(names, marks)
plt.subplot(132)
plt.scatter(names, marks)
308
plt.subplot(133)
plt.plot(names, marks)
plt.suptitle('Categorical Plotting')
plt.show()
Output:
In the above program, we have plotted the categorical graph using the subplot() function.
What is subplot()
The Matplotlib subplot() function is defined as to plot two or more plots in one figure. We can use
this method to separate two graphs which plotted in the same axis Matplotlib supports all kinds of subplots, including
It accepts the three arguments: they are nrows, ncols, and index. It denote the number of rows, number of columns and
the index.
subplot(nrows,ncols,index,**kwargs)
subplot(pos,**kwargs)
subplot(ax)
Parameters:
*args:
Three separate integers or three-digit integer describes the position of the subplot. If the three integers are nrows, ncols,
and index in order, the subplot will take the index position on a grid with nrows row and ncol column.
The argument pos are a three-digit integer, where the first digit is denoted the number of rows, the second digit denoted
the number of columns, and the third represents the index of the subplot. For example, subplot (1, 3, 2) is the same as the
subplot (132).
309
**kwargs
The subplot() function also accepts the keyword arguments for the returned axes base class.
The line graph is simple to plot; let's consider the following example:
x = [4,8,9]
y = [10,12,15]
plt.plot(x,y)
plt.title("Line graph")
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
Output:
We can customize the graph by importing the style module. The style module will be built into a matplotlib installation.
It contains the various functions to make the plot more attractive. In the below program, we are using the style module:
310
from matplotlib import style
style.use('ggplot')
x = [16, 8, 10]
y = [8, 16, 6]
y2 = [6, 15, 7]
plt.title('Epic Info')
fig = plt.figure()
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.legend()
plt.grid(True, color='k')
plt.show()
Output:
In Matplotlib, the figure (an instance of class plt.Figure) can be supposed of as a single container that consists of all the
Example-3
import numpy as np
311
fig = plt.figure()
ax = plt.axes()
ax.plot(x, np.sin(x))
Output:
The matplotlib provides the fill_between() function which is used to fill area around the lines based on the user defined
logic.
Example-4
import numpy as np
fig = plt.figure()
ax = plt.axes()
ax.plot(x, np.sin(x))
import numpy as np
x = np.arange(0.0, 2, 0.01)
312
y1 = np.sin(2 * np.pi * x)
Output:
2. Bar graphs
Bar graphs are one of the most common types of graphs and are used to show data associated with the categorical variables.
provides a bar() to make bar graphs which accepts arguments such as: categorical variables, their value and color.
players = ['Virat','Rohit','Shikhar','Hardik']
runs = [51,87,45,67]
plt.bar(players,runs,color = 'green')
plt.title('Score Card')
plt.xlabel('Players')
plt.ylabel('Runs')
plt.show()
Output:
313
Another function barh() is used to make horizontal bar graphs. It accepts xerr or yerr as arguments (in case of vertical
players = ['Virat','Rohit','Shikhar','Hardik']
runs = [51,87,45,67]
plt.title('Score Card')
plt.xlabel('Players')
plt.ylabel('Runs')
plt.show()
Output:
Let's have a look on the other example using the style() function:
314
style.use('ggplot')
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [7,15,7]
plt.title('Information')
plt.ylabel('Y axis')
plt.xlabel('X axis')
Output:
Similarly to vertical stack, the bar graph together by using the bottom argument and define the bar graph, which we want
import numpy as np
315
countries = ['USA', 'India', 'China', 'Russia', 'Germany']
plt.xticks(ind, countries)
plt.ylabel("Medals")
plt.xlabel("Countries")
plt.legend(loc="upper right")
Output:
3. Pie Chart
A pie chart is a circular graph that is broken down in the segment or slices of pie. It is generally used to represent the
percentage or proportional data where each slice of pie represents a particular category. Let's have a look at the below
example:
316
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
shadow=True, startangle=90)
plt.show()
Output:
4. Histogram
First, we need to understand the difference between the bar graph and histogram. A histogram is used for the distribution,
whereas a bar chart is used to compare different entities. A histogram is a type of bar plot that shows the frequency of a
For example we take the data of the different age group of the people and plot a histogram with respect to the bin. Now,
bin represents the range of values that are divided into series of intervals. Bins are generally created of the same size.
population_age = [21,53,60,49,25,27,30,42,40,1,2,102,95,8,15,105,70,65,55,70,75,60,52,44,43,42,45]
bins = [0,10,20,30,40,50,60,70,80,90,100]
plt.xlabel('age groups')
317
plt.ylabel('Number of people')
plt.title('Histogram')
plt.show()
Output:
import numpy as np
plt.style.use('fivethirtyeight')
mu = 50
sigma = 7
fig, ax = plt.subplots()
ax.hist(x, 20)
ax.set_title('Historgram')
ax.set_xlabel('bin range')
ax.set_ylabel('frequency')
fig.tight_layout()
plt.show()
318
Output:
5. Scatter plot
The scatter plots are mostly used for comparing variables when we need to define how much one variable is affected by
another variable. The data is displayed as a collection of points. Each point has the value of one variable, which defines the
position on the horizontal axes, and the value of other variable represents the position on the vertical axis.
Example-1:
from matplotlib import pyplot as plt
style.use('ggplot')
x = [5,7,10]
y = [18,10,6]
x2 = [6,9,11]
y2 = [7,14,17]
plt.scatter(x, y)
plt.title('Epic Info')
plt.ylabel('Y axis')
319
plt.xlabel('X axis')
plt.show()
Output:
Example-2
import matplotlib.pyplot as plt
plt.xlabel('saving*100')
plt.ylabel('income*1000')
plt.title('Scatter Plot')
plt.legend()
plt.show()
Output:
320
6. 3D graph plot
Matplotlib was initially developed with only two-dimension plot. Its 1.0 release was built with some
f three-dimensional plotting utilities on top of two-dimension display, and the result is a convenient set of tools for 3D data
visualization.
Three-dimension plots can be created by importing the mplot3d toolkit, include with the main Matplotlib installation:
When this module is imported in the program, three-dimension axes can be created by passing the keyword projection='3d'
normal axes creation routines:
Example-1:
from mpltoolkits import mplot3d
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')_
Output:
321
Example-2:
from mpl_toolkits import mplot3d
import numpy as np
height = np.array([100,110,87,85,65,80,96,75,42,59,54,63,95,71,86])
weight = np.array([105,123,84,85,78,95,69,42,87,91,63,83,75,41,80])
scatter(height,weight)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter3D(height,weight)
plt.xlabel("Height")
plt.ylabel("Weight")
plt.xlabel("Height")
plt.ylabel("Weight")
plt.show()
Output:
322
Note: We can use the plot3D () to plot simple 3D line graph.
Example-3
import matplotlib as mpl
import numpy as np
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta1)
y = r * np.cos(theta1)
ax.legend()
plt.show()
Output:
323
Important functions of Matplotlib
Functions Description
plot(x-axis value, y-axis-values) It is used to plot a simple line graph with x-axis value against the y-axis
values. show() It is used to display the graph.
title("string") It is used to set the title of the plotted graph as specified by the string.
xlabel("string") It is used to set the label for the x-axis as specified by the string.
ylabel("string") It is used to set the label for y-axis as specified by the string.
subtitle("string") It adds a common title to the plotted graph specified by the string.
subplots(nrows,ncols,figsize) It provides the simple way to create subplot, in a single call and returns a
tuple of a figure and number of axes.
set_title("string") It is an axes level method which is used to set the title of the subplots.
324
legend(loc) It is used to make a legend of the graph.
xtricks(index, categorical variables) It is used to set or get the current tick locations labels of the x-axis.
xlim(start value, end value) It is used to set the limit of values of the x-axis.
ylim(start value, end value) It is used to set the limit of values of the y-axis.
scatter(x-axis values, y-axis values) It is used to plots a scatter plot with x-axis value against the y-axis values.
set_xlabel("string") It is an axes level method which is used to set the x-label of the plot
specified as a string.
scatter3D(x-axis values, y-axis It is used to plot a three-dimension scatter plot with x- axis value against
values) the y-axis.
plot3D(x-axis values, y-axis values) It is used to plots a three-dimension line graph with x- axis values against
y-axis values.
325