Lect 09 Mpl2
Lect 09 Mpl2
Lecture 9
Application of Python language for Data Science and Artificial Intelligence
Matplotlib 2
(lecture without lecture)
We enable three-dimensional plots by importing the mplot3d toolkit, included with the main
Matplotlib installation.
With this 3D axes enabled, we can now plot a variety of three-dimensional plot types.
Once this submodule is imported, we can create a three-dimensional axes by passing the
keyword projection='3d' to any of the normal axes creation routines:
Task_01
plt.axes(projection='3d')
plt.show()
Task_02
ax = plt.axes(projection='3d')
# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'red')
# Data for three-dimensional scattered points
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='PuBu');
plt.show()
Task_03
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)[:, np.newaxis]
z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
plt.imshow(z, origin='lower', extent=[0, 5, 0, 5], cmap='viridis')
plt.colorbar()
plt.contour(X, Y, Z, colors='black');
#plt.contour(X, Y, Z, cmap='binary');
Task_05
(task 4 in 3D)
…
X, Y = np.meshgrid(x, y)
…
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
Task_06
(sombrero)
Replace the function from the previous example with the "sombrero" function:
Sometimes the default viewing angle is not optimal, in which case we can use the view_init
method to set the elevation and azimuthal angles.
Task_07
Elevation of 60 degrees (that is, 60 degrees above the x-y plane)
Azimuth of 35 degrees (that is, rotated 35 degrees counter-clockwise about the z-axis)
…
ax.set_zlabel('z');
ax.view_init(60, 35)
plt.show()
Two other types of three-dimensional plots that work on gridded data are:
• wireframes
• and surface plots.
These take a grid of values and project it onto the specified three dimensional surface, and can
make the resulting three-dimensional forms quite easy to visualize.
Task_08
…
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='gray')
ax.set_title('wireframe');
ax.set_xlabel('x')
…
A wireframe plot
A surface plot is like a wireframe plot, but each face of the wireframe is a filled polygon.
Adding a colormap to the filled polygons can aid perception of the topology of the surface
being visualized
Task_09
…
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none')
ax.set_title('surface');
…
Note that though the grid of values for a surface plot needs to be two-dimensional, it need not
be rectilinear.
Here is an example of creating a partial polar grid, which when used with the surface3D plot
can give us a slice into the function we’re visualizing.
Task_10
…
r = np.linspace(0, 6, 20)
theta = np.linspace(-0.9 * np.pi, 0.8 * np.pi, 40)
r, theta = np.meshgrid(r, theta)
X = r * np.sin(theta)
Y = r * np.cos(theta)
Z = f(X, Y)
…
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')
ax.set_title('cutout of a sombrero')
…
Surface Triangulations
(with random sampling)
Task_12
…
ax.plot_trisurf(X, Y, Z, cmap='viridis', edgecolor='none');
…
Task_13
…
theta = np.linspace(0, 2 * np.pi, 30)
w = np.linspace(-0.25, 0.25, 8)
w, theta = np.meshgrid(w, theta)
phi = 0.5 * theta
r = 1 + w * np.cos(phi)
x = np.ravel(r * np.cos(theta))
y = np.ravel(r * np.sin(theta))
z = np.ravel(w * np.sin(phi))
ax = plt.axes(projection='3d')
ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap='viridis', linewidths=0.2);
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
…