从颜色列表创建颜色映射

有关创建和操作色彩映射的更多详细信息,请参阅在Matplotlib中创建色彩映射

可以使用LinearSegmentedColormap的from_list()方法从颜色列表创建颜色映射。您必须传递一个RGB元组列表,用于定义从0到1的颜色混合。

创建自定义色彩映射

也可以为色彩映射创建自定义映射。 这是通过创建字典来实现的,该字典指定RGB通道如何从cmap的一端变为另一端。

示例:假设您希望红色在下半部分从0增加到1,绿色在中间半部分增加到相同,而在上半部分则为蓝色。 然后你会用:

  1. cdict = {'red': ((0.0, 0.0, 0.0),
  2. (0.5, 1.0, 1.0),
  3. (1.0, 1.0, 1.0)),
  4. 'green': ((0.0, 0.0, 0.0),
  5. (0.25, 0.0, 0.0),
  6. (0.75, 1.0, 1.0),
  7. (1.0, 1.0, 1.0)),
  8. 'blue': ((0.0, 0.0, 0.0),
  9. (0.5, 0.0, 0.0),
  10. (1.0, 1.0, 1.0))}

如果在这个例子中,r,g和b组件中没有不连续性,那么它很简单:上面每个元组的第二个和第三个元素是相同的 - 称之为“y”。 第一个元素(“x”)定义了0到1整个范围内的插值间隔,它必须跨越整个范围。换句话说,x的值将0到1范围划分为一组段,并且y给出每个段的端点颜色值。

现在考虑绿色。cdict[‘green’]表示对于0 <= x <= 0.25,y为零; 没有绿色。0.25 < x <= 0.75,y从0到1线性变化.x > 0.75,y保持为1,全绿色。

如果存在不连续性,则会更复杂一些。将给定颜色的cdict条目中每行中的3个元素标记为(x, y0, y1)。然后,对于x[i] 和 x[i + 1]之间的x值,在 y1[i] 和 y0[i + 1] 之间内插颜色值。

回到指南里的例子,看看cdict[‘red’]; 因为y0!= y1,它表示对于x从0到0.5,红色从0增加到1,但随后它向下跳跃,因此对于x从0.5到1,红色从0.7增加到1.绿色斜坡从0开始 当x从0变为0.5时变为1,然后跳回0,当x从0.5变为1时,斜坡变回1:

  1. row i: x y0 y1
  2. /
  3. /
  4. row i+1: x y0 y1

以上是试图表明对于x[i]到 x[i + 1] 范围内的x,插值在 y1[i] 和 y0[i + 1] 之间。因此,永远不会使用y0[0] 和 y1[-1]。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.colors import LinearSegmentedColormap
  4. # Make some illustrative fake data:
  5. x = np.arange(0, np.pi, 0.1)
  6. y = np.arange(0, 2 * np.pi, 0.1)
  7. X, Y = np.meshgrid(x, y)
  8. Z = np.cos(X) * np.sin(Y) * 10

—- 列表中的色彩映射 —-

  1. colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B
  2. n_bins = [3, 6, 10, 100] # Discretizes the interpolation into bins
  3. cmap_name = 'my_list'
  4. fig, axs = plt.subplots(2, 2, figsize=(6, 9))
  5. fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
  6. for n_bin, ax in zip(n_bins, axs.ravel()):
  7. # Create the colormap
  8. cm = LinearSegmentedColormap.from_list(
  9. cmap_name, colors, N=n_bin)
  10. # Fewer bins will result in "coarser" colomap interpolation
  11. im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm)
  12. ax.set_title("N bins: %s" % n_bin)
  13. fig.colorbar(im, ax=ax)

从颜色列表创建颜色映射示例

