Skip to content
Home » matplotlib Part 29 – Wireframes and Surface Plots

matplotlib Part 29 – Wireframes and Surface Plots

Spread the love

Wireframes and Surface Plots

Today we’ll be talking about wireframe and surface plots. We’ll be using our example from the previous lecture and modify it. Here it is again:

In [1]:
%matplotlib inline
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

x = y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.contour(X, Y, Z, 20, zdir='z', cmap=mpl.cm.Blues)
Out[1]:
<matplotlib.contour.QuadContourSet at 0x217e9ddd448>

To plot wireframes and surfaces you can use the plot_wireframe and plot_surface respectively. Let’s strt with a wireframe:

In [2]:
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_wireframe(X, Y, Z, cmap=mpl.cm.Blues)
Out[2]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x217ead73148>

You can also specify the maximum number of samples in each direction. By default these two keyword arguments are set to 50. Let’s change one of them to 10 to see the difference.

In [3]:
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_wireframe(X, Y, Z, rcount=10, cmap=mpl.cm.Blues)
Out[3]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x217eaf68308>

Now the grid is less dense in the vertical direction. Let’s now set ccount to 10 and use the default value of 50 for rcount:

In [4]:
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_wireframe(X, Y, Z, ccount=10, cmap=mpl.cm.Blues)
Out[4]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x217eae2cf88>

Now the grid is less dense in the horizontal direction. And now let’s set both these arguments to very small numbers:

In [5]:
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_wireframe(X, Y, Z, rcount=5, ccount=5, cmap=mpl.cm.Blues)
Out[5]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x217ead89348>

And now let’s have a look at surface plots. They are just like the wireframe plots with the difference that the faces are filled polygons. Let’s modify our example again:

In [6]:
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_surface(X, Y, Z, cmap=mpl.cm.Blues)
Out[6]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x217eafc1a88>

Just like with wireframes, you can use the rcount and ccount keyword arguments:

In [7]:
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_surface(X, Y, Z, rcount=20, ccount=10, cmap=mpl.cm.Blues)
Out[7]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x217eafcdfc8>

Spread the love

Leave a Reply