When using the bar3
function, the colours of the bars depend on the column or row the data is in. Example:
data = peaks(20);
bar3(data);
view(-135,30)
How can I make it that the colour of each bar only depends on the height (zdata) of that bar?
It is quite difficult to mess around with the colours of the bars, as all the bars are constructed with patches. You'll have to change the zdata of the vertices of the patches; since this is not very straightforward, I've made a function just for this purpose: bar3c
Example:
data = peaks(20);
bar3c(data);
view(-135,30)
If you want to use other colors, simply change the colormap.
If you want to set a whole range to get one same color instead of getting a gradient between your min/max values, you'll have to change the call to makecdata
. Now it uses the zdata
to specify its color, you can easily change that to give a range a specific color.
Actually, I just did that and the result can be found here.
Example:
data = peaks(20);
bar3crange([-8 0 3 4 8], data);
% colormap: red/yellow/green/blue
colormap([1 0 0;1 1 0;0 1 0;0 0 1]);
view(-135,30);
Alternatively, if you want to do it without external functions, try
data = peaks(20);
h = bar3(data);
view(-135,30);
for i = 1:size(data,2)
cdata = get(h(i),'cdata');
k = 1;
for j = 0:6:(6*size(data,1)-6)
cdata(j+1:j+6,:) = data(k,i);
k = k+1;
end
set(h(i),'cdata',cdata);
end
colormap(jet);
Credits to Sebastian at aiZac for the inspiration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With