Book Image

matplotlib Plotting Cookbook

By : Alexandre Devert
Book Image

matplotlib Plotting Cookbook

By: Alexandre Devert

Overview of this book

Table of Contents (15 chapters)
matplotlib Plotting Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating 3D curve plots


As the previous recipe has demonstrated, what we have learned in the previous chapters stands true when creating three-dimensional figures. Let's confirm this by plotting 3D parametric curves. In this recipe, we keep the same dataset as in the previous recipe; that is, the Lorenz attractor.

How to do it…

In 2D, we draw curves by calling pyplot.plot(). As the previous recipe hinted, all we have to do here is set up an Axes3D instance and call its plot() method, as shown in the following code:

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

a, b, c = 10., 28., 8. / 3.
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((10000, 3))
X = np.array([.1, .0, .0])
for i in range(points.shape[0]):
  points[i], X = X, lorenz_map(X)

fig = plt.figure()
ax = fig.gca(projection...