Define Colormap Range Based on Z Axis
2 views (last 30 days)
Show older comments
Hi everyone,
The attached is a 3D bar plot using following matrix:
A = [0.801 0.711 0.45; 0.784 0.714 0.56; 0.868 0.829 0.566];
As seen, the colormap range is from 1 to 3, defined based on X and Y axis.
I would like to use Z axis values (0 to 0.9) to define the colormap.
Any help would be highly apprecaited.
Thanks Ashkan

0 Comments
Answers (1)
Walter Roberson
on 25 May 2015
Edited: Walter Roberson
on 26 May 2015
I am assuming here you are using bar3.
bar3 creates one bar per column of data.
Inside each bar object, an XData, YData, and ZData matrix are created to draw the bars. Each of those has groups of 6 x 4 for each row of the original data. For example a (5 x 3) original matrix would generate 3 bars (one per column), and each bar would use 6 coordinates per row, so (5)*6 = 30 coordinates, each coordinate being 4 wide, so a (30) x 4 coordinate matrix in this case.
bar3() sets the CData property to a matrix the same size as the XData, YData, ZData, (so (30) x 4 in this case). It fills the CData property with the integer which is the column number -- all 1's for the first column, all 2 for the second column, etc. It also leaves the color interpretation as 'scaled' and leaves the axes CLimMode 'auto'. These two properties together mean that the min and max of all of the CData of all of the objects in the axes will be found, and colormap interpretation will be done between that range, the lowest value mapping to the first colormap entry and the highest value to the highest colormap entry. In the case of 3 bars, with bar3() having assigned all 1's to the first bar, all 2's to the second, all 3's to the third, the range would be 1 to 3 and so the 1's would get mapped to the first colormap entry, the 2's to half-way through, the 3's to the third entry.
So... what we do is create some code that grabs the values from the variable, makes 6 x 4 copies of each of them, does that for each row to produce the right CData size, and set()'s the CData property appropriately. By the time all of the bars have had their colors bashed that way, the range of the colors present in the axis will reflect the values in the original matrix with the solid blocks of 1's etc. gone, and the color for each box of each bar should be appropriate. In theory anyhow ;-)
A = YourDataToBar;
%magic 6 and magic 4 come from the way bar3() draws the bars
nrow = size(A,1);
ncol = size(A,2);
idxrep = repmat(1:nrow,6,1); %magic 6
bars = bar3(A);
for K = 1 : ncol
Z = repmat(A(idxrep(:),K),1,4); %magic 4
set(bars(K), 'CData',Z);
end
3 Comments
Shubham Mohan Tatpalliwar
on 7 Nov 2018
Edited: Shubham Mohan Tatpalliwar
on 7 Nov 2018
i think there should be a direct function in matlab
to change the color code according to axis
See Also
Categories
Find more on Color and Styling 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!