3d PlotsΒΆ

This notebook will demonstrate plots depicting 3 dimensional surfaces. I put contour plots into this notebook not because they are three dimensional plots themselves, but because they represent 3d surfaces.

[1]:
import numpy as np
from scipy.stats import multivariate_normal
import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 16})
from matplotlib import cm
from matplotlib.ticker import LinearLocator
[36]:
x1, x2 = np.meshgrid(np.arange(-4, 4, 0.25),
                     np.arange(-4, 4, 0.25))
xx = np.dstack((x1, x2))
[37]:
mvn = multivariate_normal([0.0, 0.0], [[1.0, 0.0], [0.0, 1.0]])
z = mvn.pdf(xx)
[38]:
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

surf = ax.plot_surface(x1, x2, z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_xlabel("$x_1$")
ax.set_ylabel("$x_2$")

ax.set_zlim(0, np.max(z) + 0.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
[38]:
<matplotlib.colorbar.Colorbar at 0x11b80c070>
../_images/notebooks_3d_plots_5_1.png

A contour plot then projects this surface plot onto the \(xy\)-plane and represents height by the color alone.

[41]:
fig2, ax2 = plt.subplots()
ax2.contour(x1, x2, z, cmap = cm.coolwarm)
ax2.set_aspect('equal')
../_images/notebooks_3d_plots_7_0.png