plot any number of y axis with any number of data sets, note I have included a copy of linkzoom.m
Updated 22 Jul 2014

View License

% jzplotys - Inspired by multiplotyyy (from matlab central) but uses a
% different approach. In this code all the axis are the same
% size that contain data and dummy axis are used to make the
% extra Y axis. This was done so that the linkzoom.m function
% from matlab function could be used in parallel with this plot
% function (when the x - axis sizes are different as in the
% multiplotyyy, it skews the x data and when zooming the data is
% no longer aligned on the x - axis.
% Syntax: [ax,hlines,fh] = jzplotys(xy_pairs,ax_groups,xrange)
% Inputs:
% xy_pairs = {x1,y1,x2,y2,x3,y3,...} cell array with all xdata and ydata
% ax_groups = [nax1 nax2 nax3 ...] vector indicating the number of xy
% pairs to plot on each axes, where the number of axis will be
% equal to the length of the vector. The xy pairs will be taken in
% order so the 1st nax1 pairs will be plotted on the 1rst axis,
% the next nax2 pairs will be plotted on the 2nd axes and so on.
% The length of ax_group must equal the number of xy pairs
% xrange = [xmin xmax] xlimits of plot
% leg_space = [pixels] space to make available on right side of plot for
% legned (should be around 100 pixels)
% Outputs: ax = double array containing the axes' handles, note that
% ax(1) will be the handle for the first axes and data
% ax(2) handle of second Y axis data sets
% ax(3) handle of dummy axes to display Yticks for ax(2)
% ax(4) handle of third Y axis data sets
% ax(5) handle of dummy axes to display Yticks for ax(4)
% etc.
% hlines = cell array containing the line handles (alternates
% like ax with emty handles for dummy axis
% fh = figure handle
% Example:
% x1 = (0:0.01:1)';
% x2 = (0:0.1:1)';
% x3 = (0:0.05:1)';
% y1 = x1;
% y2 = x2.^2;
% y3 = x3.^3;
% y4 = sin(x1);
% y5 = fliplr(2*x1.^2);
% y6 = 7*cos(x1);
% y7 = 7*log(x1+1.2);
% [ax,hlines,fh] = jzplotys({x1,y1,x2,y2,x3,y3,x1,y4,x1,y5,x1,y6,x1,y7},[2 1 2 2],[.25 .75],100);
% legend(cat(1,hlines{:}),'a','b','c','d','e','f','g','location',[.88 .5 .01 .01])
% ylabel(ax(1),'Y1');
% ylabel(ax(3),'Y2');
% ylabel(ax(5),'Y3');
% ylabel(ax(7),'Y4');
% Inspired by multiplotyyy.m (available at www.matlabcentral.com) by :
% Laura L. Proctor
% Author: Josh Zimmerman
% May 21, 2014

Cite As

Josh Zimmerman (2024). jzplotys.zip (https://www.mathworks.com/matlabcentral/fileexchange/46688-jzplotys-zip), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Two y-axis in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

I just added a screenshot to show what the final plot looks like.