Plotting a scalar field in 3D

So far, we have seen that 3D plots essentially mimic their 2D counterparts. However, there's more to matplotlib's three-dimensional plotting abilities. A lot of figures specific to the third dimension are also possible. Let's start with a simple use case: plotting a 2D scalar field as a 3D surface.

How to do it...

As usual, we are going to generate some test data, set up an Axes3D instance, and pass our data to it:

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, Y = np.meshgrid(x, y) Z = np.sinc(np.sqrt(X ** 2 + Y ** 2)) fig = plt.figure() ax = fig.gca(projection = '3d') ax.plot_surface(X, Y, ...

Get matplotlib Plotting Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.