Time for action – plotting in three dimensions

We will plot in three dimensions a simple three-dimensional function:

Time for action – plotting in three dimensions
  1. We need to use the 3d keyword to specify a three-dimensional projection for the plot.
    ax = fig.add_subplot(111, projection=’3d’)
  2. To create a square two-dimensional grid, we will use the meshgrid function. This will be used to initialize the x and y values.
    u = np.linspace(-1, 1, 100)
    
    x, y = np.meshgrid(u, u)
  3. We will specify the row strides, column strides, and the color map for the surface plot. The strides determine the size of the "tiles" on the surface. The choice for colormap is a matter of taste.
    ax.plot_surface(x, y, z,  rstride=4, cstride=4,cmap=cm.YlGnBu_r)

    The result is the following 3D plot:

    Time for action – plotting in three dimensions

What just happened?

We created a plot of a three dimensional function (see three_d.py):

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

fig = plt.figure()
ax = fig.add_subplot(111, projection=’3d’)

u = np.linspace(-1, 1, 100)

x, y = np.meshgrid(u, u)
z = x ** 2 + y ** 2
ax.plot_surface(x, y, z,  rstride=4, cstride=4, cmap=cm.YlGnBu_r)

plt.show()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset