How to use the same color scheme but different FaceAlpha in bar graphs with hold on?
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Dave B
2021-8-4
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!