A 3D plot of a surface

We'll now plot a 3D plot, where the Sin function is plotted against the sum of the square values of the two axes:

>>> from mpl_toolkits.mplot3d import Axes3D
>>> fig = plt.figure()
>>> ax = Axes3D(fig)
>>> X = np.arange(-4, 4, 0.25)
>>> Y = np.arange(-4, 4, 0.25)
>>> X, Y = np.meshgrid(X, Y)
>>> R = np.sqrt(X**2 + Y**2)
>>> Z = np.sin(R)
>>> ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')

After the preceding code is executed we'll get the following output:

A 3D plot of a surface

In the preceding code, we defined the x and y axes with values ranging from -4 to 4. We created a coordinate matrix with meshgrid(), then squared the values of ...

Get Mastering Python for Data Science now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.