Clear Filters
Clear Filters

how to change width and space in barplot?

9 views (last 30 days)
Amelie
Amelie on 30 Apr 2024
Answered: Voss on 30 Apr 2024
Dear all,
can someone tell me if its possible to change the width of the bars in a barplot (so that they are wider) and obtain some space between the bars (so that I have the same but bigger space across all bars) while keeping the position of the xticks fixed relative to the bars? For example, I have the xticks = 0,5,10,15... so that the 6th bar is exactly at xtick 5, the 11th bar at xtick 10 and so on. I'm trying to keep this while increasing the barwidth and barspacing. But maybe barwidth and barspacing are not optimal for this exercise? Can anyone tell me what I can do?
Thank you very much for your help!
  2 Comments
Voss
Voss on 30 Apr 2024
Can you post some runnable code that produces a bar plot like what you have now? Is it something like this?
N = 16;
data = rand(1,N);
bar(0:N-1,data)
xticks(0:5:N)
It's a little difficult to understand how the bar width and the space between the bars would both increase, unless you mean to increase the space taken up by the entire plot (i.e., increase the axes width).
Amelie
Amelie on 30 Apr 2024
Edited: Voss on 30 Apr 2024
Yes of course! I'm sorry, I've missed to provide a code!
Z=rand(26, 1);
t=0:1:25;
figure()
hold on
grid on
bar(t,Z(1:t(end)+1,1),'FaceColor','r','LineWidth',10,'EdgeColor','none');
f.axis = 12; % added [Voss]
xlabel('Quarters','FontSize',f.axis,'interpreter','latex');
ylabel('Percent','FontSize',f.axis,'interpreter','latex');
hold on
xmax=t(end);
xstep=5;
xlim([0 xmax]);
xticks([0:xstep:xmax]);
In general, I was hoping that there was a way to increase the width of the axis so that it is less compressed. Does this make any sense ? Maybe I'm looking for something that desn't even exist.

Sign in to comment.

Answers (1)

Voss
Voss on 30 Apr 2024
"increase the width of the axis so that it is less compressed"
Well, you can resize the figure, either manually or programmatically. By default, the axes will be resized automatically when the figure changes size.
Here's an example of creating a wide figure programmatically:
Z=rand(26, 1);
t=0:1:25;
figure('Position',[10 10 1000 300]) % make a wide figure
hold on
grid on
bar(t,Z(1:t(end)+1,1),'FaceColor','r','LineWidth',10,'EdgeColor','none');
xlabel('Quarters','FontSize',12,'interpreter','latex');
ylabel('Percent','FontSize',12,'interpreter','latex');
hold on
xmax=t(end);
xstep=5;
xlim([0 xmax]);
xticks(0:xstep:xmax);
Is that on the right track?

Community Treasure Hunt

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

Start Hunting!