—- 自定义色彩映射 —-

  1. cdict1 = {'red': ((0.0, 0.0, 0.0),
  2. (0.5, 0.0, 0.1),
  3. (1.0, 1.0, 1.0)),
  4. 'green': ((0.0, 0.0, 0.0),
  5. (1.0, 0.0, 0.0)),
  6. 'blue': ((0.0, 0.0, 1.0),
  7. (0.5, 0.1, 0.0),
  8. (1.0, 0.0, 0.0))
  9. }
  10. cdict2 = {'red': ((0.0, 0.0, 0.0),
  11. (0.5, 0.0, 1.0),
  12. (1.0, 0.1, 1.0)),
  13. 'green': ((0.0, 0.0, 0.0),
  14. (1.0, 0.0, 0.0)),
  15. 'blue': ((0.0, 0.0, 0.1),
  16. (0.5, 1.0, 0.0),
  17. (1.0, 0.0, 0.0))
  18. }
  19. cdict3 = {'red': ((0.0, 0.0, 0.0),
  20. (0.25, 0.0, 0.0),
  21. (0.5, 0.8, 1.0),
  22. (0.75, 1.0, 1.0),
  23. (1.0, 0.4, 1.0)),
  24. 'green': ((0.0, 0.0, 0.0),
  25. (0.25, 0.0, 0.0),
  26. (0.5, 0.9, 0.9),
  27. (0.75, 0.0, 0.0),
  28. (1.0, 0.0, 0.0)),
  29. 'blue': ((0.0, 0.0, 0.4),
  30. (0.25, 1.0, 1.0),
  31. (0.5, 1.0, 0.8),
  32. (0.75, 0.0, 0.0),
  33. (1.0, 0.0, 0.0))
  34. }
  35. # Make a modified version of cdict3 with some transparency
  36. # in the middle of the range.
  37. cdict4 = {**cdict3,
  38. 'alpha': ((0.0, 1.0, 1.0),
  39. # (0.25,1.0, 1.0),
  40. (0.5, 0.3, 0.3),
  41. # (0.75,1.0, 1.0),
  42. (1.0, 1.0, 1.0)),
  43. }

现在我们将使用此示例来说明处理自定义色彩映射的3种方法。首先,最直接和明确的:

  1. blue_red1 = LinearSegmentedColormap('BlueRed1', cdict1)

其次,显式创建地图并注册它。与第一种方法一样,此方法适用于任何类型的Colormap,而不仅仅是LinearSegmentedColormap:

  1. blue_red2 = LinearSegmentedColormap('BlueRed2', cdict2)
  2. plt.register_cmap(cmap=blue_red2)

第三,仅对于LinearSegmentedColormap,将所有内容保留为register_cmap:

  1. plt.register_cmap(name='BlueRed3', data=cdict3) # optional lut kwarg
  2. plt.register_cmap(name='BlueRedAlpha', data=cdict4)

制作图:

  1. fig, axs = plt.subplots(2, 2, figsize=(6, 9))
  2. fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
  3. # Make 4 subplots:
  4. im1 = axs[0, 0].imshow(Z, interpolation='nearest', cmap=blue_red1)
  5. fig.colorbar(im1, ax=axs[0, 0])
  6. cmap = plt.get_cmap('BlueRed2')
  7. im2 = axs[1, 0].imshow(Z, interpolation='nearest', cmap=cmap)
  8. fig.colorbar(im2, ax=axs[1, 0])
  9. # Now we will set the third cmap as the default. One would
  10. # not normally do this in the middle of a script like this;
  11. # it is done here just to illustrate the method.
  12. plt.rcParams['image.cmap'] = 'BlueRed3'
  13. im3 = axs[0, 1].imshow(Z, interpolation='nearest')
  14. fig.colorbar(im3, ax=axs[0, 1])
  15. axs[0, 1].set_title("Alpha = 1")
  16. # Or as yet another variation, we can replace the rcParams
  17. # specification *before* the imshow with the following *after*
  18. # imshow.
  19. # This sets the new default *and* sets the colormap of the last
  20. # image-like item plotted via pyplot, if any.
  21. #
  22. # Draw a line with low zorder so it will be behind the image.
  23. axs[1, 1].plot([0, 10 * np.pi], [0, 20 * np.pi], color='c', lw=20, zorder=-1)
  24. im4 = axs[1, 1].imshow(Z, interpolation='nearest')
  25. fig.colorbar(im4, ax=axs[1, 1])
  26. # Here it is: changing the colormap for the current image and its
  27. # colorbar after they have been plotted.
  28. im4.set_cmap('BlueRedAlpha')
  29. axs[1, 1].set_title("Varying alpha")
  30. #
  31. fig.suptitle('Custom Blue-Red colormaps', fontsize=16)
  32. fig.subplots_adjust(top=0.9)
  33. plt.show()

从颜色列表创建颜色映射示例2

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.imshow
  3. matplotlib.pyplot.imshow
  4. matplotlib.figure.Figure.colorbar
  5. matplotlib.pyplot.colorbar
  6. matplotlib.colors
  7. matplotlib.colors.LinearSegmentedColormap
  8. matplotlib.colors.LinearSegmentedColormap.from_list
  9. matplotlib.cm
  10. matplotlib.cm.ScalarMappable.set_cmap
  11. matplotlib.pyplot.register_cmap
  12. matplotlib.cm.register_cmap

下载这个示例