def lorenz_map(x, dt = 1e-2):
x_dt = np.array([a * (x[1] - x[0]), x[0] * (b - x[2]) - x[1], x[0] * x[1] - c * x[2]])
return x + dt * x_dt
points = np.zeros((8000, 3))
x = np.array([.1, .0, .0])
for i in range(points.shape[0]):
points[i], x = x, lorenz_map(x)
Plotting
fig = plt.figure()
ax = fig.gca(projection = ‘3d’)
ax.set_xlabel(‘X axis’)
ax.set_ylabel(‘Y axis’)
ax.set_zlabel(‘Z axis’)
ax.set_title(‘Lorenz Attractor a=%0.2f b=%0.2f c=%0.2f’ % (a, b, c))
ax.plot(points[:, 0], points[:, 1], points[:, 2], c = ‘c’)
plt.show()
到目前为止,我们看到的3D绘图方式类似与相应的2D绘图方式,但也有许多特有的三维绘图功能,例如将二维标量场绘制为3D曲面:
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 256)
y = np.linspace(-3, 3, 256)
x_grid, y_grid = np.meshgrid(x, y)
z = np.sinc(np.sqrt(x_grid ** 2 + y_grid ** 2))
fig = plt.figure()
ax = fig.gca(projection = ‘3d’)
ax.plot_surface(x_grid, y_grid, z, cmap=cm.viridis)
plt.show()
Tips