Bar3 plot increase bar thickness

11 views (last 30 days)
Preetham Manjunatha
Preetham Manjunatha on 2 Sep 2020
Answered: Cris LaPierre on 4 Sep 2020
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.
What I am getting is:
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)))];
crackclassnew = crackclass;
z = crackclassnew(:,2:end);
h = bar3(crackclassnew(:, 1), crackclassnew(:, 2:end),width,'stacked');
hold on;
for j=1:length(h)
% 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);
crackclassnew = [];
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');
legend ('Low Texture','High Texture','Location','NorthEast');
hold off
axis tight;
hYLabel = get(gca,'YLabel');
% resize the figure window
set(hFig, 'units','normalized','outerposition',[0 0 1 1])

Answers (1)

Cris LaPierre
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.


Community Treasure Hunt

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

Start Hunting!

Translated by