Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Graph with Multiple x-Axes and y-Axes

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

Define 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 line to plot y1 versus x1. Set the color for the line, x-axis, and y-axis to red.

```figure % new figure window
line(x1,y1,'Color','r')
haxes1 = gca; % handle to axes
set(haxes1,'XColor','r','YColor','r')
```

The line function automatically uses the bottom and left sides as the x-axis and y-axis.

Create another 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.

```haxes1_pos = get(haxes1,'Position'); % store position of first axes
haxes2 = axes('Position',haxes1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
```

Plot y2 versus x2 using the second axes. Set the line color to black so that it matches the color of the corresponding x-axis and y-axis.

```line(x2,y2,'Parent',haxes2,'Color','k')
```

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