In MATLAB, the axis limits of a plot can be adjusted using the "xlim" and "ylim" functions. These functions allow the user to specify the minimum and maximum values for the xaxis and yaxis, respectively.
To set the xaxis limits, use the "xlim" function and specify the desired minimum and maximum values within the parentheses. For example, to set the xaxis limits to 5 and 5, use the following code:
xlim([5 5])
Similarly, to set the yaxis limits, use the "ylim" function and specify the desired minimum and maximum values within the parentheses. For example, to set the yaxis limits to 0 and 10, use the following code:
ylim([0 10])
In addition to setting the axis limits, it is also possible to retrieve the current axis limits using the "xlim" and "ylim" functions without any input arguments. The current xaxis limits will be returned in a vector with the form [xmin xmax], and the current yaxis limits will be returned in a vector with the form [ymin ymax]. For example, to retrieve the current xaxis limits, use the following code:
x_limits = xlim;
It is also possible to set both xaxis and yaxis limits at the same time by using the "axis" function. This function takes four input arguments: [xmin xmax ymin ymax]. For example, to set the xaxis limits to 5 and 5, and the yaxis limits to 0 and 10, use the following code:
axis([5 5 0 10])
It is also possible to specify the axis limits after you plot your data. Example, you have plotted data using plot function and now you want to set the axis limits, you can use the following code:
plot(data)
xlim([5 5])
ylim([0 10])
It is also possible to set the axis limits in proportion to the data. For example, you want to set the axis limits in proportion to the data by specifying that the data should take up 90% of the axis, you can use the following code:
data = randn(100,1);
plot(data)
axis tight
xlim([min(data)std(data) max(data)+std(data)])
In this way you can easily set the axis limits in proportion to the data.
Note that it is also possible to set the axis limits in 3D plots using the "zlim" function, which works in a similar way to the "xlim" and "ylim" functions.
I hope this article has helped you understand how to adjust the axis limits in MATLAB using the "xlim", "ylim" and "axis" functions, and also how to retrieve the current axis limits, set the axis limits after plotting data and set the axis limits in proportion to the data.
In addition to adjusting the axis limits, there are a few other related topics that are useful to know when working with plots in MATLAB.
One such topic is adjusting the aspect ratio of a plot. By default, MATLAB will adjust the aspect ratio of a plot to best fit the data, but sometimes it may be desired to have a specific aspect ratio. This can be achieved using the "daspect" function. The "daspect" function takes a vector of three values, [dx dy dz], which represent the data aspect ratio for the xaxis, yaxis, and zaxis respectively. For example, to set the aspect ratio for a 2D plot to be equal in the x and y dimensions, use the following code:
daspect([1 1 1])
Another related topic is adjusting the axis tick marks. By default, MATLAB will automatically choose the tick marks on the axes, but sometimes it may be desired to have more control over where the tick marks are placed. This can be achieved using the "xticks" and "yticks" functions. The "xticks" function takes a vector of tick mark locations for the xaxis, and the "yticks" function takes a vector of tick mark locations for the yaxis. For example, to set the xaxis tick marks to be at 5, 0, and 5, and the yaxis tick marks to be at 0, 5, and 10, use the following code:
xticks([5 0 5])
yticks([0 5 10])
Another topic that is related to adjusting the axis limits is the grid lines. Grid lines are lines that are drawn on the plot to make it easier to read the data. Grid lines can be added to a plot using the "grid" function, which takes no input arguments. For example, to add grid lines to a plot, use the following code:
grid on
You can also customize the grid lines by changing the properties of the grid lines. For example, you can change the color, style and line width of the grid lines.
grid on
set(gca, 'GridLineStyle', '.')
set(gca, 'GridColor', 'r')
set(gca, 'GridAlpha', 0.5)
Finally, it is also possible to add labels to the xaxis and yaxis using the "xlabel" and "ylabel" functions. These functions take a string input, which will be displayed as the label for the corresponding axis. For example, to add the label "Xaxis" to the xaxis, and "Yaxis" to the yaxis, use the following code:
xlabel('Xaxis')
ylabel('Yaxis')
In this way, you can easily customize your plots by adjusting the axis limits, aspect ratio, tick marks, grid lines and labels.
Popular questions

How can I set the xaxis limits in a MATLAB plot?
Answer: To set the xaxis limits in a MATLAB plot, use the "xlim" function and specify the desired minimum and maximum values within the parentheses. For example, to set the xaxis limits to 5 and 5, use the following code:xlim([5 5])

How can I retrieve the current xaxis limits in a MATLAB plot?
Answer: To retrieve the current xaxis limits in a MATLAB plot, use the "xlim" function without any input arguments. The current xaxis limits will be returned in a vector with the form [xmin xmax]. For example, to retrieve the current xaxis limits, use the following code:x_limits = xlim;

How can I set the xaxis and yaxis limits at the same time in a MATLAB plot?
Answer: To set the xaxis and yaxis limits at the same time in a MATLAB plot, use the "axis" function. This function takes four input arguments: [xmin xmax ymin ymax]. For example, to set the xaxis limits to 5 and 5, and the yaxis limits to 0 and 10, use the following code:axis([5 5 0 10])

How can I set the axis limits in proportion to the data in a MATLAB plot?
Answer: To set the axis limits in proportion to the data in a MATLAB plot, you can use the "axis tight" function and then set the limits using the "xlim" or "ylim" function. The limits will be set as min(data)std(data) and max(data)+std(data). 
How can I add grid lines to a MATLAB plot?
Answer: To add grid lines to a MATLAB plot, use the "grid" function with the input argument "on", this will turn on the grid lines. For example, to add grid lines to a plot, use the following code:grid on
You can also customize the grid lines by changing the properties of the grid lines. For example, you can change the color, style and line width of the grid lines.
grid on
set(gca, 'GridLineStyle', '.')
set(gca, 'GridColor', 'r')
set(gca, 'GridAlpha', 0.5)
Tag
Plotting