How to keep different colors for stacked bars in a multiple plot?

조회 수: 6 (최근 30일)
Marco Piantini
Marco Piantini 2019년 3월 14일
댓글: Adam Danz 2019년 3월 15일
I've combined two different plots in the same axes with "hold on": a normal graph and a bar graph with stacked bars. I would like to mantain different colors for each part of the single bars, but when I plot the figure the bars turn into the same color, even if still separated in different parts. For example:
x=1:10
y1=rand(1,10)
y2=rand(10)
figure
yyaxis left
plot(x,y1)
hold on
yyaxis right
bar(x,y2,'stacked')
How can I keep different colors for each part?
Thanks so much

채택된 답변

Adam Danz
Adam Danz 2019년 3월 14일
편집: Adam Danz 2019년 3월 14일
You can apply color to the bar sections after plotting.
yyaxis right
bh = bar(x,y2,'stacked'); %save the handle!
colors = lines(length(bh)); %define colors
for i = 1:length(bh)
set(bh(i), 'FaceColor', colors(i,:))
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by