Using bar3 and color coordinating the height depending on the value
6 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
dpb
on 18 Jun 2020
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 Comments
dpb
on 18 Jun 2020
Edited: dpb
on 21 Jun 2020
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.
See Also
Categories
Find more on 2-D and 3-D Plots 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!