How to plot multiple data sets on bar3

7 vues (au cours des 30 derniers jours)
Kristine
Kristine le 27 Fév 2025
Modifié(e) : dpb le 2 Mar 2025

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 commentaires
dpb
dpb le 27 Fév 2025
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.
Kristine
Kristine le 27 Fév 2025
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.

Connectez-vous pour commenter.

Réponses (1)

dpb
dpb le 27 Fév 2025
Déplacé(e) : dpb le 27 Fév 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 commentaires
dpb
dpb le 27 Fév 2025
Modifié(e) : dpb le 27 Fév 2025
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".
dpb
dpb le 2 Mar 2025
Modifié(e) : dpb le 2 Mar 2025
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by