Open In App

Axes Appearance and Behavior in MATLAB

Last Updated : 09 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB offers many types of axes such as polar axes, cartesian axes, etc. The default appearances of these axes are dynamically determined by MATLAB on case-by-case requirements. However, MATLAB does provide its users the option to alter the behavior of these axes, by using some built-in functions. In this article, we shall how we can use these functions to alter the appearance of cartesian axes; the same methods could be applied to polar axes and other available axis types.

Axes Properties:

  1. Axes Ticks
  2. Grids and gridlines
  3. Axes labels
  4. Legends
  5. Multiple Plots on the same axes
  6. Axes Position and aspect ratio
  7. Axes limits

Let us look at each property with an example.

Axes Ticks:

We can change the values of points where the x-ticks are marked as well as their labels. 

Syntax:

xticks([<vector containing tick points>])

xticklabels({<array with the tick labels>})

The same method can be used with y and z ticks.

Example 1:

Matlab




% MATLAB code for Axes ticks
x = linspace(-3,3,100);
plot(x,x.^3)
  
% Changing xtick values
xticks([-2.3,0,2.3])
  
% Changing xtick labels
xticklabels({'x -> -2.3','x -> 0','x -> 2.3'})
  
% Changing ytick values
yticks([-27,0,27])


Output:

 

Grid and Gridlines:

In MATLAB we get the option to display gridlines and the option to have minor gridlines as well. In the case of minor gridlines, the gridlines on tick points are normal whereas the lines in between major ticks are fainter. Syntax of the same is:

plots

…………

grid on

grid minor

See the following implementation for understanding.

Example 2:

Matlab




% MATLAB code for Grid and Gridlines
x = linspace(-3,3,100);
plot(x,x.^3)
  
% Turning on grid
grid on
  
% Turning on minor gridlines
grid minor


Output:

 

As can be seen that the gridlines on the x and y tick points are denser than the ones in between them.

Axes Label:

You can simply change the x, y, and/or z axis’s label by using the following commands

xlabel(‘label1’)

ylabel(‘label2’)

zlabel(‘label3’)

Example 3:

Matlab




% MATLAB code for Axes Label
x = linspace(-3,3,100);
plot(x,x.^3)
  
% Defining x label
xlabel("X-gfg")
  
% Defining y label
ylabel("Y-gfg")


Output:

 

Legends:

We can add legends to a set of axes with the help of the legend() function. 

Syntax: 

legend(text1, …, textN)

Example 4:

Matlab




% % MATLAB code for Legends 
x = linspace(-3,3,100);
  
hold on
plot(x,x.^3)
plot(x,x.^2)
plot(x,x.^4)
hold off
  
% Adding legends
legend("Cubic","Quadratic","Bi-Quadratic")


Output:

 

Multiple Plots on the same axes:

As seen in the previous example, we can add multiple plots on the same axes using the hold trigger. The syntax of the same is:

hold on

plot1

plot2

.

.plot N

hold off

This will plot all the plots that are written in between the hold-on and hold-off commands.

Example 5

The plot of linear, quadratic, and cubic polynomials of y=x. 

Matlab




% % MATLAB code for Multiple Plots
% on the Same axes
x = linspace(-3,3,10000);
  
hold on
plot(x,.1*x)
plot(x,.1*x.^2)
plot(x,.1*x.^3)
hold off


Output:

 

Changing Axes Position and Aspect Ratio:

We can modify the position and size of axes on a figure component by providing the Position parameter to the axes() function. Following is the syntax of the same.

axes_name = axes(‘Position’, [<vector defining the location and size of axes in figure component>])

The vector is a 1×4 row vector that has following syntax

[<position of left side> <position of bottom> <width of axes> <height of axes>]

All of these values can take values from 0 to 1 where 0 defines extreme left/bottom and 1 defines extreme right/top.

See the following example where we create the cartesian axes in center of figure

Example 6:

Matlab




% % MATLAB code f
x = linspace(-3,3,10000);
  
% Defining position in center
ax=axes('Position',[.3 .3 .4 .4]);
plot(ax,x,x.^0.324)


Output:

 

Axes Limits:

We can modify the visible ranges of the x, y, and z axes by the following functions.

xlim([<left_limit> <right_limit>])

ylim([<left_limit> <right_limit>])

zlim([<left_limit> <right_limit>])

This will display the respective axes only in the given range.

Example 7:

Matlab




% % MATLAB code for Axes Limits 
x = linspace(-35,35,10000);
plot(x,sin(x)+cos(x))
  
% Defining x limits
xlim([-pi pi])
  
% Defining y limits
ylim([-2 2])
legend('Sin(x) + Cos(x)')


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads