How to plot multiple data sets on bar3

I’m trying to make multiple 3D histogram plots side-by-side. I think I need to use the bar3 function, but I don’t know what combination of code to use. Am I trying to make something that doesn’t exist in MATLAB?

2 Comments

Can you sketch what you envision this should look like or show a published example?
bar3 is quite limited in its flexibility to modify and there are no other builtin functions with additional abilities beyond it.
I hope this image is a good enough representation of that I meant. I'm not very good at 3D drawing.
x-axis and z-axis make a normal histogram, but then you have multiple sets along the y-axis.

Sign in to comment.

Answers (1)

dpb
dpb on 27 Feb 2025
Moved: dpb on 27 Feb 2025
Oh. That is what bar3 can/does do. Look at the examples; the Z data array would be 3x3 with the x-axis being rows and y the columns.
Z=[1 2 1; 3 3 2; 3 2 3].'; % counts that approximate sketch
hB=bar3(Z); % make base 3D bar
map=[0 0 1; 1 0 0; 0 1 0]; % set colormap to match sketch b,r,g
colormap(map)

3 Comments

I tried something similar and it didn't work (shown below). I am also working with a pretty large data set and I want those area to be histograms for that reason, so I can bin the data. I'm also not sure I have the same number of data for each set.
When I ran your code with my data set the image shown is what I got.
A = Data.1;
B = Data.2;
C = Data.3;
D = Data.4;
E = [A; B; C; D];
bar3(E)
You created a column vector, not an Nx4 array as can be seen in the figure as being only the one row.
bar3([A B C D])
instead. If the lengths are not the same, then augment each to the length of the longest with NaN.
I don't know how you generated the Data.N variable references, however, trying to create an example runs into issues...
Data=struct('1',rand(4,1))
Error using struct
Invalid field name "1".
MAX=[100 200 300].'; % max counts for example
N=randi(20,[3 1]); % a variable number for number/observation
data=arrayfun(@(max,n)randi(max,[n 1]),MAX,N,'uni',0)
data = 3x1 cell array
{19x1 double} {12x1 double} { 8x1 double}
NMAX=max(N); % what was the biggest one?
data=(arrayfun(@(i,n)cat(1,data{i},nan(NMAX-n,1)),[1:numel(N)].',N,'uni',0)).'
data = 1x3 cell array
{19x1 double} {19x1 double} {19x1 double}
data=cell2mat(data);
bar3(data)
The above illustrates the agumentation of shorter columns -- with just random data -- a maximum count was set for each of the three columns; and then a random number of elements of an array was generated. For the particular case, that turned out to be 19, 12, 8 as seen from the size of the cell array. Then the maximum number was found and the shorter columns augmented by adding NaN elements to make each column in the array the same length; a mandatory condition to cread a 2D array other than a cell array. But, while it would be the ideal solution, bar3 is not extended to handle cell array inputs, only arrays. The augmented array is passed to bar3 and the NaN elements look as if were zeros...I didn't recall it doing that; the plot and other line routines simply do not plot NaN elements. But, that's the limits of what bar3 can do out of the box...
Your alternate choice is to only plot the number of elements in each up to the shortest number of observations, ignoring those with only partial data.

Sign in to comment.

Categories

Products

Release

R2024b

Asked:

on 27 Feb 2025

Edited:

dpb
on 2 Mar 2025

Community Treasure Hunt

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

Start Hunting!