Imagesc Color Assignment of Values

17 vues (au cours des 30 derniers jours)
Lucio De Pra
Lucio De Pra le 4 Jan 2022
Commenté : Lucio De Pra le 5 Jan 2022
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.

Réponses (2)

yanqi liu
yanqi liu le 5 Jan 2022
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 commentaire
Lucio De Pra
Lucio De Pra le 5 Jan 2022
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.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 4 Jan 2022
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 commentaire
Lucio De Pra
Lucio De Pra le 4 Jan 2022
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!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Color and Styling dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by