Numerical Methods Using Python: (MCSC-202)
Numerical Methods Using Python: (MCSC-202)
(MCSC-202)
USING PYTHON
By
Samir Shrestha
Department of Mathematics
Kathmandu University, Dhulikhel
Lecture-5
Python Packages
Python Packages
numpy: Multi-dimensional Array Operations
import numpy as np
import scipy as sp
import pandas as pd
Subplot, Subplots
3D-plots
Quiver plots
Introducing Matplotlib
Matplotlib
Matplotlib is a comprehensive library for creating static, animated
and interactive visualization of data in Python
Features:
Develops publication quality plots with just few lines of code
Provides full control of line styles, font properties, axis properties,
color choise etc
Export to a number of file formats and interactive environments
The module matplotlib.pyplot is a collection of 2D/3D plotting
functions that provide Python with MATLAB-style functionality
matplotlib doesn't come bundled with Python. It has to be
installed it separately:
Example4: Plotting with given x-data and y-data choosing color, marker
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
plt.plot(x,y,'-ro')# red color curve together with marker o
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Data visualization')
plt.show()
Matplotlib
An RGB or RGBA (red, green, blue, alpha) tuple of float values in the
closed interval [0,1] can be used for all possible color combinations.
Eg (0.1, 0.3. 0.5) or (0.1, 0.3. 0.5, 0.9)
Matplotlib
Specifying the Marker
Matplotlib provides single character short hand notations to handle
markers. Marker works for both plot and scatter
Color Meaning
'.' point
'*' star
'o' circle
'v' Triangle down
'^' Triangle up
'<' Traingle left
'>' Triangle right
's' square
'p' pentagon
'x' cross
'd' Dimond
'+' plus
Subplot
Matplotlib
Subplot:
Matplotlib subplot() function can be used to plot two or more plots
in one figure. Explained with example
Example1:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4,4,30);
y1 = x**2
y2 = x**3
plt.figure()
plt.subplot(1,2,1)
plt.plot(x,y1,'r')
plt.subplot(1,2,2)
plt.plot(x,y2,'b')
plt.show()
Subplots
Matplotlib
Subplots:
This ia an alternate method that provides a way to plot multiple
figures in single figure window
subplots() returns a Figure instance and single or an array of Axes
objects. This is the simplest and recommended way of creating
single and multiple figures.
This way of creating figures has more control over figure and axes
Example2: (Single Plot)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2*np.pi,2*np.pi,50)
y = x*np.sin(x)
fig, ax = plt.subplots(1,1,figsize=(10,6))
ax.plot(x,y,'g')
ax.set_title('Single Plot')
plt.show()
Matplotlib
Subplots continue...
Example3: (Vertically Staked plot)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2*np.pi,2*np.pi,50)
y = x*np.sin(x)
z = x*np.cos(x)
fig, ax = plt.subplots(2,1,figsize=(8,8))
fig.suptitle('Vertically staked subplots')
ax[0].plot(x,y,'r')
ax[0].set_title('plot of $x sinx$')
ax[1].plot(x,z,'g')
ax[1].set_title('plot of $x cosx$')
plt.show()
Matplotlib
Subplots continue...
Example4: (Horizontally Staked Plot)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2*np.pi,2*np.pi,50)
y = x*np.sin(x)
z = x*np.cos(x)
fig, ax = plt.subplots(1,2,figsize=(8,8))
fig.suptitle('Vertically staked subplots')
ax[0].plot(x,y,'r')
ax[0].set_title('plot of $x sinx$')
ax[1].plot(x,z,'g')
ax[1].set_title('plot of $x cosx$')
plt.show()
Subplots continue... Matplotlib
Example5: ( Two direnctional Staked Plot)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2*np.pi,2*np.pi,50)
y = x*np.sin(x)
z = x*np.cos(x)
fig, ax = plt.subplots(2,2,figsize=(15,6))
fig.suptitle('Horizontal staked subplots')
ax[0,0].plot(x,y,'m')
ax[0,0].set_title('plot of $x sinx$')
ax[0,1].plot(x,z,'c')
ax[0,1].set_title('plot of $x cosx$')
ax[1,0].plot(x,y,'g')
ax[1,0].set_title('plot of $x sinx$')
ax[1,1].plot(x,z,'r')
ax[1,1].set_title('plot of $x cosx$')
plt.show()
3D-Plots
3-Dimensional Plotting
Matplotlib
3-dimensional plots are enabled by importing the mplot3d toolkit that is
included with the main matplotlib installation
https://jakevdp.github.io/PythonDataScienceHandbook/index.html
3-Dimensional Plotting Continue ... Matplotlib
Example4: (Contour Plot on Space)
from mpl_toolkits import mpl ot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,8))
ax = plt.axes(projection="3d") # creates 3-dimensional axes
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
X, Y = np.meshgrid(x,y)
Z = X**2 + Y**2
ax.view_init(60,35) # View angle
ax.contour3D(X,Y,Z,50,cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.grid([])
https://jakevdp.github.io/PythonDataScienceHandbook/index.html
Matplotlib
3-Dimensional Plotting Continue ...
Example5: (Surface Wireframe)
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,8))
ax = plt.axes(projection="3d") # creates 3-dimensional axes
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
X, Y = np.meshgrid(x,y)
Z = X**2 + Y**2
ax.plot_wireframe(X,Y,Z,color='green')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('Wireframe')
ax.grid([])
https://jakevdp.github.io/PythonDataScienceHandbook/index.html
Matplotlib
3-Dimensional Plotting Continue ...
Exercise:
Question 1: Write the program to show the contour plot of
𝑥 5 5 −𝑥 2 −𝑦 2
the function 𝑓 𝑥, 𝑦 = (1 − + 𝑥 + 𝑦 )/𝑒 on the
2
domain −3 ≤ 𝑥, 𝑦 ≤ 3