Fill() for multiple polygons with specified colors -- weird behavior?

Hello,
I am trying to use the fill() function to color multiple polygons, but I am observing some weird behavior for indexing to color map..
f =figure;
ax1 = axes;hold on;
% DEFINE MY POLYGONS
x1 = [ 1 1 2 2];
y1 = [ 1 2 2 1];
x2 = [ 3 3 4 4];
y2 = [3 4 4 3];
x3 = [ 4 4 5 5];
y3 = [1 2 2 1];
% COMBINE THEM TO BE MATRICES
x_all = [x1' x2' x3'];
y_all = [y1' y2' y3'];
colormap([1 0 0; 0 1 0; 0 0 0]);% DEFINE MY COLORMAP TO JUST BE RED, GREEN, BLACK
c_all = [1;2;3]; % This should index to my color map.. in this case, 1=RED 2=GREEN 3=BLACK
h = fill(x_all,y_all,c_all);
So.. running the code above gives me what i expected. 3 squares in the order RED GREEN BLACK.
However, If i change:
c_all = [1 ; 1; 1]
I would expect all 3 polygons to come out red, but instead they are all green.
Can anyone explain my misunderstanding or provide a solution to specify exact colors to multiple polygons?
note: I don't want to use the patch() function, because I would like to change my x coordinates into datetime values.
Thank you!

Answers (1)

I guess you have to explicitly override the CDataMapping.
x1 = [ 1 1 2 2];
y1 = [ 1 2 2 1];
x2 = [ 3 3 4 4];
y2 = [3 4 4 3];
x3 = [ 4 4 5 5];
y3 = [1 2 2 1];
x_all = [x1' x2' x3'];
y_all = [y1' y2' y3'];
colormap(([1 0 0; 0 1 0; 0 0 0]));
c_all = [1;1;1];
h = fill(x_all,y_all,c_all,'CDataMapping','direct');

Categories

Asked:

on 22 Mar 2021

Edited:

on 22 Mar 2021

Community Treasure Hunt

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

Start Hunting!