Reputation: 1566
I have a bar plot with 2 Y-axes generated with plotyy()
and want to assign a colormap to each of them. I tired to use colormap(axis,map)
but it seems to overwrite the settings of the whole plot, instead of the ones from axis
only.
My aproach:
[haxes,hbar1,hbar2]=plotyy(1:2,randi(2000,2,3),3:5,randi(100,3,3),'bar','bar');
set(haxes(1), 'xtickLabel', name(1:2));
set(haxes(2), 'xtickLabel', name(3:5) );
set(haxes,{'ycolor'},{'r';[67,186,52]./256});
%set(hbar1, 'facecolor', ['r','w','y']); %<- Things like this work, but are not what Iwant
colormap(haxes(1),hot)
colormap(haxes(2),summer)
results in this plot
and I want something like this (which I generated by hand, coloring single bars)
How can I apply two different colormaps in a single figure?
Upvotes: 1
Views: 697
Reputation: 2063
Concatenate all the colormaps you want to use into a single colormap, then set each barseries object to index directly into the relevant bin of this colormap. E.g.
[haxes,hbar1,hbar2]=plotyy(1:2,randi(2000,2,3),3:5,randi(100,3,3),'bar','bar');
colormap(haxes(1),[hot(3); summer(3)])
for a = 1:3
set(get(hbar1(a), 'Children'), 'CData', [a; a], 'CDataMapping', 'direct');
set(get(hbar2(a), 'Children'), 'CData', [a; a; a]+3, 'CDataMapping', 'direct');
end
produces:
Upvotes: 1
Reputation: 2063
A colormap applies to the entire figure, and not just an axes, so when you set the colormap the second time you are just overwriting the first colormap. To quote the colormap documentation (with my own emphasis):
colormap(ax,...)
uses the figure corresponding to axes ax instead of the current figure.
Upvotes: 0
Reputation: 4136
This does exacly what you want,
y = [4.2; 4.6; 5];
h = figure;
a = axes('parent', h);
hold(a, 'on')
colors = {'r', 'b', 'g'};
for i = 1:numel(y)
bar(i, y(i), 'parent', a, 'facecolor', colors{i});
end
Upvotes: 0