large errorbars on semilogx

I am trying to plot mid vs columns. And I am getting either partial or very elongated errorbars. Please find the datafile and plot below. Will really appreciate a solutions to this issue. Thanks.

Answers (2)

What's the difference between the logarithm of 1 and the logarithm of 2?
format longg
x1 = log([1 2])
x1 = 1×2
0 0.693147180559945
delta1 = diff(x1)
delta1 =
0.693147180559945
Now what's the difference between the logarithms of 11 and 12?
x2 = log([11 12])
x2 = 1×2
2.39789527279837 2.484906649788
delta2 = diff(x2)
delta2 =
0.0870113769896297
How much larger is the difference between logarithms?
delta1/delta2
ans =
7.96616723629781
So an errorbar of length 1 will be drawn much longer (about 8 times as long) if it starts around x = 1 than if it starts around x = 11. What value would you have to use as the right endpoint for the x = 11 errorbar to make it appear as long as the errorbar of length 1 starting at x = 1?
x3right = exp(x2(1)+delta1)
x3right =
22
semilogx([1 2 NaN 11 12 NaN 11 x3right], [1 1 1 2 2 2 3 3])
ylim([0 4])
The line at y = 1 looks to be about the same length as the one at y = 3.
A quick check on my monitor with a ruler shows that the line at y = 2 is about an eighth as long as the lines at y = 1 and y = 3.

2 Comments

any solution to that?
Solution to what? To making the error bars longer?
Either don't use a log scale on the axes or change your data about how long the error bars should be. In my example, the approach involving x3right was an example of the latter. For an example of the former:
plot([1 2 NaN 11 12], [1 1 1 2 2])
ylim([0 3])
These lines are the same length on my screen.

Sign in to comment.

What you see is because of the log XScale:
  • Partial errorbars: If the left endpoint of the errorbar would be <= 0, it is not rendered on log scale, because log of a number <= 0 is complex or -Inf.
  • Elongated errorbars: also an effect of logarithmic x-scaling. For example, a unit-length segment takes up more space in the plot if it goes from 1 to 2 than it takes if it goes from 10 to 11 (that's just how logarithmic scales work).
semilogx([1 2],[1 1])
hold on
semilogx([10 11],[1 1])
xlim([0.1 100])

5 Comments

Is there any solution for this? How can I have errorbars on both sides. When I use scatterplot and plot log values. I dont get the same problem.
"When I use scatterplot and plot log values. I dont get the same problem."
If you try to scatter plot a point with a non-positive x-coordinate on a log x-scale plot, you will get the same problem. Notice the warning, and notice that only one point appears even though I gave scatter two points.
figure()
scatter([-1 2],[1 2])
set(gca,'XScale','log')
drawnow(); % to see the warning:
Warning: Negative data ignored
"Is there any solution for this?"
First, some data generating a non-positive errorbar left endpoint:
x = 1:10;
y = 1:10;
err = 2*ones(1,10);
err(1) = 3; % make the first errorbar go negative ( 1 - 3/2 = -0.5 < 0 )
Plotting the errorbars the usual way gives a cut-off errorbar and a warning:
figure()
errorbar(x,y,[],[],err/2,err/2,'LineStyle','none','Marker','v')
set(gca(),'XScale','log')
ylim([0 11])
drawnow(); % to see the warning:
Warning: Negative data ignored
One solution is to limit the plotted errors so that the errorbar endpoints are all positive.
xmin = 0.01; % pick the lower x-limit you want the plot to have
% adjust the left error widths:
left_err = err/2;
idx = x-left_err<=0;
left_err(idx) = x(idx)-xmin;
% plot errorbars with those new left widths (right widths remain the same as before):
figure()
errorbar(x,y,[],[],left_err,err/2,'LineStyle','none','Marker','v')
set(gca(),'XScale','log')
ylim([0 11])
I got an error:
Error using errorbar
The orientation flag can only be used for 2, 3, or 4 data arguments.
Voss
Voss on 19 Mar 2024
Edited: Voss on 19 Mar 2024
Please share your code, at least the line of code where you call the errorbar function.
Or compare your errorbar call to mine and figure out what you need to change to make it work.
figure()
set(gca(),'XScale','log')
semilogx((col1),mid,'*m',LineStyle='none',LineWidth=1.5,MarkerSize= 5 )
hold on
errorbar(col1,mid,(err_col1),'horizontal',LineStyle='none', Color='m',LineWidth = 0.1,CapSize=2)
grid on
hold off

Sign in to comment.

Asked:

on 18 Mar 2024

Commented:

on 19 Mar 2024

Community Treasure Hunt

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

Start Hunting!