简体   繁体   English

Matlab:一维数组到带有颜色图的 RGB 三元组

[英]Matlab: 1D array to RGB triplets with colormap

I'm trying to draw a set of rectangles, each with a fill color representing some value between 0 and 1. Ideally, I would like to use any standard colormap.我正在尝试绘制一组矩形,每个矩形的填充颜色代表 0 和 1 之间的某个值。理想情况下,我想使用任何标准颜色图。

Note that the rectangles are not placed in a nice grid, so using imagesc , surf , or similar seems unpractical.请注意,矩形未放置在一个漂亮的网格中,因此使用imagescsurf或类似方法似乎不切实际。 Also, the scatter function does not seem to allow me to assign a custom marker shape.此外, scatter功能似乎不允许我分配自定义标记形状。 Hence, I'm stuck to plotting a bunch of Rectangles in a for-loop and assigning a FillColor by hand.因此,我坚持在 for 循环中绘制一堆矩形并手动分配FillColor

What's the most efficient way to compute RGB triplets from the scalar values?从标量值计算 RGB 三元组的最有效方法是什么? I've been unable to find a function along the lines of [r,g,b] = val2rgb(value,colormap).我一直无法找到类似[r,g,b] = val2rgb(value,colormap). Right now, I've built a function which computes 'jet' values, after inspecting rgbplot (jet).现在,在检查rgbplot (jet) 之后,我已经构建了一个计算“jet”值的函数。 This seems a bit silly.这似乎有点傻。 I could, of course, obtain values from an arbitrary colormap by interpolation, but this would be slow for large datasets.当然,我可以通过插值从任意颜色图中获取值,但这对于大型数据集来说会很慢。

So, what would an efficient [r,g,b] = val2rgb(value,colormap) look like?那么,一个有效的[r,g,b] = val2rgb(value,colormap)什么样的?

You have another way to handle it: Draw your rectangles using patch or fill specifying the color scale value, C , as the third parameter.您还有另一种处理方法:使用patchfill绘制矩形,指定色阶值C作为第三个参数。 Then you can add and adjust the colorbar:然后您可以添加和调整颜色条:

x = [1,3,3,1,1];
y = [1,1,2,2,1];
figure
for ii = 1:10
    patch(x + 4 * rand(1), y + 2 * rand(1), rand(1), 'EdgeColor', 'none')
end
colorbar

With this output:有了这个输出:

在此处输入图片说明

I think erfan's patch solution is much more elegant and flexible than my rectangle approach.我认为 erfan 的补丁解决方案比我的矩形方法更优雅和灵活。

Anyway, for those who seek to convert scalars to RGB triplets, I'll add my final thoughts on the issue.无论如何,对于那些寻求将标量转换为 RGB 三元组的人,我将添加我对这个问题的最终想法。 My approach to the problem was wrong: colors should be drawn from the closest match in the colormap without interpolation.我解决这个问题的方法是错误的:应该从颜色图中最接近的匹配中绘制颜色,而无需插值。 The solution becomes trivial;解决方案变得微不足道; I've added some code for those who stumble upon this issue in the future.我为将来偶然发现此问题的人添加了一些代码。

% generate some data
x = randn(1,1000);

% pick a range of values that should map to full color scale
c_range = [-1 1];

% pick a colormap
colormap('jet');

% get colormap data
cmap = colormap;

% get the number of rows in the colormap
cmap_size = size(cmap,1);

% translate x values to colormap indices
x_index = ceil( (x - c_range(1)) .* cmap_size ./ (c_range(2) - c_range(1)) );

% limit indices to array bounds
x_index = max(x_index,1);
x_index = min(x_index,cmap_size);

% read rgb values from colormap
x_rgb = cmap(x_index,:);

% plot rgb breakdown of x values; this should fall onto rgbplot(colormap)
hold on;
plot(x,x_rgb(:,1),'ro');
plot(x,x_rgb(:,2),'go');
plot(x,x_rgb(:,3),'bo');
axis([c_range 0 1]);
xlabel('Value');
ylabel('RGB component');

With the following result:结果如下: 使用“jet”颜色图从标量值计算的 RGB 分量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM