matplotlib 自定义颜色图如何工作
How does matplotlib custom colormaps work
我试图用文档中的示例创建自定义颜色图,但我不知道如何设置颜色范围。
https://matplotlib.org/2.0.2/examples/pylab_examples/custom_cmap.html
这是最接近我需要的:(全绿从 1.0 到 0.916,全黄从 0.916 到 0.75,全红低于 0.75)
cdict1 = {'red': ((0.0, 1.0, 1.0),
(0.75, 1.0, 1.0),
(1.0, 0.0, 0.0)),
'green': ((0.0, 0.0, 0.0),
(0.75, 1.0, 1.0),
(0.91666666666, 1.0, 1.0),
(1.0, 1.0, 1.0)),
'blue': ((0.0, 0.0, 0.0),
(0.5, 0.0, 0.0),
(1.0, 0.0, 0.0))}
我不明白为什么这个颜色图是颜色之间的平滑过渡。
要创建具有 3 种边界不等的固定颜色的颜色图,推荐的方法使用 BoundaryNorm
。
如果您真的只想使用颜色图,可以创建一个 from a list of colors。
A LinearSegmentedColormap
使用给定值的特定颜色进行平滑过渡。为了使其适用于固定颜色,可以将这些值设置为相等。该函数要么以“旧”方式处理 rgb 值,要么使用 (value, color)
对列表 (LinearSegmentedColormap.from_list()
).
下面的示例代码展示了它是如何工作的:
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm, LinearSegmentedColormap
import numpy as np
x, y = np.random.rand(2, 100)
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(14, 4))
# working with a BoundaryNorm
cmap1 = ListedColormap(['red', 'yellow', 'green'])
norm1 = BoundaryNorm([0, 0.75, 0.916, 1], ncolors=3)
scat1 = ax1.scatter(x, y, c=y, cmap=cmap1, norm=norm1)
plt.colorbar(scat1, ax=ax1, spacing='proportional')
ax1.set_title('working with BoundaryNorm')
# creating a special colormap
colors = ['green' if c > 0.916 else 'red' if c < 0.75 else 'yellow' for c in np.linspace(0, 1, 256)]
cmap2 = ListedColormap(colors)
scat2 = ax2.scatter(x, y, c=y, cmap=cmap2, vmin=0, vmax=1)
plt.colorbar(scat2, ax=ax2)
ax2.set_title('special list of colors')
cmap3 = LinearSegmentedColormap.from_list('', [(0, 'red'), (0.75, 'red'), (0.75, 'yellow'), (0.916, 'yellow'),
(0.916, 'green'), (1, 'green')])
scat3 = ax3.scatter(x, y, c=y, cmap=cmap3, vmin=0, vmax=1)
plt.colorbar(scat3, ax=ax3)
ax3.set_title('LinearSegmentedColormap')
plt.tight_layout()
plt.show()
plt.colorbar
的 spacing='proportional'
选项显示了它们比例位置的边界。默认显示 3 个等距边界以及值。
我试图用文档中的示例创建自定义颜色图,但我不知道如何设置颜色范围。
https://matplotlib.org/2.0.2/examples/pylab_examples/custom_cmap.html
这是最接近我需要的:(全绿从 1.0 到 0.916,全黄从 0.916 到 0.75,全红低于 0.75)
cdict1 = {'red': ((0.0, 1.0, 1.0),
(0.75, 1.0, 1.0),
(1.0, 0.0, 0.0)),
'green': ((0.0, 0.0, 0.0),
(0.75, 1.0, 1.0),
(0.91666666666, 1.0, 1.0),
(1.0, 1.0, 1.0)),
'blue': ((0.0, 0.0, 0.0),
(0.5, 0.0, 0.0),
(1.0, 0.0, 0.0))}
我不明白为什么这个颜色图是颜色之间的平滑过渡。
要创建具有 3 种边界不等的固定颜色的颜色图,推荐的方法使用 BoundaryNorm
。
如果您真的只想使用颜色图,可以创建一个 from a list of colors。
A LinearSegmentedColormap
使用给定值的特定颜色进行平滑过渡。为了使其适用于固定颜色,可以将这些值设置为相等。该函数要么以“旧”方式处理 rgb 值,要么使用 (value, color)
对列表 (LinearSegmentedColormap.from_list()
).
下面的示例代码展示了它是如何工作的:
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm, LinearSegmentedColormap
import numpy as np
x, y = np.random.rand(2, 100)
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(14, 4))
# working with a BoundaryNorm
cmap1 = ListedColormap(['red', 'yellow', 'green'])
norm1 = BoundaryNorm([0, 0.75, 0.916, 1], ncolors=3)
scat1 = ax1.scatter(x, y, c=y, cmap=cmap1, norm=norm1)
plt.colorbar(scat1, ax=ax1, spacing='proportional')
ax1.set_title('working with BoundaryNorm')
# creating a special colormap
colors = ['green' if c > 0.916 else 'red' if c < 0.75 else 'yellow' for c in np.linspace(0, 1, 256)]
cmap2 = ListedColormap(colors)
scat2 = ax2.scatter(x, y, c=y, cmap=cmap2, vmin=0, vmax=1)
plt.colorbar(scat2, ax=ax2)
ax2.set_title('special list of colors')
cmap3 = LinearSegmentedColormap.from_list('', [(0, 'red'), (0.75, 'red'), (0.75, 'yellow'), (0.916, 'yellow'),
(0.916, 'green'), (1, 'green')])
scat3 = ax3.scatter(x, y, c=y, cmap=cmap3, vmin=0, vmax=1)
plt.colorbar(scat3, ax=ax3)
ax3.set_title('LinearSegmentedColormap')
plt.tight_layout()
plt.show()
plt.colorbar
的 spacing='proportional'
选项显示了它们比例位置的边界。默认显示 3 个等距边界以及值。