plot un-equally spaced data in errorbar

1 次查看(过去 30 天)
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)

采纳的回答

dpb
dpb 2021-2-22
编辑:dpb 2021-2-22
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 个评论
Chaman Srivastava
Chaman Srivastava 2021-2-22
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 2021-2-22
编辑:dpb 2021-2-23
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Errorbars 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by