Using built-in colormaps is as simple as passing the name of the required colormap (as given in the colormaps reference) to the plotting function (such as pcolormesh
or contourf
) that expects it, usually in the form of a cmap
keyword argument:
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
plt.pcolormesh(np.random.rand(20,20),cmap='hot')
plt.show()
Colormaps are especially useful for visualizing three-dimensional data on two-dimensional plots, but a good colormap can also make a proper three-dimensional plot much clearer:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import LinearLocator
# generate example data
import numpy as np
x,y = np.meshgrid(np.linspace(-1,1,15),np.linspace(-1,1,15))
z = np.cos(x*np.pi)*np.sin(y*np.pi)
# actual plotting example
fig = plt.figure()
ax1 = fig.add_subplot(121, projection='3d')
ax1.plot_surface(x,y,z,rstride=1,cstride=1,cmap='viridis')
ax2 = fig.add_subplot(122)
cf = ax2.contourf(x,y,z,51,vmin=-1,vmax=1,cmap='viridis')
cbar = fig.colorbar(cf)
cbar.locator = LinearLocator(numticks=11)
cbar.update_ticks()
for ax in {ax1, ax2}:
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
ax.set_aspect('equal')
ax1.set_zlim([-1,1])
ax1.set_zlabel(r'$\cos(\pi x) \sin(\p i y)$')
plt.show()