How to handle a colormap with multiple patches?

10 views (last 30 days)
Hello Matlab-Pros
I have a program that should mark certain elements of 3 different graphs if special conditions are met. This conditions can lead to 3 different colors.
To do this I use a custom colormap with exactly 3 colors. If the input actually needs all of this colors it works fine, but if it only needs one color it always uses the middle one.
I put the essential part of the program in the attached script, this should make my problem clear.
How can I always get the colors right?
Best regards and thanks in advance,
Michael
% This one works: (1 = red, 2 = orange, 3 = yellow)
areas = [54 134 1; 354 701 2; 2913 3687 3];
% In case the three patches have the same color it doesn't work: (all should be red)
areas = [54 134 1; 354 701 1; 2913 3687 1];
% Create Figure and Axes
Figure = figure;
subAx1 = subplot(1,3,1);
subAx2 = subplot(1,3,2);
subAx3 = subplot(1,3,3);
xlimit1 = [0.3 0.8];
xlimit2 = [20 50];
xlimit3 = [0 10000];
% This part rearranges 'areas' and 'xlimits' to a way, that 'patch' can work with it
areas(:,1:2) = (areas(:,1:2) - 1)/100;
x1 = repmat([xlimit1(1) xlimit1(2) xlimit1(2) xlimit1(1)],size(areas,1),1)';
x2 = repmat([xlimit2(1) xlimit2(2) xlimit2(2) xlimit2(1)],size(areas,1),1)';
x3 = repmat([xlimit3(1) xlimit3(2) xlimit3(2) xlimit3(1)],size(areas,1),1)';
y = repelem(areas(:,1:2)',2,1);
% Assign colors tp map
map = [1 0 0 % 1 = red
1 0.5 0 % 2 = orange
1 1 0]; % 3 = yellow
colormap(map)
color = areas(:,3);
patch(subAx1,x1,y,color, 'FaceAlpha',0.3, 'EdgeColor','none');
patch(subAx2,x2,y,color, 'FaceAlpha',0.3, 'EdgeColor','none');
patch(subAx3,x3,y,color, 'FaceAlpha',0.3, 'EdgeColor','none');
% Set axes limits
subAx1.XLim = xlimit1;
subAx2.XLim = xlimit2;
subAx3.XLim = xlimit3;
subAx1.YLim = [0 80];
subAx2.YLim = [0 80];
subAx3.YLim = [0 80];

Accepted Answer

Stephen23
Stephen23 on 24 Jan 2019
Edited: Stephen23 on 24 Jan 2019
Set the caxis limits:
A simple working example:
>> map = [1,0,0;1,0.5,0;1,1,0]; % red;orange;yellow
>> colormap(map)
>> patch([0,1,2,0],[0,2,0,0],3) % 3 -> should be yellow
>> saveas(gcf,'Before.png')
>> caxis([1,3]) % set the color limits
>> saveas(gcf,'After.png')
Before:
Before.png
After:
After.png
  3 Comments
Michael Steinbuechler
Michael Steinbuechler on 24 Jan 2019
Okay I found the problem. Seems like the caxis command doesnt like subplots if it doesnt get a handle.
Thats what the solution in my case looks like:
subAx1.CLimMode = 'manual';
subAx2.CLimMode = 'manual';
subAx3.CLimMode = 'manual';
subAx1.CLim = [1,3];
subAx2.CLim = [1,3];
subAx3.CLim = [1,3];
Thank you very much!!!
Stephen23
Stephen23 on 24 Jan 2019
According to the caxis documentation this should also work:
caxis(subAx1,[1,3])
caxis(subAx2,[1,3])
caxis(subAx3,[1,3])

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!