0% found this document useful (0 votes)
4 views7 pages

Lect 09 Mpl2

Uploaded by

danieltshuma64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Lect 09 Mpl2

Uploaded by

danieltshuma64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

App of Python for DS & AI Lecture 9

Lecture 9
Application of Python language for Data Science and Artificial Intelligence

Matplotlib 2
(lecture without lecture)

import matplotlib as mpl


import matplotlib.pyplot as plt #plt interface
from mpl_toolkits import mplot3d #toolkit

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()

An empty three-dimensional axes

Three-Dimensional Points and Lines


The most basic three-dimensional plot is a line or scatter plot created from sets of (x,
y, z) triples.

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');

Piotr Zaremba Summer 2025 1/7 Lect_09_mpl2.docx


App of Python for DS & AI Lecture 9

plt.show()

Points and lines in three dimensions

Check what color maps are available.


Choose a map other than the one in the example.
from matplotlib import colormaps
list(colormaps)

Plotting a two-dimensional function


(lecture 3 reminder, broadcasting)

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()

Two-Dimensional Contour Plots


Task_04
(meshgrid)
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

Piotr Zaremba Summer 2025 2/7 Lect_09_mpl2.docx


App of Python for DS & AI Lecture 9

plt.contour(X, Y, Z, colors='black');
#plt.contour(X, Y, Z, cmap='binary');

Three-Dimensional Contour Plots


Like two-dimensional ax.contour plots, ax.contour3D requires all the input data to be in the
form of two-dimensional regular grids, with the Z data evaluated at each point.
Here we’ll show a three-dimensional contour diagram of a three dimensional sinusoidal
function

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');

A three-dimensional contour plot

Task_06
(sombrero)
Replace the function from the previous example with the "sombrero" function:

def f(x, y):


return np.sin(np.sqrt(x ** 2 + y ** 2))
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)

ax = plt.axes(projection=’3d’)

Piotr Zaremba Summer 2025 3/7 Lect_09_mpl2.docx


App of Python for DS & AI Lecture 9

A three-dimensional „sombrero” contour plot

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()

Adjusting the view angle for a three-dimensional plot

Wireframes and Surface Plots

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')

Piotr Zaremba Summer 2025 4/7 Lect_09_mpl2.docx


App of Python for DS & AI Lecture 9

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');

A three-dimensional surface plot

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')

Piotr Zaremba Summer 2025 5/7 Lect_09_mpl2.docx


App of Python for DS & AI Lecture 9

A polar surface plot

Random sampling plot


Sometimes, instead of a uniformly sampled grid (Cartesian or polar) as in the previous
examples, we want to sample randomly.
Task_11

theta = 2 * np.pi * np.random.random(1000)
r = 6 * np.random.random(1000)

ax = plt.axes(projection='3d')
ax.scatter(X, Y, Z, c=Z, cmap='viridis', linewidth=0.5)

Surface Triangulations
(with random sampling)
Task_12

ax.plot_trisurf(X, Y, Z, cmap='viridis', edgecolor='none');

Visualizing of the Möbius strip

Task_13

Piotr Zaremba Summer 2025 6/7 Lect_09_mpl2.docx


App of Python for DS & AI Lecture 9


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))

from matplotlib.tri import Triangulation


tri = Triangulation(np.ravel(w), np.ravel(theta))

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)

Piotr Zaremba Summer 2025 7/7 Lect_09_mpl2.docx

You might also like