overlay bar plot without mixing color
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I would like to create a bar plot with overlaying bars of different colours.
I tried using FaceAlpha but this alters the colours.
Basically, I would like my plot to look like a stacked bar colour wise, but like an ovelayed bar in terms of the values.
This is the simplified code and result of what I have tried:
A = [1;5;3;7;4];
B = [3;2;4;8;2];
figure;
bar(A, 'FaceColor', 'b', 'FaceAlpha',0.5)
hold on
bar(B, 'FaceColor', 'y', 'FaceAlpha',0.5)
hold off

How can I overlay my bars in a way that they show the colour I defined instead of mixing them to this weird muddy green in the overlaying bits?
0 Kommentare
Akzeptierte Antwort
Rik
am 8 Nov. 2021
Here is my solution, which allows you to specify the 3 face colors separately:
A = [1;5;3;7;4];
B = [3;2;4;8;2];
shared=min(A,B);
A_high=max(0,A-shared);
B_high=max(0,B-shared);
data=[shared A_high B_high];
h=bar(data,'stacked');
h(1).FaceColor='g';
h(2).FaceColor='b';
h(3).FaceColor='y';
3 Kommentare
Rik
am 8 Nov. 2021
I think need to add another column in data to do that. The idea would be the same in principle, but you would set one column to 0 if A is larger than B and the other to 0 if B is larger than A.
Why don't you have a try first?
And what should happen if A has the same value as B?
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
