I would like to create a bar plot with bars of specified width. I have tried a lot but so far nothing has worked for me.
Each element of x is the center of an interval. I would like the center of each bar to be in that position, and the width of this bar should correspond to the length of the interval. y contains the height of each bar. My goal is to obtain a bar plot with bars of varying width.
% inter contains the limits of my intervals
inter = [-32.6;-31.3;-20.3;-19.0;-15.4;-14.1;-11.7;-10.4;-8.8];
x = [ -31.6000; -19.8000; -17.4000; -13.1500; -10.5000; -8.8000];
y = [ 2.3529; 1.0417; 1.3158; 1.5337; 2.5000; 1.0152];
% trying to create the bar plot, however, all widths are the same:
bar(x,y);
You could use patch in order to draw each bar yourself. We can use inter(k) for the left and inter(k + 1) for the right edge of each bar k, and the height we already know is y(k).
figure;
for k = 1 : length(y)
xl = inter(k);
xr = inter(k + 1);
patch([xr, xr, xl, xl], [0, y(k), y(k) 0], 'b');
end
It turns out, you don't even need vector x.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With