axis command fails for log scale

6 views (last 30 days)
Charles
Charles on 22 Jan 2018
Answered: Charles on 23 Jan 2018
figure
plot([1 2], [1 2])
axis([0 3 0 3])
axis(gca)
set(gca,'yscale','log')
%axis auto
axis(gca)
The second axis command does not return the correct value. It returns zero for the min Y axis limit, and does not reflect any of the changes from switching to the log scale. Including the axis auto function is a partial workaround for this example, but for some reason does not work the same way in my (very complicated) example.

Answers (2)

Jan
Jan on 22 Jan 2018
It is not the axis command, which fails, but the axes contains the wrong values.
ax = axes;
plot([1 2], [1 2]);
ax.YLim = [0, 3];
ax.YScale = 'log';
drawnow;
ax.YLim % Returns: 0 3
But the displayed range is [1, 3]. This seems to be a bug.
The workaround is easy: Either let Matlab set the Y-limits automatically or write you own function, which avoids to set the Y-limit of a log-scaled axis to the impossible value 0.
  1 Comment
Charles
Charles on 22 Jan 2018
Thanks Jan. I currently have a workaround a bit like that - I use 'axis auto' and then combine the axis limits from before and after in the combination I need.
Seeing as we agree it's a bug, do we need to do anything to send it up the chain of command?

Sign in to comment.


Charles
Charles on 23 Jan 2018
I did a bit more experimenting, and it appears to only be a problem if you set the Y axis limits to zero before changing the Y axis scale to log - matlab auto adjusts the plot but does not update the axes values. If the axis limit was set to zero automatically by matlab, the problem does not occur - matlab auto adjusts the scale as well as the axes values. I think this is what Jan was getting at. If you set a negative limit, you get a warning.
When you set the limit to zero before changing to log scale, matlab is being forced to behave in auto mode despite being in manual mode, but only does half the job - it updates the plot but not the properties. I am suspicious that this was done deliberately - for example it would preserve the manually set Y axis limits after changing to log and then back to linear scale. But it would make more sense to handle this better - eg warning people for a zero limit the same way they do for negative limits. I think even that is a bit clunky - better to just add an extra property for the manually set linear axis limits (max as well as min if you want to be robust) that only get used when you switch from log to linear scale in manual mode.
My script has to set the Y limit to zero in some scenarios, but if I set it to any nonzero value prior to changing to log scale, or use axis auto after, I should be able to make it behave.

Categories

Find more on Exponents and Logarithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!