How to use the same color scheme but different FaceAlpha in bar graphs with hold on?

9 Ansichten (letzte 30 Tage)
Hi everyone,
I am having two sets of data. I am using bar command to plot them using hold on. However, I want my first bar graph and second bar graph have idnetical color scheme with only different FaceAlphas but I don't know how to make the color scheme the same for these graphs. Could anyone please help me? thank you in advance.
Data1 = round(100*rand(5,6)); % Your data Data1
Data2 = round(100*rand(5,6));
bar(Data1)
hold on
bar(Data2,'FaceAlpha', 0.5)
hold off

Antworten (1)

Dave B
Dave B am 4 Aug. 2021
A few options, depending on where you might head next:
Option 1: By default, bar is going to use the ColorOrder from the axes for its colors, and the values repeat themselves. The default colororder has 7 colors and you have 6 bars, so using 6 colors would do the trick here:
colororder(lines(6))
Option 2: The way ColorOrder maps colors uses the SeriesIndex property. The first 6 bars have SeriesIndex values of 1 through 6, the second have values 7-12. This means you can keep your ColorOrder as it was, and if you added a line (for instance) it would use the 7th color.
b1=bar(Data1)
hold on
b2=bar(Data2,'FaceAlpha', 0.5)
hold off
for i = 1:length(b2)
b2(i).SeriesIndex = b1(i).SeriesIndex
end
Option 3: You could just set the FaceColor directly. Be careful with this one as it means that future calls to colororder will affect b1 (which is still using automatic colors) but not b2.
b1=bar(Data1)
hold on
b2=bar(Data2,'FaceAlpha', 0.5)
hold off
for i = 1:length(b2)
b2(i).FaceColor = b1(i).FaceColor;
end

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by