create a graph with one y axis in the left and one y axis in the right with different axis limits. values of the two y axes are referred to the same x values in matlab

To create a graph with one y axis in the left and another y axis in the right with different limits, you can use the yyaxis function in Matlab.

Here is an example code snippet that generates such a graph:

main.m
% Generate some sample data
x = 1:10;
y1 = x.^2;
y2 = exp(x);

% Create the plot
figure;
yyaxis left;
plot(x, y1, 'b');
ylim([0, 100]);
ylabel('y1');

yyaxis right;
plot(x, y2, 'r');
ylim([0, 10000]);
ylabel('y2');

xlabel('x');
title('Graph with dual y-axes');
262 chars
20 lines

In this code, we first generate some sample data (x, y1, and y2).

Next, we create a new figure using figure. We then use yyaxis left to set the left y-axis as the active axis for plotting. We plot y1 against x using plot, and set the y-axis limits using ylim. We then add a y-axis label using ylabel.

We switch to the right y-axis using yyaxis right and repeat the previous steps with y2. We also set the limits for the right y-axis and add a label.

Finally, we add an x-axis label and a title to the plot.

This should produce a graph with two y-axes on opposite sides of the plot, with different limits and labels for each.

related categories

gistlibby LogSnag