Graphics 2D 3D
Graphics 2D 3D
Mathematical
Plotting with
Matplotlib
Requirements
2
17/10/2022
Functions in Python
4
17/10/2022
Plotting in Python
numpy.linspace
6
17/10/2022
import numpy as np
import matplotlib.pyplot as plt
Plotting style
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
lines = ax.plot(y1, 'o', y2, 'x', y3, '*')
plt.show()
8
17/10/2022
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
lines = ax.plot(y1, 'o', y2, 'x', y3, '*')
ax.set_title("Plot of the data y1, y2, and y3")
ax.set_xlabel("x axis label")
ax.set_ylabel("y axis label")
ax.legend(("data y1", "data y2", "data y3"))
plt.show()
10
17/10/2022
Plotting lines
11
Subplots
+ It is also possible to draw multiple graphs in the same figure.
+ plt.subplots(int, int, index), default: (1, 1, 1)
+ Three integers (nrows, ncols, index).
+ The subplot will take the index position on a grid with nrows rows
and ncols columns.
+ index starts at 1 in the upper left corner and increases to the right.
+ index can also be a two-tuple specifying the (first, last) indices (1-
based, and including last) of the subplot,
+ e.g., fig.add_subplot(3, 1, (1, 2)) makes a subplot that spans the
upper 2/3 of the figure.
12
17/10/2022
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.suptitle("MY SHOP")
plt.show()
13
14
17/10/2022
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
lines = ax.plot(y1, 'o', y2, 'x', y3, '*')
ax.set_title("Plot of the data y1, y2, and y3")
ax.set_xlabel("x axis label")
ax.set_ylabel("y axis label")
ax.legend(("data y1", "data y2", "data y3"))
plt.savefig(’figure1.png’)
15
3D plotting with
Matplotlib
+ 3D plotting in
Matplotlib starts by
enabling the utility
toolkit.
+ Importing the mplot3d
library
16
17/10/2022
projection="3d"
17
Try this!!!
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection="3d")
plt.show()
18
17/10/2022
Empty
19
Draw something in 3D
+ fig = plt.figure()
ax = plt.axes(projection="3d")
z_points = 15 * np.random.random(100)
x_points = np.cos(z_points) + 0.1 * np.random.randn(100)
y_points = np.sin(z_points) + 0.1 * np.random.randn(100)
ax.scatter3D(x_points, y_points, z_points, c=z_points, cmap='hsv');
plt.show()
20
17/10/2022
21
Result
22
17/10/2022
23
Surface Plots
24
17/10/2022
25
First
+ fig = plt.figure()
ax = plt.axes(projection="3d")
+ def z_function(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 2))
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = z_function(X, Y)
26
17/10/2022
Second
+ ax.plot_wireframe(X, Y, Z, color='green')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
27
Third
28
17/10/2022
Result
29
3D Bar Plots
30
17/10/2022
+ Each bar in a bar plot always needs 2 things: a position and a size.
+ With 3D bar plots three variables such as x, y, z are needed.
+ We’ll select the z axis to encode the height of each bar; therefore,
each bar will start at z = 0 and have a size that is proportional to the
value we are trying to visualise. The x and y positions will represent
the coordinates of the bar across the 2D plane of z = 0.
+ We’ll set the x and y size of each bar to a value of 1 so that all the
bars have the same shape.
31
Bar plot
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
x3 = [1,2,3,4,5,6,7,8,9,10]
y3 = [5,6,7,8,2,5,6,3,7,2]
z3 = np.zeros(10)
dx = np.ones(10)
dy = np.ones(10)
dz = [1,2,3,4,5,6,7,8,9,10]
ax1.bar3d(x3, y3, z3, dx, dy, dz)
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')
plt.show()
32