Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bar3 colour depending on zdata

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)

bar3 example

How can I make it that the colour of each bar only depends on the height (zdata) of that bar?

like image 258
Gunther Struyf Avatar asked Oct 10 '12 22:10

Gunther Struyf


2 Answers

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)

bar3c example

Edit

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);

bar3crange example

like image 143
Gunther Struyf Avatar answered Nov 19 '22 11:11

Gunther Struyf


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);

enter image description here

Credits to Sebastian at aiZac for the inspiration.

like image 23
zelanix Avatar answered Nov 19 '22 11:11

zelanix