using subplot in bar , how to adjust bar width

17 views (last 30 days)
peter huang
peter huang on 2 Oct 2022
Answered: Chunru on 2 Oct 2022
how to adjust unified bar width in subplot
ex. code
clear all ; clc ;clf
x1 = [1 2 3];
y1 = [1 2 3 ];
x2 = [1 2 3 4 5];
y2 = [1 2 3 4 5];
%%
subplot(211)
bar(x1 ,y1)
subplot(212)
bar(x2 ,y2)

Answers (1)

Chunru
Chunru on 2 Oct 2022
x1 = [1 2 3];
y1 = [1 2 3 ];
x2 = [1 2 3 4 5];
y2 = [1 2 3 4 5];
% Make xlim same so that the bar width is the same
h1= subplot(221);
bar(x1 ,y1)
h2 = subplot(222);
bar(x2 ,y2);
set([h1 h2], 'Xlim', [0 6])
% Adjust the bar width
h3=subplot(223);
hbar1=bar(x1 ,y1, 0.8);
h4=subplot(224);
hbar2 = bar(x2 ,y2, 0.8);
xl1 = xlim(h3);
xl2 = xlim(h4);
s = diff(xl2)/diff(xl1);
hbar1.BarWidth = 0.8/s;

Community Treasure Hunt

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

Start Hunting!