combining multiple bar graph
3 views (last 30 days)
Show older comments
I have 4 bar graphs. How to combine them into one graph.
It should look loke a 3D graph.
This is the code.
data{i} has 4 cells each of dimension 24x13.
for i = 1:4
data{i} = xlsread(filename,i);
figure
bar3(data{i},'stacked')
end
0 Comments
Answers (1)
ag
on 25 Sep 2024
Hi Nethravathi,
To combine multiple 3D bar graphs into a single figure, you can use the bar3 function and manipulate the positions of each set of bars to ensure they don't overlap.
The below code snippet demonstrates how to achieve this, using dummy data:
% Number of datasets
numDatasets = 4;
% Dimensions of each dataset
numRows = 24;
numCols = 13;
% Generate random data for each dataset
data = cell(1, numDatasets);
for i = 1:numDatasets
data{i} = rand(numRows, numCols); % Replace this with your actual data
end
% Create a new figure
figure;
hold on; % This allows multiple plots in the same figure
% Define an offset for each dataset to prevent overlap
offset = 2; % Adjust this value to change the spacing between datasets
% Plot each dataset using bar3
for i = 1:numDatasets
% Adjust the x-position of each dataset
xOffset = (i - 1) * (numCols + offset);
% Plot the 3D bar graph with an offset
bar3(data{i}, 'stacked');
% Adjust the XData to offset the bars
h = get(gca, 'Children');
for j = 1:length(h)
xData = get(h(j), 'XData');
set(h(j), 'XData', xData + xOffset);
end
end
% Set axis labels
xlabel('Category');
ylabel('Group');
zlabel('Value');
% Set title
title('Combined 3D Bar Graph');
% Adjust viewing angle for better visualization
view(-30, 30);
% Release the hold on the current figure
hold off;
For more details, please refer to the following MathWorks documentation: bar3 - https://www.mathworks.com/help/matlab/ref/bar3.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Directed Graphs 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!