1
我非常喜欢“RdBu_r”颜色表,但我想剪掉蓝色和红色之间的白色部分。是否有捷径可寻?删除颜色表的一部分
我非常喜欢“RdBu_r”颜色表,但我想剪掉蓝色和红色之间的白色部分。是否有捷径可寻?删除颜色表的一部分
是的,但是就你而言,制作一个在蓝色和红色之间插值的色彩图可能更容易。
例如:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
cmap = LinearSegmentedColormap.from_list('name', ['red', 'blue'])
fig, ax = plt.subplots()
im = ax.imshow(np.random.random((10, 10)), cmap=cmap)
fig.colorbar(im)
plt.show()
注意,如果你想红的阴影不是一个HTML颜色的名称,你可以替换的确切RGB值。
然而,如果你确实想“切出中间的”另一种颜色表,你就应该评估它,不包括中间的范围,并创建一个新的颜色表:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# Remove the middle 40% of the RdBu_r colormap
interval = np.hstack([np.linspace(0, 0.3), np.linspace(0.7, 1)])
colors = plt.cm.RdBu_r(interval)
cmap = LinearSegmentedColormap.from_list('name', colors)
# Plot a comparison of the two colormaps
fig, axes = plt.subplots(ncols=2)
data = np.random.random((10, 10))
im = axes[0].imshow(data, cmap=plt.cm.RdBu_r, vmin=0, vmax=1)
fig.colorbar(im, ax=axes[0], orientation='horizontal', ticks=[0, 0.5, 1])
axes[0].set(title='Original Colormap')
im = axes[1].imshow(data, cmap=cmap, vmin=0, vmax=1)
fig.colorbar(im, ax=axes[1], orientation='horizontal', ticks=[0, 0.5, 1])
axes[1].set(title='New Colormap')
plt.show()