Using errorbar() with semilogy() in MATLAB?
Asked Answered
A

2

6

I'd like to plot data x & y with errorbars, ebar, and its fit, yfitted, on a semilog plot. This doesn't seem to work:

figure;
hold on;
errorbar(x,y,ebar);
semilogy(x,yfitted);

Instead of semilog plot I get a linear plot. What should I be doing differently?

Aldwon answered 23/8, 2010 at 17:53 Comment(0)
D
9

try

h = errorbar(x,y,ebar);
set(get(h,'Parent'), 'YScale', 'log')

or

ax = axes();
errorbar(ax, x,y,ebar);
set(ax, 'YScale', 'log');
Discrown answered 23/8, 2010 at 18:8 Comment(2)
sort of works but it screws all of the errorbars up.. e.g. it fails to draw some of the vertical linesAldwon
As you probably realize, you can't take the log of 0 or a negative numberDiscrown
D
3

This is what the documentation says.

"If you attempt to add a loglog, semilogx, or semilogy plot to a linear axis mode graph with hold on, the axis mode will remain as it is and the new data will plot as linear"

I would suggest that you just reverse the order of your plotting, that is.

semilogy(x,yfitted);
hold on;
errorbar(x,y,ebar);
Diametral answered 23/8, 2010 at 18:11 Comment(2)
It should work. Are you writing >>figure; >>hold on; before using semilogy? If you do, it wont work. Just write the three lines in my post and you'll get the same result as the answer above.Diametral
I got this solution to work on 2010a, so +1, but it's more direct and generalizable to set axis properties directly than to worry about what matlab freezes with the hold commandDiscrown

© 2022 - 2024 — McMap. All rights reserved.