Search code examples
matlabgraphicsmatlab-figureaxes

Change axis range in Matlab stem plot


The x-axis range seems to begin at the first data point and end at the last by default. I'd like to extend this a little bit in both directions so my graph looks zoomed out a bit. How do I set this? I don't see it in the stem documentation.

example code:

f = [0.0 0.45 0.55 1.0];
a = [1.0 1.0 0.0 0.0];

filter = firpm(10,f,a);

plot(f,a);
stem(filter);

and I want to change the x-axis from 0 to 20 (it currently defaults at 1 to 11).


Solution

  • This is not done by stem or any other plotting function. To control axis range you use either axis:

    axis(limits) specifies the limits for the current axes. Specify the limits as vector of four, six, or eight elements. [...]

    or xlim:

    xlim(limits) specifies the x-axis limits for the current axes. Specify limits as a two-element vector of the form [xmin xmax], where xmax is greater than xmin. [...]
    xl = xlim returns a two-element vector containing the current limits. [...]

    For example, to extend the current range of the x axis 1 unit to each side:

    xlim(xlim + [-1 1])
    

    (note that this uses the two types of calls described in the xlim documentation excerpts above).

    Or, in your specific example,

    xlim([0 20])