Coloring Rectangles to Specific Value's Colormap

조회 수: 34 (최근 30일)
Matt C
Matt C 2016년 9월 26일
편집: Matt C 2016년 9월 30일
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
elementToLinkToColorMap = rectanglesWithValue(i1,3)
%%%This is the code I can't determine
colouring = ??? link elementToLinkToColorMap to the cool color map
%%%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
Matt C 2016년 9월 30일
편집: Matt C 2016년 9월 30일
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]);

추가 답변 (1개)

simone martinelli
simone martinelli 2016년 9월 28일
편집: simone martinelli 2016년 9월 28일
Have you try to use ''patch'' command? In the help of ''patch'' you can also find how to choose colour or shade your shape.
  댓글 수: 1
Matt C
Matt C 2016년 9월 29일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by