## Examples of Python Colormaps for Surface Plots

I was sitting in an airport recently, waiting to board a plane to fly to a conference. I wanted to make use of the time so I decided to review the different colormaps for use with Python matplotlib 3D surface plots. A colormap is a collection of colors that will be applied automatically to a surface plot, where the color depends on the value of the plot.

There are dozens and dozens of colormaps. I usually use “hsv” (Hue, Saturation, Value) or “jet” which range from red to organge to yellow to green to blue. But for some plots, different colormaps give a better visualization — it’s all very subjective. If you omit an argument value for the cmap parameter, you get shades of solid blue-gray which isn’t very nice for most plots.

A good reference for colormaps is the Web page at matplotlib.org/stable/tutorials/colors/colormaps.html. That page comments that the hsv colormap is not recommended for some visualizations, but I mildly disagree with that opinion.

For my demo surface function I used the simple sphere function, f(x,y) = x^2 + y^2. Seven of the colormaps I use most often. The last tab20 colormap is not a smooth gradient of colors and so the coloring of the surface is discrete rather than continuous. Large polyp stony corals are just that — stony-calcium bases with relatively large polyps (the flower-looking things). For some reason, corals have always frightened me somewhat — they look like man-eating plants that sting. But they’re pretty. Left: Red and green Blastomussa Wellsi. Center: Tricolor Gonistrea. Right: Orange tubastrea.

Here’s the demo code:

```# colormaps_demos.py

from matplotlib import cm  # color map
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

X = np.linspace(-2, 2, 100)
Y = np.linspace(-1, 2, 100)
X, Y = np.meshgrid(X, Y)
Z = X**2 + Y**2  # "sphere" function

fig = plt.figure()
ax = fig.gca(projection='3d')

# change the value of the "cmap" parameter
surf = ax.plot_surface(X, Y, Z, \
rstride=1, cstride=1, cmap=cm.hsv, \
edgecolor='darkred', linewidth=0.1)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x,y)')

# plt.savefig('sphere.jpg')
plt.show()
```
This entry was posted in Miscellaneous. Bookmark the permalink.