Introduction to Computer Programming
Matplotlib
Kripabandhu Ghosh
CDS, IISER Kolkata
Hi again!
NumPy
NumPy
Nomenclature
NumPy stands for Numerical Python
Features
Python library used for working with arrays (type: ndarray)
Scientific calculations
Functionalities for linear algebra, matrices etc.
Ease of application
Efficiency (e.g. over Lists)
NumPy arrays used popularly in data science due to speed and resources
But we have List !!!
NumPy
Nomenclature
NumPy stands for Numerical Python
Features
Python library used for working with arrays (type: ndarray)
Scientific calculations
Functionalities for linear algebra, matrices etc.
Ease of application
Efficiency (e.g. over Lists)
NumPy arrays used popularly in data science due to speed and resources
But we have List !!!
NumPy arrays are faster than Lists
List Vs. NumPy array
List NumPy array
Slow Fast (50x faster)
List Vs. NumPy array
List NumPy array
Slow Fast (50x faster)
Stores items of different types Stores items of the same type
List Vs. NumPy array
List NumPy array
Slow Fast (50x faster)
Stores items of different types Stores items of the same type
Takes more memory Takes less memory
Using NumPy: import
Code
import numpy
a = [Link]([1, 2, 3, 4, 5]) #array of numpy
print([Link](a))
Output
15
Interpretation
Accessing array and sum functions in numpy package
Using NumPy: import with alias
Code
import numpy as np
a = [Link]([1, 2, 3, 4, 5]) #array of numpy
print([Link](a))
Output
15
Interpretation
Accessing array and sum functions in numpy package using the alias np
NumPy array: creation
Create from List
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
Output
[1 2 3 4 5]
<type ‘[Link]’>
Create from Tuple
import numpy as np
arr = [Link]((1, 2, 3, 4, 5))
print(arr)
print(type(arr))
Output
[1 2 3 4 5]
<type ‘[Link]’>
We can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray
NumPy array: operations
Applying a function on a list
def f(x):
return x**2
x list = [1.0, 2.5, 4.0, 6.0]
y list = []
for e in x list:
y [Link](f(e))
print(y list)
Output
[1.0, 6.25, 16.0, 36.0]
NumPy array: operations (contd.)
Applying a function on a NumPy array
def f(x):
return x**2
import numpy as np
x = [Link]([1.0, 2.5, 4.0, 6.0])
y = f(x)
print(y)
Output
[ 1. 6.25 16. 36. ]
NumPy array: operations (contd.)
Elementwise operations
import numpy as np
x = [Link]([1.0, 2.5, 4.0, 6.0])
y = x+2
print(y)
y = x*2
print(y)
print(x+y) #Note: you get a concatenated result for a list
y = x**2
print(y)
Output
[3. 4.5 6. 8. ]
[ 2. 5. 8. 12.]
[ 3. 7.5 12. 18. ]
[ 1. 6.25 16. 36. ]
NumPy array: more on creation
Using arange
Format: [Link](start, stop, step ....)
Characteristics: Return evenly spaced values within a given interval.
start: Start of interval. The interval includes this value. The default start value is 0.
stop: End of interval. The interval does not include this value, except in some cases where step is
not an integer and floating point round-off affects the length of out.
step: Spacing between values. The default step size is 1. If step is specified as a position
argument, start must also be given.
Code
import numpy as np
a = [Link](0, 1, 0.1)
print(a)
Output
[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
Matplotlib
Matplotlib
Introduction
Graph plotting library used for data visualization
Uses NumPy
Matplotlib along with NumPy can be considered as the open source
equivalent of MATLAB
Matplotlib: the first graph
Code
import [Link] as plt
import numpy as np
x = [Link](0, 50, 0.1)
y=x
[Link](x, y)
[Link](‘[Link]’)
Matplotlib: adding markers
Code
import [Link] as plt
import numpy as np
x = [Link](0, 50, 5)
y=x
[Link](x, y, marker = ‘o’)
[Link](‘[Link]’)
Matplotlib: marker table1
1 [Link]
Matplotlib: adding markers (fmt)
marker line color
Code
import [Link] as plt
import numpy as np
x = [Link](0, 50, 5)
y=x
[Link](x, y, ‘o:r’)
[Link](‘[Link]’)
Matplotlib: line and color table2
2 [Link]
Matplotlib: adding linestyle
Code
import [Link] as plt
import numpy as np
x = [Link](0, 50, 5)
y=x
[Link](x, y, linestyle = ‘dotted’, color = ‘r’)
[Link](‘[Link]’)
Matplotlib: multiple lines
Code
import [Link] as plt
import numpy as np
x = [Link](0, 50, 5)
y=x
w = x**2
[Link](x, y, linestyle = ‘dotted’, color = ‘r’, marker = ‘o’)
[Link](x, w, linestyle = ‘dashed’, color = ‘g’, marker = ‘s’)
[Link](‘[Link]’)
Matplotlib: multiple lines (contd.)
Code
import [Link] as plt
import numpy as np
x new = [Link](0, 2*[Link], 0.2)
w = [Link](x new)
y new = [Link](x new)
[Link](x new, w, ‘o-r’, x new, y new, ‘d-m’)
[Link](‘[Link]’)
Matplotlib: title, labels, legend
Code
import [Link] as plt
import numpy as np
x new = [Link](0, 2*[Link], 0.2)
w = [Link](x new)
y new = [Link](x new)
[Link](x new, w, ‘X:r’, label = ‘Sin’)
[Link](x new, y new, ‘s– –g’, label = ‘Cos’)
[Link](‘Sin vs Cos’)
[Link](‘x-axis’)
[Link](‘y-axis’)
[Link]()
[Link](‘[Link]’)
Matplotlib: subplots
Code
import [Link] as plt
import numpy as np
x new = [Link](0, 2*[Link], 0.2)
w = [Link](x new)
y new = [Link](x new)
[Link](1, 2, 1) #the figure has 1 row, 2 columns, and this plot is the first plot
[Link](‘Sin’)
[Link](‘x-axis’)
[Link](‘y-axis’)
[Link](x new, y new, ‘X:r’, label = ‘Sin’)
[Link](1, 2, 2) #the figure has 1 row, 2 columns, and this plot is the second plot
[Link](‘Cos’)
[Link](‘x-axis’)
[Link](‘y-axis’)
[Link](x new, w, ‘s– –g’, label = ‘Cos’)
[Link](‘Sin vs. Cos’)
[Link](‘[Link]’)
Matplotlib: subplots (contd.)
Matplotlib: bar
Code
import [Link] as plt
import numpy as np
x = [Link]([“Virat”, “Rahul”, “Rohit”, “Shikhar”])
y = [Link]([55, 80, 15, 105])
[Link](x, y, color = “hotpink”, width = 0.5)
[Link](‘[Link]’)
Matplotlib: pie
Code
import [Link] as plt
import numpy as np
x = [Link]([“Virat”, “Rahul”, “Rohit”, “Shikhar”])
y = [Link]([55, 80, 15, 105])
[Link](y, labels = x)
[Link](‘[Link]’)
Python Definitely Doesn’t Bite!