How to assign different colormaps to different stacked bars

3 views (last 30 days)
I have a defined variable of 2x12, and would like to create a stacked bar plot but using two different colormaps defined below:
c1=[0 0 1];%B
c2=[1 1 1];%W
c3=[1 0 0]; %R
n3=11;
n4=53;
cmap1=[linspace(c1(1),c2(1),n3);linspace(c1(2),c2(2),n3);linspace(c1(3),c2(3),n3)];
cmap1(:,end+1:end+n4)=[linspace(c2(1),c3(1),n4);linspace(c2(2),c3(2),n4);linspace(c2(3),c3(3),n4)];
n5=42;
n6=22;
cmap2=[linspace(c1(1),c2(1),n5);linspace(c1(2),c2(2),n5);linspace(c1(3),c2(3),n5)];
cmap2(:,end+1:end+n6)=[linspace(c2(1),c3(1),n6);linspace(c2(2),c3(2),n6);linspace(c2(3),c3(3),n6)];
I don't know how to apply these two different colormaps on the two stacked bar plots without one overwriting the other, so I always end up with the same colormap for both stacks. Any suggestions?
Thanks!

Answers (1)

Walter Roberson
Walter Roberson on 18 Nov 2017
Look in the File Exchange for freezeColors().
Draw the first item and apply its colormap. Use freezeColors() to convert it to RGB from pseudocolor. Now you can draw the second item and apply its colormap because the first item is no longer using colormaps.
  6 Comments
Walter Roberson
Walter Roberson on 18 Nov 2017
b = bar(var1, 'stacked');
for K = 1 : length(b); b(K).FaceColor = cmap1(:,K).'; end
and likewise with the second variable.
Tasi
Tasi on 18 Nov 2017
I am trying to adjust this code, but it is still not doing it. I am trying to figure out why that is. Thanks so much for your help.

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!