算了,自问自答一下,用matplotlib.colors里面的LinearSegmentedColormap类可以自定义color_map。
代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# Create custom colormaps
cdict = {'red': ((0.0, 1.0, 1.0), # Full red at the first stop
(0.5, 0.0, 0.0), # No red at second stop
(1.0, 1.0, 1.0)), # Full red at final stop
#
'green': ((0.0, 0.0, 0.0), # No green at all stop
(0.5, 0.0, 0.0), #
(1.0, 0.0, 0.0)), #
#
'blue': ((0.0, 0.0, 0.0), # No blue at first stop
(0.5, 1.0, 1.0), # Full blue at second stop
(1.0, 0.0, 0.0))} # No blue at final stop
cmap = LinearSegmentedColormap('Rd_Bl_Rd', cdict, 256)
im = np.outer(np.ones(10), np.linspace(0, 255, 256))
fig = plt.figure(figsize=(9, 2))
ax = fig.add_subplot('111')
ax.set_xticks(np.linspace(0, 255, 3))
ax.set_xticklabels([0, 0.5, 1])
ax.set_yticks([])
ax.set_yticklabels([])
ax.imshow(im, interpolation='nearest', cmap=cmap)
示例图: