Define Colormap Range Based on Z Axis
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Walter Roberson
2015년 5월 25일
편집: Walter Roberson
2015년 5월 26일
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
Shubham Mohan Tatpalliwar
2018년 11월 7일
편집: Shubham Mohan Tatpalliwar
2018년 11월 7일
i think there should be a direct function in matlab
to change the color code according to axis
참고 항목
카테고리
Help Center 및 File Exchange에서 Color and Styling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!