Imagesc Color Assignment of Values

조회 수: 17 (최근 30일)
Lucio De Pra
Lucio De Pra 2022년 1월 4일
댓글: Lucio De Pra 2022년 1월 5일
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