Imagesc Color Assignment of Values

17 次查看(过去 30 天)
Hi I am currently trying to create a colormap for the function imagesc(); Currently my code looks like the following:
caxis manual
my_map=[0 0 0;1 1 1;1 0 1;1 0 0];
x=[0:100];
imagesc(x)
colormap(my_map)
colorbar
As of right now it will map values of 0:25 to white, 25:50 red and so forth. Is there any method I could use to change the assignment of values for the colors. Example: assigning values of 0:5 to white 6:80 black.
Thank you in advance.

回答(2 个)

yanqi liu
yanqi liu 2022-1-5
clc; clear all; close all;
% 0:25 to white, 25:50 red and so forth.
figure;
caxis manual
my_map=[0 0 0;1 1 1;1 0 1;1 0 0];
x=[0:100];
imagesc(x)
colormap(my_map)
colorbar
% 0:5 to white 6:80 black.
figure;
caxis manual
my_map=[repmat([1 1 1], length(0:5), 1)
repmat([0 0 0], length(6:80), 1)
repmat([1 0 0], length(81:100), 1)];
x=[0:100];
imagesc(x)
colormap(my_map)
colorbar
  1 个评论
Lucio De Pra
Lucio De Pra 2022-1-5
Thank you for the response this is very helpful. Is it possible to make the values go from 0:.2 and 0.2:0.4 on the colorbar. I have tried limiting the caxis but worked to no avail.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2022-1-4
NO.
Values below the first caxis value are always assigned the lowest color in the color map.
Values above the second caxis value are always assigned the highest color in the color map.
In-between, the ratio: (value - caxis(1))/(caxis(2)-caxis(1)) is calculated, and that gives you the linear portion of the way through the colormap that is used.
There is no way to say to use a particular colormap entry for a particular range of values, and a different colormap entry for a different range of values.
What you can do is create a colormap with 27 entries. Assign the same white color to the first two. Assign black to the remaining 25 entries. The span of values is 81, so the first three values (0, 1, 2) would use the "first" white slot, the second three values (3, 4, 5) would use the "second" white slot, and all other values would use some other slot that would happen to hold black.
But it is typically easier instead,
bin = discretize(x, [0 5 80 inf])
imagesc(bin)
colormap(my_map)
  1 个评论
Lucio De Pra
Lucio De Pra 2022-1-4
Oh okay I understand so let's say i have an array of numbers with values between 0-1 and I want my threshold to be 0.1. I can set a colormap with 10 entries and basically black out 0:0.1 and if i want the color red for the range 0.1-0.5 i can just set the intervals of [0.1 0.2] [0.2 0.3] and [0.3 0.4] as the color red.
I appreciate the help regardless thank you!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Color and Styling 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by