How to plot multiple data sets on bar3

조회 수: 8 (최근 30일)
Kristine
Kristine 2025년 2월 27일
편집: dpb 2025년 3월 2일

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
dpb
dpb 2025년 2월 27일
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 2025년 2월 27일
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.

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

답변 (1개)

dpb
dpb 2025년 2월 27일
이동: dpb 2025년 2월 27일
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
dpb
dpb 2025년 2월 27일
편집: dpb 2025년 2월 27일
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 2025년 3월 2일
편집: dpb 2025년 3월 2일
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.

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

카테고리

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

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by