0

假设我有一个整数矩阵。我想将一个值映射到颜色。例如,值 0 将显示为黑色,值 0.5 将显示为绿色等。我尝试了颜色图,但它没有按我的意愿工作。在颜色图中,当我更改矩阵中的值时,它也会影响其他值。

那么如何将颜色映射到值?一个单元格包含 0,显示为黑色。一个单元格包含 0.5,显示为绿色。一个单元格包含等于或大于 1 的数字,显示为黄色。

谢谢!

4

1 回答 1

0

The easiest way I would think of doing anything like what you are saying is:

  • Define a colormap as:

    cm = colormap([0 0 0; 0 1 0; 1 1 0]);

  • Define a caxis as:

    caxis([0 1]);

  • Use something like pcolor to represent your data.

All these is assuming that you want to represent some kind of color map in 2D. If you intend to plot a simple curve, but changing the color of the data points, you should do it manually, plotting them individually and setting the color manually for each point, depending on the value of the data:

% plot y versus x variable
for ii = 1:length(x)
    if y(ii) < 0.5
        color = [0 0 0];
    elseif y(ii) < 1
        color = [0 1 0];
    else
        color = [1 1 0];
    end
    plot(x(ii), y(ii), '.', 'Color', color);
    hold on;
end
hold off;
于 2013-02-12T12:54:08.860 回答