How to change the size of bar in bar3 plot?
2 views (last 30 days)
Show older comments

In the attached bar3 plot, in 'kanal 1' and 'kanal 2' axis values are not equidistant. But in plot all the bar are same square sized only height is different. Is there and command to change the size for better representation? A sample code is given here:
bar3(rand(3,4))
set(gca,'XTickLabel',[10 20 30 40])
set(gca,'YTickLabel',[100 200 300])
I just need non square sized bar. Bar size need to be changed according to axis label value. If there any other plotting suggestions, it will be warmly welcomed. Thanks
0 Comments
Answers (2)
Anudeep Kumar
on 13 May 2025
Hey Md Asraful Kabir,
As far as I understood from the documentation of 'bar3', it spaces bars equally regardless of what tick label say. In order to plot bars spaced and sized according to actual axis values, we need to plot the bars manually.
This can be achieved using ‘fill3’ function.
Below is a small code snippet to do the same:
% Data
Z = rand(3,4);
% Define true axis values
xvals = [10 20 30 40];
yvals = [100 200 300];
% Bar width (as fraction of the minimum spacing)
xw = min(diff(xvals)) * 0.8;
yw = min(diff(yvals)) * 0.8;
% Plot bars manually
figure; hold on
for i = 1:length(yvals)
for j = 1:length(xvals)
% Bar base position
x0 = xvals(j) - xw/2;
y0 = yvals(i) - yw/2;
z0 = 0;
h = Z(i,j);
% Draw bar as a cuboid (6 faces)
%with the specified RGB value (e.g., [0.7 0.7 1] is a light blue).
fill3([x0 x0 x0+xw x0+xw], [y0 y0+yw y0+yw y0], [z0 z0 z0 z0], [0.8 0.8 0.8]); % bottom
fill3([x0 x0 x0+xw x0+xw], [y0 y0+yw y0+yw y0], [h h h h], [0.8 0.8 0.8]); % top
fill3([x0 x0 x0 x0], [y0 y0 y0+yw y0+yw], [z0 h h z0], [0.7 0.7 1]); % left
fill3([x0+xw x0+xw x0+xw x0+xw], [y0 y0 y0+yw y0+yw], [z0 h h z0], [0.7 0.7 1]); % right
fill3([x0 x0+xw x0+xw x0], [y0 y0 y0 y0], [z0 z0 h h], [0.6 0.6 1]); % front
fill3([x0 x0+xw x0+xw x0], [y0+yw y0+yw y0+yw y0+yw], [z0 z0 h h], [0.6 0.6 1]); % back
end
end
xlabel('Kanal 1')
ylabel('Kanal 2')
zlabel('Value')
set(gca, 'XTick', xvals, 'YTick', yvals)
view(45,30)
box on
hold off
I have hereby attached the documentation of ‘fill3’ for your reference so that you can modify the function based on your requirement.
I hope this helps!
0 Comments
Adam Danz
on 13 May 2025
Edited: Adam Danz
on 13 May 2025
The problem is that bar3 does not have an input argument for x coordinates and there's no easy way to update the x coordinates of the bars.
You could, however, set the axes' DataAspectRatio to stretch the data to the desired aspect ratio.
rng default
b = bar3(rand(3,4));
ax = ancestor(b(1),'axes');
ax.XTickLabel = [10 20 30 40];
ax.YTickLabel = [100 200 300];
axis(ax,'equal')
ax.DataAspectRatio = [200 30, 15];
Notice that the X aspect value (200) is approximately the desired Y-Data range and the Y aspect value (30) is approximately desired the X-Axis range. The Z aspect value acts as a scaler. Increase and decrease it to see what I mean.
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!