Python 你能把颜色贴图的顺序颠倒过来吗?
我有一张branca彩色地图,可以翻一下订单吗 e、 g。 这: 为此: 代码:Python 你能把颜色贴图的顺序颠倒过来吗?,python,colormap,Python,Colormap,我有一张branca彩色地图,可以翻一下订单吗 e、 g。 这: 为此: 代码: cm.linear是LinearColormap实例的集合 LinearColormap具有一个颜色列表colors,以及用于将值线性映射到颜色的最小值vmin和最大值vmax LinearColormap初始值设定项将颜色列表以及最小值和最大值作为参数 def reversed_colormap(existing): return cm.LinearColormap( color
是cm.linear
实例的集合LinearColormap
具有一个颜色列表LinearColormap
,以及用于将值线性映射到颜色的最小值colors
和最大值vmin
vmax
初始值设定项将颜色列表以及最小值和最大值作为参数LinearColormap
def reversed_colormap(existing): return cm.LinearColormap( colors=list(reversed(existing.colors)), vmin=existing.vmin, vmax=existing.vmax )
因此,您可以在现有实例的基础上创建一个新的
LinearColormap
实例,方法是将反转的颜色传递给颜色列表,并使用相同的最小值和最大值
def reversed_colormap(existing):
return cm.LinearColormap(
colors=list(reversed(existing.colors)),
vmin=existing.vmin, vmax=existing.vmax
)
例如:
RdBu_11_reverse = reversed_colormap(cm.linear.RdBu_11)
试试这个:
import branca.colormap as cm
den_colormap = cm.linear.RdBu_11.colors
print('Original : \n', den_colormap)
den_colormap.reverse()
print('reversed : \n', den_colormap)
out = cm.LinearColormap(colors=den_colormap)
print('out : \n', out.colors)
输出:
Original :
[(0.403921568627451, 0.0, 0.12156862745098039, 1.0), (0.6980392156862745, 0.09411764705882353, 0.16862745098039217, 1.0), (0.8392156862745098, 0.3764705882352941, 0.30196078431372547, 1.0), (0.9568627450980393, 0.6470588235294118, 0.5098039215686274, 1.0), (0.9921568627450981, 0.8588235294117647, 0.7803921568627451, 1.0), (0.9686274509803922, 0.9686274509803922, 0.9686274509803922, 1.0), (0.8196078431372549, 0.8980392156862745, 0.9411764705882353, 1.0), (0.5725490196078431, 0.7725490196078432, 0.8705882352941177, 1.0), (0.2627450980392157, 0.5764705882352941, 0.7647058823529411, 1.0), (0.12941176470588237, 0.4, 0.6745098039215687, 1.0), (0.0196078431372549, 0.18823529411764706, 0.3803921568627451, 1.0)]
reversed :
[(0.0196078431372549, 0.18823529411764706, 0.3803921568627451, 1.0), (0.12941176470588237, 0.4, 0.6745098039215687, 1.0), (0.2627450980392157, 0.5764705882352941, 0.7647058823529411, 1.0), (0.5725490196078431, 0.7725490196078432, 0.8705882352941177, 1.0), (0.8196078431372549, 0.8980392156862745, 0.9411764705882353, 1.0), (0.9686274509803922, 0.9686274509803922, 0.9686274509803922, 1.0), (0.9921568627450981, 0.8588235294117647, 0.7803921568627451, 1.0), (0.9568627450980393, 0.6470588235294118, 0.5098039215686274, 1.0), (0.8392156862745098, 0.3764705882352941, 0.30196078431372547, 1.0), (0.6980392156862745, 0.09411764705882353, 0.16862745098039217, 1.0), (0.403921568627451, 0.0, 0.12156862745098039, 1.0)]
out :
[(0.0196078431372549, 0.18823529411764706, 0.3803921568627451, 1.0), (0.12941176470588237, 0.4, 0.6745098039215687, 1.0), (0.2627450980392157, 0.5764705882352941, 0.7647058823529411, 1.0), (0.5725490196078431, 0.7725490196078432, 0.8705882352941177, 1.0), (0.8196078431372549, 0.8980392156862745, 0.9411764705882353, 1.0), (0.9686274509803922, 0.9686274509803922, 0.9686274509803922, 1.0), (0.9921568627450981, 0.8588235294117647, 0.7803921568627451, 1.0), (0.9568627450980393, 0.6470588235294118, 0.5098039215686274, 1.0), (0.8392156862745098, 0.3764705882352941, 0.30196078431372547, 1.0), (0.6980392156862745, 0.09411764705882353, 0.16862745098039217, 1.0), (0.403921568627451, 0.0, 0.12156862745098039, 1.0)]
这似乎对我不起作用。选项1失败,因为反向列表没有长度,而选项2生成的地图只有红色。好的,谢谢。然后您需要将
reversed
返回的迭代器显式转换为列表。我不知道为什么选项2不起作用。