3D Charts

Matplotlib Basics

1 min read

Published Oct 5 2025


15
0
0
0

ChartsGraphsMatplotlibNumPyPandasPythonVisualisation

Matplotlib supports 3D plotting via the module mpl_toolkits.mplot3d.


You create a 3D Axes object by setting:

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

Then you can call 3D-specific methods like plot3D(), scatter3D(), plot_surface(), etc.






Line plot – ax.plot3D()

import matplotlib.pyplot as plt
import numpy as np

z = np.linspace(0, 5, 500)
x = z * np.sin(20 * z)
y = z * np.cos(20 * z)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot3D(x, y, z, 'gray')
plt.show()

Plots a 3D line connecting (x, y, z) points.


matplotlib 3d line example





Scatter plot – ax.scatter3D()

import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
colors = z

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter3D(x, y, z, c=colors, cmap='viridis')
plt.show()

Each point in 3D space can have its own color, size, and marker.


matplotlib 3d scatter example





Surface plot – ax.plot_surface()

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, cmap='plasma', edgecolor='none')
plt.show()

Visualises a continuous 3D surface over a grid.


matplotlib 3d surface example





Wireframe plot – ax.plot_wireframe()

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 30)
y = np.linspace(-5, 5, 30)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='black')
plt.show()

Same as surface plot but shows grid lines — great for structure visualisation.


matplotlib 3d wireframe example





Contour plot (3D projection) – ax.contour3D()

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='viridis')
plt.show()

Draws 3D contour lines (like elevation contours in a topographic map).


matplotlib 3d contor example





Useful Customisations

ax.set_title('3D Plot Example')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

# Change viewing angle
ax.view_init(elev=30, azim=45)
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact