필터 지우기
필터 지우기

Using bar3 and color coordinating the height depending on the value

조회 수: 6 (최근 30일)
Jason
Jason 2020년 6월 18일
편집: dpb 2020년 6월 21일
Hello, I am trying to adjust the following code from the documentation to create a 3D barchart and have the height colored depending on the value
% From documentation
Z = magic(5);
b = bar3(Z);
colorbar
for k = 1:length(b)
zdata = b(k).ZData;
b(k).CData = zdata;
b(k).FaceColor = 'interp';
end
This is my data, (Im just plotting one column, column 2)
data =
1.00 44.48 8.20
2.00 68.53 140.26
3.00 3628.97 843.39
and the code Im using.
b1=bar3(data(:,2));
for k = 1:length(b1)
zdata = b1(k).ZData;
b1(k).CData = zdata;
b1(k).FaceColor = 'interp';
end
and this is what I get
and if I check the ZData:
b1.ZData
I get
Zdata =
NaN 0 0 NaN
0 44.48 44.48 0
0 44.48 44.48 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 68.53 68.53 0
0 68.53 68.53 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 3628.97 3628.97 0
0 3628.97 3628.97 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
what are all the extra rows and columns? They seem to be messing up what I want to do

답변 (1개)

dpb
dpb 2020년 6월 18일
They're all the faces color vertices data -- it's a convolved mess to fiddle with manually, fer shure, good buddy! :(
Your code is ok, the problem is the scaling of the three bars is so disparate the linear scaling isn't effective. Try
b1(k).CData = log10(zdata);
and you'll at least see some differences...not sure the best generic solution for such a case, but that's the root cause problem.
  댓글 수: 4
Jason
Jason 2020년 6월 18일
Actually, its still not quite right.
I wanted the vertical walls to also change colour gradually, at the moment its just the tops of the bars.
I used this as my data.
data=[33;44;120;66;17]
b1=bar3(data);
for k = 1:length(b1)
zdata = b1(k).ZData;
b1(k).CData = log10(zdata);
b1(k).FaceColor = 'interp';
end
dpb
dpb 2020년 6월 18일
편집: dpb 2020년 6월 21일
That has to do with only using the one column/row -- and I'm not sure otomh just what would need to fixup to produce the effect, sorry.
Try changing you data vector to
data=[data data];
and the same code (excepting take the log10 scaling back out) and you'll see the shading come back.
I remember digging into the CData stuff pretty throughly a couple years ago and there being a thread/Answer on the subject but I'd have no way to find it quickly at the moment, unfortunately.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by