This example shows how to create a chart using the bottom and left sides of the axes for the first plot and the top and right sides for the second plot.
Plot a red line using the
line function. Set the color for the x-axis and y-axis lines to red.
Note: Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the
set function instead, such as
figure x1 = 0:0.1:40; y1 = 4.*cos(x1)./(x1+2); line(x1,y1,'Color','r') ax1 = gca; % current axes ax1.XColor = 'r'; ax1.YColor = 'r';
Create a second axes in the same location as the first axes by setting the position of the second axes equal to the position of the first axes. Display the x-axis at the top of the axes and the y-axis on the right side. Set the axes
'none' so that the first axes is visible underneath the second axes.
Note: Starting in R2014b, you can use dot notation to query properties. If you are using an earlier release, use the
get function instead, such as
ax1_pos = get(ax1,'Position').
ax1_pos = ax1.Position; % position of first axes ax2 = axes('Position',ax1_pos,... 'XAxisLocation','top',... 'YAxisLocation','right',... 'Color','none');
Plot a line in the second axes. Set the line color to black so that it matches the color of the corresponding x-axis and y-axis.
x2 = 1:0.2:20; y2 = x2.^2./x2.^3; line(x2,y2,'Parent',ax2,'Color','k')
The chart contains two lines that correspond to different axes. The red line corresponds to the red axes. The black line corresponds to the black axes.