bar graph with 3 axes

Hello everyone, I want to create a 3d bar graph with 3 axes but somehow I couldn't do because matlab ploting browser wants only x and y information. I created the graph by using stem3 command and the output is shown above but I would like to have the graph as shown below:
stem3(d,m,p, '-b','LineWidth',4);
xlabel('d'); ylabel('m'); zlabel('p');
zoom on; grid on;

1 Comment

dpb
dpb on 27 Jul 2022
You'll be disappointed, but bar3 is the best it gets in basic MATLAB. You can specify the years on the Y axis, but X is implied by the number of columns in Z and will be from 1:6 in your case; you'll have to use xticktabel to label them.
It really, really is a weak link/entry -- always has been; seemingly always will be, I've railed about it and bar for 30 years with no real effect.

Sign in to comment.

Answers (2)

You can use bar3.
bar3(rand(10));
xlabel('d'); ylabel('m'); zlabel('p');

2 Comments

I already used it but it shows only m and p values on the graph
dpb
dpb on 27 Jul 2022
You have to arrange p to be a 2D array with years the rows; the various categories the columns.

Sign in to comment.

dpb
dpb on 27 Jul 2022
p=10*abs(randn(7,6)); % some dummy data -- 7 periods by 6 systems
d=[72 142 225 275 975 2475 5975]; % the return period values
m={'Structure','Contents','Mechanical','Electrical','IT','Windows'}; % systems
bar3(p,0.6) % the basic bar3 plot
xticklabels(m), yticklabels(d) % label the tick values
xlabel('System') % and the axes
ylabel(['Return' newline 'Period'])
gives a crude starting point -- will need a lot of cleanup yet to be presentable, but puts the data on the axes...
@Adam Danz -- you listening??? :)

6 Comments

I think the values in last column is not compatible with the other values so this is why my graph looks bad..
z=['Magnitude' 'Distance' 'Probability'
4.55E+00 5.00E+00 1.3E-291
4.55E+00 1.50E+01 1.3E-291
4.55E+00 2.50E+01 1.3E-291
4.55E+00 3.50E+01 1.3E-291
4.55E+00 4.50E+01 1.3E-291
4.55E+00 5.50E+01 1.3E-291 ]
dpb
dpb on 27 Jul 2022
Edited: Adam Danz on 27 Jul 2022
I'd contend p~10^(-291) is probably meaningless to begin with, but that aside -- note also that your 'Magnitude' column values are all identical so there's no distinction in that dimension to start wtih.
As for how to use bar3, it only accepts values along the y-axis; the x-axis is 1:numberColumns in the input Z array -- per the documentation, the X values don't have to be sorted, but they do have to be unique which yours aren't so you can't use the vector z(:,1) as input anyway -- although it makes no sense to do so until and unless you can get sufficient precision to differentiate the values.
As is, all you've got data for is a 1D bar graph against distance for a given magnitude value; you would have to have another column of p values at least to be able to draw a 3D bar in two independent dimensions, anyway. Of course, since all the p values are also all the same, the graph will be rather boring, whatever the magnitude.
The short story is besides the numeric values, you simply don't have enough values to begin with to be able to use bar3.
bus gogen
bus gogen on 27 Jul 2022
Edited: bus gogen on 27 Jul 2022
I totally get it, you are right. I will modify the values thanks. :) actually I put ony first 6 rows of my data, there are other different small values too but yes I need to modify them to make proper graph.
dpb
dpb on 27 Jul 2022
Edited: Adam Danz on 27 Jul 2022
Remember for bar3, the number of columns in Z array sets the number of X-axis values; there's a Y-axis value for each row of Z.
Thanks for sharing this use-case for specifying x-values in bar3.
@dpb, I hope you don't mind I edited your comment - the x's and y's were mixed up.
The number of columns of z sets the number of x values.
Lastly, an alternative to xticklabels & yticklabels is to set the labels directly from from the axis object,
data = rand(5,3);
bar3(data)
xlabel('x')
ylabel('y')
set(gca, 'XTick', 1:width(data), 'XTickLabels', {'A','B','C'}, ...
'YTick', 1:5, 'YTickLabels', 10:10:50)
dpb
dpb on 27 Jul 2022
@Adam Danz -- I'm always doing that for some reason -- thanks for the correction.
In this case the actual values aren't terribly helpful for numeric plotting because the scaling is grossly weighted to one end, but as a general precept/facility it's useful on occasion. The "standard" barplot typically is just fixed bar spacing, granted, but when it's helpful, it's painful to not have it available.
One other thing about bar3 -- there is no handle to the overall object and, ergo, no way to fix up things like the 'width' parameter after the fact to fine-tune -- one has to do it over and over again from the git-go. I suppose it's owing to the need to resize both directions makes it harder than for the 1D version, but still...

Sign in to comment.

Categories

Asked:

on 27 Jul 2022

Edited:

on 27 Jul 2022

Community Treasure Hunt

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

Start Hunting!