-1

Without specifying any values, how can I write code that will align the plot axes such that the minimum axis value of either x or y (that MATLAB will choose automatically if unassigned) is now the minimum for both axes? Example: If x would have started at 0 and y would have started at 5, I've now forced them both to be 0.

AAC
  • 563
  • 1
  • 6
  • 18

1 Answers1

1

You can get and set the bounds for the Axes object with its XLim and YLim properties. These properties have have two values each, lower and upper bound. The first value is the lower bound. For example, for axes with handle 'ah', the lower x-axis bound is ah.XLim(1).

You want both axes to start at the lower of the two lower bounds:

ah = gca; % Get the current axes, you can use a handle you already have
low = min(ah.XLim(1), ah.YLim(1));
ah.XLim(1) = low;
ah.YLim(1) = low;

As @Wolfie comments below, there are also the functions xlim and ylim to get and set the XLim and YLim properties. I always thought they were superfluous, since they don't really simplify accessing these properties.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • 1
    Note that, `xlim(1)` is `min(xlim)` so you could just do `low = min([xlim, ylim])`. `xlim` and `ylim` should not be used as variable names, since they are the in built function names for getting and setting the axes limits (without needing to use `get` or `set`), as they would be used in my `low` definition at the start of this comment. With this in mind, your solution could be replaced by `xlim( [min([xlim, ylim]), max(xlim)] ); ylim( [min([xlim, ylim]), max(ylim)] )`. Reading this back, I realise I sound condescending, apologies, your logic is sound, variable names only real issue! – Wolfie May 22 '18 at 08:06
  • 1
    @Wolfie: Good points, though. Thanks! -- I think you are telling me not to post answers late at night... :) – Cris Luengo May 22 '18 at 13:03