为了回答这个问题,“bwr”颜色图的颜色可以通过import matplotlib.cm
print(matplotlib.cm.datad["bwr"])
哪个指纹
^{pr2}$
这就是["blue", "white", "red"]。在
但对于实际应用来说,这可能不是太有用。
要创建中间有更大范围的白色的颜色贴图,最好从颜色列表中创建颜色贴图,可能还有它们各自的值。在
为此,可以使用^{}方法。在from matplotlib.colors import LinearSegmentedColormap
colors = [(0, "blue"), (0.4, "white"), (0.6, "white"), (1, "red")]
cmap = LinearSegmentedColormap.from_list("bwwr", colors)
数字介于0和1之间,需要升序并表示相应颜色的“位置”。上面的方法将创建一个颜色贴图,渐变从蓝色到白色介于0和0.4之间,然后所有白色在0.4和0.6之间,然后从白色渐变到红色,介于0.6和1之间。在import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
colors = [(0, "blue"), (0.4, "white"), (0.6, "white"), (1, "red")]
cmap = LinearSegmentedColormap.from_list("bwwr", colors)
a = np.arange(0,100).reshape(10,10)
fig, (ax,ax2) = plt.subplots(ncols=2, figsize=(7,3))
im = ax.imshow(a, cmap="bwr")
fig.colorbar(im, ax=ax)
im2 = ax2.imshow(a, cmap=cmap)
fig.colorbar(im2, ax=ax2)
ax.set_title("bwr")
ax2.set_title("bwwr")
plt.show()