dkv
dkv

Reputation: 7302

Change Matlab default x-limits behavior

If I run

plot(1:1001)

Matlab automatically sets the x range to be 1:1200. I am aware that xlim([1,1001]) exists, but I don't want to add this line every single time I use plot.

Is there a way to change Matlab's behavior so that plot sets the x-limits equal to the range of the data by default instead of having to do it manually every time? (For example with a flag I can set at the top of my script).

Or a parameter I can pass to plot to make this happen?

Upvotes: 4

Views: 1314

Answers (3)

Aero Engy
Aero Engy

Reputation: 3608

You could set your default XlimSpec property for Axes.

set(0,'defaultAxesXLimSpec', 'tight')

Then when you plot it will use X axes tight setting for every new plot.

This will revert back to normal after you close & restart Matlab. However, you could add that to your startup script to always apply it.

This meets some of your requirements I saw you mentioned in the comments about not using a wrapper and trying to minimize running extra commands after call plot

Upvotes: 3

Luis Mendo
Luis Mendo

Reputation: 112749

You want the x-axis limits to tightly fit your data. If you don't mind the y-axis also having that behaviour, you can simply use

plot(1:1001)
axis tight

From the documentation,

axis tight sets the axis limits to the range of the data


If you want only the x-axis to be tight, you can do it manually as follows:

h = plot(1:1001); % h is a line object
h.Parent.XLim = [min(h.XData) max(h.XData)]; % set x-axis limits as per the line data

Note that the second line uses dot notation, which is available since R2014b. For older Matlab versions use set/get .


Or you can do it automatically by setting the seemingly undocumented 'XLimSpec' property of the axis to 'tight':

plot(1:1001)
set(gca, 'XLimSpec', 'tight')

which is what axis tight internally does (for the x-, y- and z-axis), at least in R2017a.

Upvotes: 1

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

Matlab uses an internal algorithm to determine the best interval of axis ticks. I doubt you can manipulate it and, even if it was possible, I recommend you not to do it. Such operation would change the behavior of your own Matlab installation, but everybody else using your code would stumble upon a different axis labelling probably not well fitting with your purposes.

Keep on defining your ticks manually or implement your own generalized logics into a separate function that you can release together with your scripts. This code shows how to implement one, and can represent a good starting point for you.

If you want to make things simpler, create a wrapper of the plot function as follows:

h = plot_wrapper(true,1:101,1:101);

function varargout = plot_wrapper(fix_limits,varargin)

    han = plot(varargin{:});

    if (fix_limits)
        x = get(han,'XData');
        xlim(gca,[min(x) max(x)]);

        y = get(han,'YData');
        ylim(gca,[min(y) max(y)]);
    end

    if (nargout)
        varargout{1} = han;
    end

end

Upvotes: 2

Related Questions