plot a graph with two different y values related to the same x values in matlab

To plot a graph with two different y values related to the same x values in Matlab, you can use the yyaxis command. Here is an example code:

main.m
% Generate some sample data
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);

% Plot the data using yyaxis
yyaxis left % plot y1 on the left y-axis
plot(x, y1, 'b-')
ylabel('y1')
yyaxis right % plot y2 on the right y-axis
plot(x, y2, 'r-')
ylabel('y2')
xlabel('x')
256 chars
14 lines

In this code, yyaxis left sets the left y-axis as active, and yyaxis right sets the right y-axis as active. The plot function is then used to plot the data on the respective y-axes. Note that we also include xlabel and ylabel commands to label the axes.

related categories

gistlibby LogSnag