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

6 vues (au cours des 30 derniers jours)
Marco Piantini
Marco Piantini le 14 Mar 2019
Commenté : Adam Danz le 15 Mar 2019
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

Réponse acceptée

Adam Danz
Adam Danz le 14 Mar 2019
Modifié(e) : Adam Danz le 14 Mar 2019
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
  2 commentaires
Marco Piantini
Marco Piantini le 15 Mar 2019
Thank you so much Adam, it works!
Adam Danz
Adam Danz le 15 Mar 2019
Nice!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by