How to add wireframe, surface, and triangular surface plots

By considering the plot not just as a bunch of points, but as something filled in, we can use either the wireframe keyword or the wireframe method on the new axes, as shown here:

# Wireframe plot: r/c, stride & count
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x,y = np.meshgrid(np.arange(-10,10,0.5), np.arange(-10,10,0.5))
r = np.linalg.norm([x,y], axis=0)
goldstone = -160*np.power(r,2)+np.power(r,4)
ax.plot_wireframe(x,y,goldstone)
  1. In the preceding snippet, we pass x, y and goldstone, which gives us a nice surface, as shown here:

  1. In order to change how Matplotlib decides where to put this wireframe, there are two options: we have stride and count. This includes both the row stride and the column stride. By default, it is set to 1. We will double that, as shown in the following code:
# Wireframe plot: r/c, stride & count
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x,y = np.meshgrid(np.arange(-10,10,0.5), np.arange(-10,10,0.5))
r = np.linalg.norm([x,y], axis=0)
goldstone = -160*np.power(r,2)+np.power(r,4)
ax.plot_wireframe(x,y,goldstone, rstride=4)

We can see that the code generates fewer of these lines in the wireframe, basically jumping to every second point, as shown here:

We can also see a different output by changing rstride to 10.

The same can be applied to the columns as well. Hence, when we change from rstride to cstride, we change the total number of lines.

  1. In order to fill the curve in, we use the surface plot method, so that, rather than actually plotting the wireframe, we plot the surface, which also takes these stride arguments. But, by default, the stride is not 1, but actually 10; hence, to get the same kind of resolution as before, we have to manually set the strides down to 1, as shown here:
# Surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x,y = np.meshgrid(np.arange(-10,10,0.5), np.arange(-10,10,0.5))
r = np.linalg.norm([x,y], axis=0)
goldstone = -160*np.power(r,2)+np.power(r,4)
ax.plot_surface(x,y,goldstone, rstride=1, cstride=1)

The output of the preceding code is as follows:

  1. To pass a color map argument for a colored surface plot, pass a color which will simply change the color of the surface. We will pass in cmap='viridis' and get the same resolution as before, as shown in the following output:

So, this is a nice way of getting visually rich plots that show a lot of information in 3D.

  1. Next, to get the connections in our 3D plot to be made based on nearest neighbors, rather than by the array order, we use the tri-surface plot:
# Tri-surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x,y = np.meshgrid(np.arange(-10,10,0.5), np.arange(-10,10,0.5))
r = np.linalg.norm([x,y], axis=0)
goldstone = -100*np.power(r,2)+np.power(r,4)
x = x[r<10]
y = y[r<10]
goldstone = goldstone[r<10]
ax.plot_trisurf(x,y,goldstone)
  1. Here, we generate a filled-in surface, where triangles are created based on neighbor nearness, as shown here:

..................Content has been hidden....................

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