Bar3 plot increase bar thickness
9 views (last 30 days)
Show older comments
I am trying to plot the stacked bar3 plot. How to increases the thickness of the bars? All the bars look flat. I need something like this with thick bars stacked next to each other.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/355405/image.png)
What I am getting is:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/355411/image.png)
Below is the code and I have also attached the dependency files. Please let me know a solution.
clc; close all; clear;
% Load mat file
load ('TextureData.mat');
% Plot 3D graph
hFig = figure;
for k = 1:length(imbincountstack)
crackclass = imbincountstack{k};
if (size(crackclass,1) == 1)
crackclassnew(1,:) = crackclass(1,:);
crackclassnew(2,:) = [crackclass(1,1)+1, zeros(1,length(crackclass(1, 2:end)))];
else
crackclassnew = crackclass;
end
z = crackclassnew(:,2:end);
h = bar3(crackclassnew(:, 1), crackclassnew(:, 2:end),width,'stacked');
hold on;
for j=1:length(h)
set(h(j),'xdata',get(h(j),'xdata')+k-1);
set(h(j),'FaceAlpha',texturefeature_inpstruct.alpha)
end
% If crackclass has only one row then pad with zero and hide zero
% bar graph
if (size(crackclass,1) == 1)
for i = 1:numel(h)
index = logical(kron(z(:, i) == 0, ones(6, 1)));
zData = get(h(i), 'ZData');
zData(index, :) = nan;
set(h(i), 'ZData', zData);
end
end
crackclassnew = [];
end
set(gca, 'PlotBoxAspectRatioMode','auto')
set(gca, 'XTickLabel',class_string, 'XTick', 1:numel(class_string), 'FontSize', 20, 'LineWidth', 3) %, ...
ylabel ('Image Resolution (kilo pixels)', 'FontSize', 20)
zlabel ('Number of Images', 'FontSize', 20)
if (texturefeature_inpstruct.lowTextureonTop == 1)
legend ('High Texture','Low Texture','Location','NorthEast');
else
legend ('Low Texture','High Texture','Location','NorthEast');
end
hold off
axis tight;
hYLabel = get(gca,'YLabel');
set(hYLabel,'rotation',-18,'VerticalAlignment','middle')
% resize the figure window
set(hFig, 'units','normalized','outerposition',[0 0 1 1])
0 Comments
Answers (1)
Cris LaPierre
on 4 Sep 2020
If you read Mike's blog post (where your desired image comes from), you'll notice he wrote his own function for creating a 3D stacked bar plot.
For your bar3 plot, there is a scale issue. Your Y-values cover such a large range that the width in Y is getting compressed so that everything fits. The most they can be here is the width of the distance between two values on the Y axis, 1. For fatter bars, you either need to bin your data into groups, change the aspect ratio so the plot extends much further in the Y direction, or plot a subset of the data that does not have such a large range of Y values.
0 Comments
See Also
Categories
Find more on Line Plots 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!