3D Plotting with Matplotlib in Python: Syntax, Examples, and Visualizations
This article introduces Python's matplotlib 3D plotting capabilities, demonstrating how to create line, scatter, and surface visualizations with step-by-step code examples and animated GIFs that illustrate the impact of parameters such as learning rate and projection settings.
Many situations require visualizing internal relationships between data; using plots can reveal patterns.
The following animated GIFs show how 3‑D visualizations in matplotlib help understand logistic‑regression network performance and the effect of different learning rates on algorithm convergence.
➤01 3D Plot
1. Basic Syntax
After installing matplotlib , the mpl_toolkits.mplot3d module is available automatically.
<code># Importing Libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
# 3D Plotting
fig = plt.figure()
ax = plt.axes(projection="3d")
# Labeling
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
</code>2. Python Cmd
Insert the above statements into a Python command line or script.
3. Example
(1) Ex1
<code>#!/usr/local/bin/python
# -*- coding: gbk -*-
# TEST2.PY -- by Dr. ZhuoQing 2020-11-16
from headm import *
from mpl_toolkits.mplot3d import axes3d
ax = plt.axes(projection='3d')
x = [1,2,3,4,5,6,7,8,9]
y = [2,3,4,6,7,8,9,5,1]
z = [5,6,2,4,8,6,5,6,1]
ax.plot3D(x,y,z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
</code>Animated GIF demonstrates the 3D plot.
(2) Ex2
<code>from mpl_toolkits.mplot3d import axes3d
ax = plt.axes(projection='3d')
angle = linspace(0, 2*pi*5, 400)
x = cos(angle)
y = sin(angle)
z = linspace(0, 5, 400)
ax.plot3D(x,y,z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
</code>Animated GIF shows a parametric 3D curve.
(3) Ex3
<code>import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4*np.pi, 4*np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
plt.show()
</code>Animated GIF illustrates the 3D parametric curve.
➤02 Scatter Plot
Replace plot3D with scatter to create a 3‑D scatter plot.
<code>from mpl_toolkits.mplot3d import axes3d
ax = plt.axes(projection='3d')
angle = linspace(0, 2*pi*5, 40)
x = cos(angle)
y = sin(angle)
z = linspace(0, 5, 40)
ax.scatter(x, y, z, color='b')
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
</code>Animated GIF shows the scatter example.
➤03 3D Surface Plot
(1) Ex1
<code>#!/usr/local/bin/python
# -*- coding: gbk -*-
# TEST2.PY -- by Dr. ZhuoQing 2020-11-16
from headm import *
from mpl_toolkits.mplot3d import axes3d
ax = plt.axes(projection='3d')
x = arange(-5, 5, 0.1)
y = arange(-5, 5, 0.1)
x, y = meshgrid(x, y)
R = sqrt(x**2 + y**2)
z = sin(R)
ax.plot_surface(x, y, z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
</code>Animated GIF displays a colored surface.
(2) Example with color map
<code>from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
</code>Animated GIF shows the colored 3‑D surface with a color bar.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.