Help with a double plot

7 views (last 30 days)
aurc89
aurc89 on 19 Sep 2014
Answered: Guillaume on 19 Sep 2014
I need to plot two functions, y1 and y2, versus the same axis x. y1 values are very 'distant' from the ones of y2 (eg. y1=[2 4 7 3 5 6], y2=[25000 56000 78000 32000 78000 39000]). Thus, I need to plot y1 and y2 on two different scales but on the same plot, for example y1 between 0 and 10 and y2 between 0 and 100000. A command like:
plot(x,y1,x,y2)
cannot give me that. How can I do? thanks

Accepted Answer

Guillaume
Guillaume on 19 Sep 2014
I would use xlim, if you only want to change the limits of the x axis. plotyy creates two axis, so you need to change the limits for both of them.
axs = plotyy(x, y1, x, y2);
xlim(axs(1), [2 5]);
xlim(axs(2), [2 5]);
%or arrayfun(@(ax), xlim(ax, [ 2 5]), axs);

More Answers (2)

Mischa Kim
Mischa Kim on 19 Sep 2014
Edited: Mischa Kim on 19 Sep 2014
Use, plotyy:
x = 1:6;
y1 = [2 4 7 3 5 6]:
y2 = [25000 56000 78000 32000 78000 39000];
plotyy(x,y1,x,y2)
  1 Comment
aurc89
aurc89 on 19 Sep 2014
Edited: aurc89 on 19 Sep 2014
Thanks! There's still a problem: If I want to visualize the plot only between two values of x, let's say between 2 and 5, I use the command
axis([2 5 min max])
where min and max are two values that I set, and this doesn't matter; the problem is along x, because only the function y1 is zoomed between 2 and 5 !

Sign in to comment.


Image Analyst
Image Analyst on 19 Sep 2014
That's certainly bizarre. Not sure why the x axis gets so messed up. Here's a workaround:
x = 1:6;
y1 = [2, 4, 7, 3, 5, 6];
y2 = [25000, 56000, 78000, 32000, 78000, 39000];
plotyy(x,y1,x,y2)
% xlim([2,5]); % Produces messed up x axis
% Here's a workaround
xIndexes = x >= 2 & x <= 5; % Find indexes that are in range.
% Plot only those indexes in range, and don't mess with xlim().
plotyy(x(xIndexes),y1(xIndexes),x(xIndexes),y2(xIndexes))
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.1, 1, .5]);

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!