Create 3D figure using matplotlib


Import Python modules

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

Create a 3D canvas

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

3D Canvas

Create a 3D spiral line

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

# generate some random data for a 3d spiral line
zline = np.linspace(0,15,1000)
xline = np.sin(zline)
yline = np.cos(zline)

ax.plot3D(xline, yline, zline)
[<mpl_toolkits.mplot3d.art3d.Line3D at 0x26088ece590>]

3D Spiral Line

Create 3D dots

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

zdata = 10 * np.random.random(50)
xdata = np.sin(zdata)
ydata = np.cos(zdata)

ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens')
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x2608be04bd0>

3D Dots


Author: wenvenn
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source wenvenn !