# Coloring Rectangles to Specific Value's Colormap

16 views (last 30 days)
Matt C on 26 Sep 2016
Edited: Matt C on 30 Sep 2016
Hello, all. I am currently trying to create a figure in which I am drawing multiple rectangles on a graph, and then trying to shade them to a certain colormap that is related to a tertiary value. I can't really figure out how to do this via examples online, and I would appreciate some assistance.
My question can be simplified: if I have a nx1 array between 0 and some arbitrary max, how would I determine the color of each element assuming I have the jet colormap set from 0 to the same arbitrary max?
Here is the rectangle code that I am working with:
rectanglesWithValue = [1 1 30; 1 2 60; 1 3 20; 2 1 -1; 2 2 0; 2 3 80; 3 1 50; 3 2 40; 3 3 -1];
xMin = 0;
xMax = 3;
yMin = 0;
yMax = 3;
numRectanglesInX = 3;
numRectanglesInY = 3;
rectWidth = (xMax - xMin)/numRectanglesInX;
rectHeight = (yMax - yMin)/numRectanglesInY;
figure;
axis([xMin xMax yMin yMax])
for i1 = 1:length(rectanglesWithValue(:,1))
%%%IF THE THIRD VALUE OF THE RECTANGLE IS EQUAL TO -1, FILL IT IN BLACK
if rectanglesWithValue(i1,3) == -1
colouring = 'black';
else
%%%IF THE THIRD VALUE OF A RECTANGLE IS BETWEEN min AND max
%%%OF (rectanglesWithValue(:,3)) SET IT TO THE COOL COLORMAP
%%%This is the code I can't determine
%%%This is the code I can't determine
end
rectangle('Position',[rectanglesWithValue(i1,1)-rectWidth/2 rectanglesWithValue(i1,2)-rectHeight/2 rectWidth rectHeight],'FaceColor',colouring);
end
Thanks!

Matt C on 30 Sep 2016
Edited: Matt C on 30 Sep 2016
After starting to create R-G-B equations in order to solve my problem, and getting frustrated, I managed to find a function that somebody had produced on stack exchange which solved this issue. Here is the link:
The function is called 'vals2Colormap', and it is part of this package.
Here is my specific usage:
colourMin = min(rectanglesWithValue(:,3));
colourMax = max(rectanglesWithValue(:,3));
[colouring, ~, ~] = vals2Colormap(elementToLinkToColorMap,[colourMin colourMax],'cool', [colourMin colourMax]);

simone martinelli on 28 Sep 2016
Edited: simone martinelli on 28 Sep 2016
Have you try to use ''patch'' command? In the help of ''patch'' you can also find how to choose colour or shade your shape.
Matt C on 29 Sep 2016
This is where I'm not sure if any of the standard colormap schemes work. A lot of the colormap commands require being fed a matrix/vector, whereas I can only feed in individual elements in my code (my above code has been severely simplified from the problem I am facing).
I'm wondering if there is a simple solution to map a value to a specific colour.
Example:
x = 10 ---> What color is x, knowing min_X is 0 and max_X is 184? ---> computer returns [0 3 2] as its color.
x = 35 ---> What color is x, knowing min_X is 0 and max_X is 184? ---> computer returns [0 5 1] as its color.
I know that a very complex way of doing this is to actually look at the individual R-G-B curves of each colormap over increasing intensity, and then creating a function which can then fit your element's value to the curve. I'm just wondering if there is an easier way.

### Categories

Find more on Colormaps in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!