matplotlib中的反向颜色Map

lb3vh1jj  于 2023-10-24  发布在  其他
关注(0)|答案(7)|浏览(201)

我想知道如何简单地反转给定色彩Map表的颜色顺序,以便与plot_surface一起使用。

wydwbb8l

wydwbb8l1#

标准的色彩Map表也都有相反的版本。它们的名字都是一样的,但最后都加上了_r。(文档在这里)

643ylb08

643ylb082#

解决方案非常简单。假设你想使用“秋季”色彩Map方案。标准版本:

  1. cmap = matplotlib.cm.autumn

要反转色彩Map表的色谱,请使用get_cmap()函数并将'_r'附加到色彩Map表标题,如下所示:

  1. cmap_reversed = matplotlib.cm.get_cmap('autumn_r')
qgzx9mmu

qgzx9mmu3#

在matplotlib中,颜色Map表不是一个列表,但它包含了它的颜色列表colormap.colors。模块matplotlib.colors提供了一个函数ListedColormap()来从列表中生成颜色Map表。所以你可以通过执行以下操作来反转任何颜色Map表:

  1. colormap_r = ListedColormap(colormap.colors[::-1])
fcy6dtqo

fcy6dtqo4#

从Matplotlib 2.0开始,有一个reversed()方法用于ListedColormapLinearSegmentedColorMap对象,所以你可以只做
cmap_reversed = cmap.reversed()
这里是文档。

ybzsozfc

ybzsozfc5#

由于LinearSegmentedColormaps是基于红、绿色和蓝色的字典,因此有必要反转每个项目:

  1. import matplotlib.pyplot as plt
  2. import matplotlib as mpl
  3. def reverse_colourmap(cmap, name = 'my_cmap_r'):
  4. """
  5. In:
  6. cmap, name
  7. Out:
  8. my_cmap_r
  9. Explanation:
  10. t[0] goes from 0 to 1
  11. row i: x y0 y1 -> t[0] t[1] t[2]
  12. /
  13. /
  14. row i+1: x y0 y1 -> t[n] t[1] t[2]
  15. so the inverse should do the same:
  16. row i+1: x y1 y0 -> 1-t[0] t[2] t[1]
  17. /
  18. /
  19. row i: x y1 y0 -> 1-t[n] t[2] t[1]
  20. """
  21. reverse = []
  22. k = []
  23. for key in cmap._segmentdata:
  24. k.append(key)
  25. channel = cmap._segmentdata[key]
  26. data = []
  27. for t in channel:
  28. data.append((1-t[0],t[2],t[1]))
  29. reverse.append(sorted(data))
  30. LinearL = dict(zip(k,reverse))
  31. my_cmap_r = mpl.colors.LinearSegmentedColormap(name, LinearL)
  32. return my_cmap_r

看看它的工作原理:

  1. my_cmap
  2. <matplotlib.colors.LinearSegmentedColormap at 0xd5a0518>
  3. my_cmap_r = reverse_colourmap(my_cmap)
  4. fig = plt.figure(figsize=(8, 2))
  5. ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
  6. ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
  7. norm = mpl.colors.Normalize(vmin=0, vmax=1)
  8. cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = my_cmap, norm=norm,orientation='horizontal')
  9. cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = my_cmap_r, norm=norm, orientation='horizontal')

编辑

我没有得到user3445587的评论。它在彩虹色图上工作得很好:

  1. cmap = mpl.cm.jet
  2. cmap_r = reverse_colourmap(cmap)
  3. fig = plt.figure(figsize=(8, 2))
  4. ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
  5. ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
  6. norm = mpl.colors.Normalize(vmin=0, vmax=1)
  7. cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = cmap, norm=norm,orientation='horizontal')
  8. cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = cmap_r, norm=norm, orientation='horizontal')

但它特别适合自定义声明的颜色Map表,因为自定义声明的颜色Map表没有默认的_r。下面的例子取自http://matplotlib.org/examples/pylab_examples/custom_cmap.html

  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. blue_red1 = mpl.colors.LinearSegmentedColormap('BlueRed1', cdict1)
  11. blue_red1_r = reverse_colourmap(blue_red1)
  12. fig = plt.figure(figsize=(8, 2))
  13. ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
  14. ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
  15. norm = mpl.colors.Normalize(vmin=0, vmax=1)
  16. cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = blue_red1, norm=norm,orientation='horizontal')
  17. cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = blue_red1_r, norm=norm, orientation='horizontal')

展开查看全部
wsewodh2

wsewodh26#

目前还没有内置的方法来反转任意颜色Map,但一个简单的解决方案是实际上不修改颜色条,而是创建一个反转的Normalize对象:

  1. from matplotlib.colors import Normalize
  2. class InvertedNormalize(Normalize):
  3. def __call__(self, *args, **kwargs):
  4. return 1 - super(InvertedNormalize, self).__call__(*args, **kwargs)

然后你可以使用plot_surface和其他Matplotlib绘图函数,例如:

  1. inverted_norm = InvertedNormalize(vmin=10, vmax=100)
  2. ax.plot_surface(..., cmap=<your colormap>, norm=inverted_norm)

这将适用于任何Matplotlib色彩Map表。

qqrboqgw

qqrboqgw7#

有两种类型的LinearSegmentedColormaps。在某些情况下,_segmentdata是显式给出的,例如,对于jet:

  1. >>> cm.jet._segmentdata
  2. {'blue': ((0.0, 0.5, 0.5), (0.11, 1, 1), (0.34, 1, 1), (0.65, 0, 0), (1, 0, 0)), 'red': ((0.0, 0, 0), (0.35, 0, 0), (0.66, 1, 1), (0.89, 1, 1), (1, 0.5, 0.5)), 'green': ((0.0, 0, 0), (0.125, 0, 0), (0.375, 1, 1), (0.64, 1, 1), (0.91, 0, 0), (1, 0, 0))}

对于rainbow,_segmentdata如下所示:

  1. >>> cm.rainbow._segmentdata
  2. {'blue': <function <lambda> at 0x7fac32ac2b70>, 'red': <function <lambda> at 0x7fac32ac7840>, 'green': <function <lambda> at 0x7fac32ac2d08>}

我们可以在matplotlib的源代码中找到这些函数,它们如下所示:

  1. _rainbow_data = {
  2. 'red': gfunc[33], # 33: lambda x: np.abs(2 * x - 0.5),
  3. 'green': gfunc[13], # 13: lambda x: np.sin(x * np.pi),
  4. 'blue': gfunc[10], # 10: lambda x: np.cos(x * np.pi / 2)
  5. }

你想要的一切都已经在matplotlib中完成了,只需要调用cm.revcmap,它会反转这两种类型的segmentdata,所以

  1. cm.revcmap(cm.rainbow._segmentdata)

应该做的工作-你可以简单地创建一个新的LinearSegmentData。在revcmap中,基于SegmentData的函数的反转是用

  1. def _reverser(f):
  2. def freversed(x):
  3. return f(1 - x)
  4. return freversed

而其他列表则像往常一样颠倒

  1. valnew = [(1.0 - x, y1, y0) for x, y0, y1 in reversed(val)]

所以实际上你想要的是

  1. def reverse_colourmap(cmap, name = 'my_cmap_r'):
  2. return mpl.colors.LinearSegmentedColormap(name, cm.revcmap(cmap._segmentdata))
展开查看全部

相关问题