
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set Axis Limits in MATLAB
MATLAB provides various built-in functions, such as xlim(), ylim(), and axis() that help us to adjust axis limits as per our requirements. In this tutorial, we will learn about adjusting axis limits of a plot in MATLAB.
Functions to Set Axis Limits
In MATLAB, there are three main functions widely used for adjusting axis limits of a plot. These functions are as follows:
"xlim()" Function - The "xlim()" function is used to adjust X-axis limit of a plot in MATLAB.
"ylim()" Function - The "ylim()" function is used to adjust Y-axis limit of a plot in MATLAB.
"axis ()" Function - The "axis()" function is used to adjust both X-axis and Y-axis limits simultaneously or automatically.
The implementation of these functions in MATLAB programming is demonstrated with the example programs in the below sections of this article.
"xlim()" and "ylim()" Functions
In MATLAB, the "xlim()" and "ylim()" functions are used to adjust the X-axis and Y-axis limits individually.
Syntax
xlim([left_limit, right_limit]); ylim([left_limit, right_limit]);
Let us consider an example to understand this implementation.
Example
% MATLAB program for adjusting axes limits individually % Create a sample vector of data x = linspace(0, 50, 10); y = x.^2; % Plot the x and y data on graph plot(x, y); % Set the X and Y axis limits xlim([0, 50]); % Setting X axis limits from 0 to 50 ylim([0, 2500]); % Setting Y axis limits from 0 to 2500
Output
Explanation
In this MATLAB program, first of all we call "linspace" function to create a vector "x", having linearly spaced values from 0 to 50. Then, we calculate square of values of "x" and store them in another vector "y". After that we call "plot" function to plot the values of "x" and "y". Next, we call functions "xlim" and "ylim" to adjust the X-axis and Y-axis limits respectively. In this example, we set the X-axis limit from 0 to 50 and the Y-axis limit from 0 to 2500.
"axis()" Function
In MATLAB, the "axis()" function allows use to adjust both X-axis and Y-axis limits simultaneously.
Syntax
axis([x_left_limit, x_right_limit, y_left_limit, y_right_limit]);
Let us now consider an example program to understand the implementation of axis() function.
Example
% MATLAB program to adjust both axes limits simultaneously % Create a sample vector of data x = linspace(-5, 5, 11); y = x.^3; % Plot the x and y data on graph plot(x, y); % Set the X axis and Y axis limits simultaneously axis([-6, 6, -200, 200]) % Setting X-axis limits from -6 to 6 and Y-axis limits from -200 to 200
Output
Explanation
In the above MATLAB program, we start by calling the "linspace" function to create a vector "x", having linearly spaced values from -5 to 5. Then, we calculate cubes of values of "x" and store them in another vector "y". After that we call "plot" function to plot the values of "x" and "y". Next, we call "axis" function to set the X-axis and Y-axis limits simultaneously. In this example, we set the X-axis limits from -6 to 6 and the Y-axis limits from -200 to 200.
"axis auto" Function
In MATLAB, we can use the "axis" function with auto option to adjust the X-axis and Y-axis limits automatically depending on the range of the data.
Syntax
axis auto;
The following MATLAB program demonstrate the implementation of the "axis" function with auto option to adjust axis limits of a plot automatically.
Example
% MATLAB program to adjust axes limits automatically % Create a sample vector of data x = linspace(-5, 5, 11); y = x.^3; % Plot the x and y data on graph plot(x, y); % Set the X axis and Y axis limits simultaneously axis auto; % Adjusting X-axis and Y-axis limits automatically
Output
Explanation
In this MATLAB program, firstly, we call the "linspace" function to create a vector "x", having linearly spaced values from -5 to 5. Then, we compute cube of values of vector "x" and store them in another vector "y".
After that we call the "plot" function to plot the values of "x" and "y". Then, we call "axis" function with "auto" parameter to set the X-axis and Y-axis limits automatically based on data range. In this example, MATLAB automatically set the X-axis limits from -5 to 5 and the Y-axis limits from -150 to 150 depending on the data ranges.
Conclusion
In conclusion, MATLAB has three built-in function "xlim", "ylim", and "axis" to adjust the axis limits of a plot in MATLAB. We have illustrated the use of these three function with different parameter values in the above programs.