MATLAB Answers

Fill/patch with different colors

조회 수: 22(최근 30일)
Hi there, i want to display my behavioral data as filled areas over time.
x = [1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8];
y = [0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0];
a = fill(x,y,'b');
a.FaceAlpha = 0.1;
xlabel('time [s]')
Something simple like that. However, i want for example the second and third area to be the same color because they are the same behavior and the first and last be something different. How do i do that ? I already read through the patch/fill manual but didn't figured it out. A huge thank you in advance!
  댓글 수: 1
Mathieu NOE
Mathieu NOE 3 Mar 2021
hello
this is it :
x = [1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8];
y = [0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0];
colour = [1 1 1 1 0 0 0 0 0 0 0 0 2 2 2 2 ];
a = fill(x,y,colour);
a.FaceAlpha = 0.5;
xlabel('time [s]')

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

채택된 답변

Walter Roberson
Walter Roberson 3 Mar 2021
It might be possible if you played around with FaceVertexCData and similar properties for long enough, but it would be easier if you were to change how you draw.
x = [1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8];
y = [0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0];
vertices = [x(:), y(:)];
faces = [
1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16;
];
groups = [1 2 2 1];
cmap = parula(numel(unique(groups)));
colors = cmap(groups,:);
patch('vertices', vertices, 'Faces', faces, 'FaceColor', 'flat', 'FaceVertexCData', colors, 'CDataMapping', 'direct', 'FaceAlpha', 0.1);
xlabel('time [s]')

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by