反转自定义颜色图
reverse a custom colormap
我想反转我的自定义颜色图,如下所示
import matplotlib.cm as cm
import matplotlib as mpl
cm.register_cmap(name='owb',
data = {'red': ((0.0, 1.0, 1.0), # orange
(0.5, 1.0, 1.0), #
(1.0, 0.0, 0.0)), # blue
'green': ((0.0, 0.6, 0.6), # orange
(0.5, 1.0, 1.0), #
(1.0, 0.0, 0.0)), # blue
'blue': ((0.0, 0.0, 0.0), # orange
(0.5, 1.0, 1.0), #
(1.0, 1.0, 1.0)) # blue
})
现在,由于我一直在寻找这个问题的解决方案,在这样做的过程中,我遇到了这个线程:
colormap reversed
我在这里尝试使用 .reversed()
:
data = {'red': ((0.0, 1.0, 1.0), # orange
(0.5, 1.0, 1.0), #
(1.0, 0.0, 0.0)), # blue
'green': ((0.0, 0.6, 0.6), # orange
(0.5, 1.0, 1.0), #
(1.0, 0.0, 0.0)), # blue
'blue': ((0.0, 0.0, 0.0), # orange
(0.5, 1.0, 1.0), #
(1.0, 1.0, 1.0)) # blue
}
owb = mpl.colors.ListedColormap('OrWhBlu', data)
owb_reversed = owb.reversed()
这里我得到了以下错误:
TypeError: unsupported operand type(s) for +: 'dict' and 'str'
但我就是没能成功。
请让我知道我做错了什么。
干杯
编辑:
我尝试了 Kostas 的解决方案,并且由于我必须在另一个需要 cmap 名称的函数中使用 colormap_r 我尝试以这种方式解决它:
cmap_r = cm.register_cmap(name="owb_r", cmap=owb_r)
调用的函数:
plt.pcolormesh(xx, yy, -Z, cmap="owb_r")
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=y_2d, cmap="owb_r",
edgecolors='k')
现在我得到以下错误:
ValueError: Invalid RGBA argument: 'u'
您需要 LinearSegmentedColormap
,而不是 ListedColormap
。然后你需要注册它的reversed()
版本。
import numpy as np
import matplotlib.cm as cm
import matplotlib as mpl
import matplotlib.pyplot as plt
colors = [(1.0, 0.6, 0.0), "white","blue"]
cm.register_cmap(cmap=mpl.colors.LinearSegmentedColormap.from_list("owb", colors).reversed())
fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(12,12), cmap="owb_r")
fig.colorbar(im)
plt.show()
我想反转我的自定义颜色图,如下所示
import matplotlib.cm as cm
import matplotlib as mpl
cm.register_cmap(name='owb',
data = {'red': ((0.0, 1.0, 1.0), # orange
(0.5, 1.0, 1.0), #
(1.0, 0.0, 0.0)), # blue
'green': ((0.0, 0.6, 0.6), # orange
(0.5, 1.0, 1.0), #
(1.0, 0.0, 0.0)), # blue
'blue': ((0.0, 0.0, 0.0), # orange
(0.5, 1.0, 1.0), #
(1.0, 1.0, 1.0)) # blue
})
现在,由于我一直在寻找这个问题的解决方案,在这样做的过程中,我遇到了这个线程:
colormap reversed
我在这里尝试使用 .reversed()
:
data = {'red': ((0.0, 1.0, 1.0), # orange
(0.5, 1.0, 1.0), #
(1.0, 0.0, 0.0)), # blue
'green': ((0.0, 0.6, 0.6), # orange
(0.5, 1.0, 1.0), #
(1.0, 0.0, 0.0)), # blue
'blue': ((0.0, 0.0, 0.0), # orange
(0.5, 1.0, 1.0), #
(1.0, 1.0, 1.0)) # blue
}
owb = mpl.colors.ListedColormap('OrWhBlu', data)
owb_reversed = owb.reversed()
这里我得到了以下错误:
TypeError: unsupported operand type(s) for +: 'dict' and 'str'
但我就是没能成功。
请让我知道我做错了什么。
干杯
编辑: 我尝试了 Kostas 的解决方案,并且由于我必须在另一个需要 cmap 名称的函数中使用 colormap_r 我尝试以这种方式解决它:
cmap_r = cm.register_cmap(name="owb_r", cmap=owb_r)
调用的函数:
plt.pcolormesh(xx, yy, -Z, cmap="owb_r")
plt.scatter(X_2d[:, 0], X_2d[:, 1], c=y_2d, cmap="owb_r",
edgecolors='k')
现在我得到以下错误:
ValueError: Invalid RGBA argument: 'u'
您需要 LinearSegmentedColormap
,而不是 ListedColormap
。然后你需要注册它的reversed()
版本。
import numpy as np
import matplotlib.cm as cm
import matplotlib as mpl
import matplotlib.pyplot as plt
colors = [(1.0, 0.6, 0.0), "white","blue"]
cm.register_cmap(cmap=mpl.colors.LinearSegmentedColormap.from_list("owb", colors).reversed())
fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(12,12), cmap="owb_r")
fig.colorbar(im)
plt.show()