plot un-equally spaced data in errorbar
3 次查看(过去 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. ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/527169/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/527169/image.jpeg)
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)
0 个评论
采纳的回答
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 个评论
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
- plot against actual x and set ticks to those values; then the labels come along "for free", or
- 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 Center 和 File Exchange 中查找有关 Errorbars 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!