基于条件的颜色 numpy 2-dim 数组
Color numpy 2-dim array based on condition
我有一个二维数组,其值介于 -0.5 到 1.5 之间,如下所示:
我想用两种颜色给这个数组上色:
如果值 低于 0,我希望它是 green (但不是绿色的“阴影”,更像是绿色的另一个 cmap ),如果 大于 0 ,则具有 红色 (同样。不是一种特定的颜色,但更像是带有范围的 cmap)。所以结果应该看起来像这样(概念上):
我还没有找到应用这些“两个 cmap”的方法,所以我的最终目标是根据条件将这个数组着色为两种颜色。
编辑:示例:
np.array([0.1,0.15,0.2,0.5,0.7,
-0.1,-0.12,0.23,0.4,0.8,
-0.01,-.15,-0.1,0.2,0.5]).reshape(5,3)
为不同范围的数据使用不同的颜色是颜色图的工作,所以如果我是你,我会尽量避免绘制数据两次(即重叠两个不同颜色图的图,这看起来像你可能在想什么)。然后你可以选择一个现有的颜色图,或者制作一个...或者我可以想出另一种方法来做到这一点。
请注意,在此处的示例中,我使用的数据与您提供的数组略有不同。
选择一个现有的 'diverging' 颜色图很简单,可以这么说,只需确保将它居中放置在您想要设置截止点的位置即可。在你的情况下,这是零:
import numpy as np
import matplotlib.pyplot as plt
arr = np.array([ 0.1, 0.15, 0.2, 0.5, 0.7,
-0.1, -0.12, 0.23, 0.4, 0.8,
-0.01, -0.45, -0.1, 0.2, 0.5]).reshape(3, 5)
plt.imshow(arr, cmap="PiYG_r", vmin=-1.5, vmax=1.5) # Center it.
plt.colorbar(shrink=0.75)
这产生:
如果您想要自定义颜色图,可以使用 LinearSegmentedColormap
进行插值。这可能是我在你的情况下会做的。像这样使用它:
from matplotlib.colors import LinearSegmentedColormap
cdict = {'red': [[0.0, 0.0, 0.0], # Anchor, left, right.
[0.5, 0.0, 0.0], # Black at centre point.
[1.0, 1.0, 1.0]],
'green': [[0.0, 1.0, 1.0],
[0.5, 0.0, 0.0],
[1.0, 0.2, 0.2]],
'blue': [[0.0, 0.0, 0.0],
[0.5, 0.0, 0.0],
[1.0, 0.4, 0.4]]}
newcmp = LinearSegmentedColormap('newCmap', segmentdata=cdict, N=256)
plt.imshow(arr, cmap=newcmp, vmin=-1.5, vmax=1.5)
plt.colorbar(shrink=0.75)
这导致:
或者,您可以像这样构建图像数组来计算混合 RGB 图像:
red = arr.copy()
red[arr < 0] = np.nan
grn = arr.copy()
grn[arr >= 0] = np.nan
blu = np.zeros_like(red)*np.nan
rgb = np.dstack([red, np.abs(grn), blu])
plt.imshow(rgb)
这会产生:
这里的主要缺点是没有颜色条(它是一个 RGB 立方体)。
我有一个二维数组,其值介于 -0.5 到 1.5 之间,如下所示:
我想用两种颜色给这个数组上色:
如果值 低于 0,我希望它是 green (但不是绿色的“阴影”,更像是绿色的另一个 cmap ),如果 大于 0 ,则具有 红色 (同样。不是一种特定的颜色,但更像是带有范围的 cmap)。所以结果应该看起来像这样(概念上):
我还没有找到应用这些“两个 cmap”的方法,所以我的最终目标是根据条件将这个数组着色为两种颜色。
编辑:示例:
np.array([0.1,0.15,0.2,0.5,0.7,
-0.1,-0.12,0.23,0.4,0.8,
-0.01,-.15,-0.1,0.2,0.5]).reshape(5,3)
为不同范围的数据使用不同的颜色是颜色图的工作,所以如果我是你,我会尽量避免绘制数据两次(即重叠两个不同颜色图的图,这看起来像你可能在想什么)。然后你可以选择一个现有的颜色图,或者制作一个...或者我可以想出另一种方法来做到这一点。
请注意,在此处的示例中,我使用的数据与您提供的数组略有不同。
选择一个现有的 'diverging' 颜色图很简单,可以这么说,只需确保将它居中放置在您想要设置截止点的位置即可。在你的情况下,这是零:
import numpy as np
import matplotlib.pyplot as plt
arr = np.array([ 0.1, 0.15, 0.2, 0.5, 0.7,
-0.1, -0.12, 0.23, 0.4, 0.8,
-0.01, -0.45, -0.1, 0.2, 0.5]).reshape(3, 5)
plt.imshow(arr, cmap="PiYG_r", vmin=-1.5, vmax=1.5) # Center it.
plt.colorbar(shrink=0.75)
这产生:
如果您想要自定义颜色图,可以使用 LinearSegmentedColormap
进行插值。这可能是我在你的情况下会做的。像这样使用它:
from matplotlib.colors import LinearSegmentedColormap
cdict = {'red': [[0.0, 0.0, 0.0], # Anchor, left, right.
[0.5, 0.0, 0.0], # Black at centre point.
[1.0, 1.0, 1.0]],
'green': [[0.0, 1.0, 1.0],
[0.5, 0.0, 0.0],
[1.0, 0.2, 0.2]],
'blue': [[0.0, 0.0, 0.0],
[0.5, 0.0, 0.0],
[1.0, 0.4, 0.4]]}
newcmp = LinearSegmentedColormap('newCmap', segmentdata=cdict, N=256)
plt.imshow(arr, cmap=newcmp, vmin=-1.5, vmax=1.5)
plt.colorbar(shrink=0.75)
这导致:
或者,您可以像这样构建图像数组来计算混合 RGB 图像:
red = arr.copy()
red[arr < 0] = np.nan
grn = arr.copy()
grn[arr >= 0] = np.nan
blu = np.zeros_like(red)*np.nan
rgb = np.dstack([red, np.abs(grn), blu])
plt.imshow(rgb)
这会产生:
这里的主要缺点是没有颜色条(它是一个 RGB 立方体)。