根据当前颜色图选择颜色

Pick a color according to current colormap

我的图形包含许多半透明等值面以可视化密度。我选择颜色范围 70:140:

vs=griddata(x,y,z,v,xs,ys,zs,'linear');
for i=70:5:140
  p(i)=patch(isosurface(xs,ys,zs,vs,i));
  isonormals(xs,ys,zs,vs,p(i));
  rd=(i-70)/70;
  set(p(i),'facealpha',0.5);
  set(p(i),'FaceColor',[rd 0 1-rd],'EdgeColor','none'); % set colors
end;
alpha(0.3);

所以,这里的颜色从蓝色(密度=70)到红色(密度=140)不等。 如何从当前颜色图中获取给定范围的颜色?

文档解释了 here 如何使用 fix 函数将值映射到颜色图。

这是您提供的范围的示例,其中可以使用 cmap(index(i),:) 访问范围 rng:

中的 i 的颜色
rng=70:5:140;
cmap = colormap;
m = size(cmap,1);
index = fix((rng-min(rng))/range(rng)*m)+1;
index(index<1) = 1;
index(index>m) = m;

% Plot example
hold on;
arrayfun(@(i) plot(rng(i),rng(i),'.', ...
    'markersize',30,'color',cmap(index(i),:)),1:length(rng));
colorbar; hold off;

此示例输出以下图:

我不确定我是否完全理解你在问什么,但你也可以查看命令 caxis,它将根据给定的当前范围设置颜色缩放 colormap .可以与 hsv 等函数一起使用,以将颜色图设置为请求的长度并将值缩放到该范围内。

     colormap(hsv(71));
     caxis([70 140]);
     patch(isosurface(x,y,z,v,c))
     ...