使用内置的颜色图很简单,只需将所需颜色图的名称(如在颜色图参考中给出)传递给期望的绘图功能(例如pcolormesh或contourf),通常以cmap关键字参数的形式进行:
importmatplotlib.pyplotas plt import numpy as np plt.figure() plt.pcolormesh(np.random.rand(20,20),cmap='hot') plt.show()
色图对于在二维图上可视化三维数据特别有用,但是良好的色图也可以使正确的三维图更清晰:
importmatplotlib.pyplotas plt from mpl_toolkits.mplot3d import Axes3D frommatplotlib.tickerimport 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()