plot un-equally spaced data in errorbar

1 view (last 30 days)
Hello.
I'm trying to plot unequally spaced data in error bar plot with vertical error bar. My script looks something like this: But my xtick value is repeated as shown below. What am i doing wrong? I just want the x axis from 28-->224 and not the extra marker after it.
x1 = [28 56 112 224];
xt = 1:length(x1);
y1 = [21.2 18.01 16.8 14.38];
sd_vct1 = [4.93 1.77 1.81 2.32];
figure(1)
errorbar(xt,y1,sd_vct1,'-ob')
set(gca, 'XTickLabel',x1)

Accepted Answer

dpb
dpb on 22 Feb 2021
Edited: dpb on 22 Feb 2021
You're looking for
x1 = [28 56 112 224];
y1 = [21.2 18.01 16.8 14.38];
sd_vct1 = [4.93 1.77 1.81 2.32];
errorbar(x1,y1,sd_vct1,'-ob')
instead of plotting versus ordinal position and then 'XTickLabels'
The labels will come along for the ride for free with the ticks as well as showing the real trend of the data with x...
If it really is evenly-spaced errorbars that are wanted, then
errorbar([],y1,sd_vct1,'-ob')
hAx=gca;
xlim(xlim+[-0.2 0.2])
xticks(1:numel(x1))
hAx.XTickLabel=x1;
Actually, since the x1 are in power ratio, one can get the latter effect by
errorbar(x1,y1,sd_vct1,'-ob')
hAx=gca;
xticks(x1)
hAx.XScale='log';
hAx.XMinorTick='off';
xlim(xlim+[xlim.*[-10 10]/100])
  2 Comments
Chaman Srivastava
Chaman Srivastava on 22 Feb 2021
Hi dpb,
Thanks for the answer. It does work, but I'm wondering what is wrong is my script? Also I didn't understand the the latter script? Isn't there something straight forward, just wondering :)
dpb
dpb on 22 Feb 2021
Edited: dpb on 23 Feb 2021
Your initial script tried to write tick labels at the four places of the input x vector x1 but the default ticks were/are
>> xticks
ans =
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000
>>
or seven ticks but only four lablels. Per the documentation for axes,
"XTickLabel, YTickLabel, ZTickLabel — Tick labels
'' (default) | cell array of character vectors | string array | categorical array
Tick labels, specified as a cell array of character vectors, string array, or categorical array. If you do not want tick labels to show, then specify an empty cell array {}. If you do not specify enough labels for all the ticks values, then the labels repeat."
So, since you only had four labels for seven positions, you get the four at the first four ticks and then start over again with the first label at the fifth tick.
The fix is either
  1. plot against actual x and set ticks to those values; then the labels come along "for free", or
  2. if do plot ordinal, set the ticks to those ordinal values so the number of ticks matches the number of labels.
The last option gives the appearance of a linear/ordinal plot but uses the actual data. This works because and only because, your x values are different by a factor of two between each case--that means are linearly proportional to log(x). So, I just set the x axis to log scale and "voila!", the spacing is linear between them, just as it is if/when plotted against ordinal number.
The last line isn't needed; it just adds a little room on the two ends so the first/last plotted points don't fall on the axes boundaries. Since is log-scaled, I used a muliplier of 10% under/over instead of the same linear scale for it as did when plotted versus ordinal position on linear scale. This makes the actual width the same for the same reason log(x1) is linear--it's a multipier, not an adder(*).
(*) Remember why logs were so valuable in the days before calculators -- multiplication/divison is addition/subtraction of logarithms, a much easier piece of work!

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!