Graph with Multiple x-Axes and y-Axes

This example shows how to create a graph using the bottom and left sides of the axes for the first plot, and the top and right sides of the axes for the second plot.

Create the data to plot.

x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;

Use the line function to plot y1 versus x1 using a red line. Set the color for the x-axis and y-axis to red. Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

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. Specify the location of the x-axis as the top of the graph and the y-axis as the right side of the graph. Set the axes Color to 'none' so that the first axes is visible underneath the second axes.

ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...

Use the line function to plot y2 versus x2 on the second axes. Set the line color to black so that it matches the color of the corresponding x-axis and y-axis.


The graph contains two lines that correspond to different axes. The red line corresponds to the red axes. The black line corresponds to the black axes.

See Also

| |

Related Examples

Was this topic helpful?