Simple Multiple Y axes with Scale, Pan, and Zoom Features
2 views (last 30 days)
Show older comments
Plotting multiple signals on a line graph with separately scalable y-axes is very useful when analyzing data from data sets containing more than 2 signals (I've tried using PLOTYY or other available tools on file exchange but I wasn't fully satisfied with them so I decided to make my own)
Here is a rough script that will plot 8 scalable lines. I used the LINKAXES command to acquire a common x axes. However this command shifts most of the lines outside the white figure box and into the surrounding grey background. Is there a quick fix to my script that will scale/cut everything to the white figure box?
clear
close all
%%Dummy data
x1 = 0:0.01:1;
x2 = x1;
x3 = x1;
x4 = x1;
x5 = x1;
x6 = x1;
x7 = x1;
x8 = x1;
y1 = x1;
y2 = x2.^2;
y3 = x3.^3;
y4 = sin(x4);
y5 = cos(x5);
y6 = -cos(x6);
y7 = -sin(x7);
y8 = x8.^4;
ylabels{1}='A'; ylabels{2}='B'; ylabels{3}='C'; ylabels{4}='D'; ylabels{5}='E'; ylabels{6}='F'; ylabels{7}='G'; ylabels{8}='H';
Create figure window
fh=figure('units','normalized',...
'DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on');
cfig = get(fh,'Color');
pos = [0.190 0.1 0.56 0.8]; %Axes Position [*left bottom *width height]
offset = pos(3)/15.5; %originally = /5.5
%Plot the first two lines
ah(1) = axes('Parent',fh,'XColor','k','YColor','b');
lh1 = line(x1,y1,'Color','b','Parent',ah(1));
lineColors = get(ah(1),'ColorOrder');
ah(2) = axes('Parent',fh);
lh2 = line(x2,y2,'Color','k','Parent',ah(2));
set(ah(2),'YAxisLocation','right','Color','none');
%Reduce width of the two axes generated by plotyy
pos(1) = pos(1) + offset;
pos(3) = pos(3) - offset/2;
set(ah,'position',pos);
%Determine the position of the rest of axes (spacing between
%y-axis(offset*#))
pos4=[pos(1) pos(2) pos(3)+offset pos(4)];
pos3=[pos(1) - offset pos(2) pos(3)+offset pos(4)];
pos6=[pos(1) pos(2) pos(3)+offset*2 pos(4)];
pos5=[pos(1) - offset*2 pos(2) pos(3)+offset*2 pos(4)];
pos8=[pos(1) pos(2) pos(3)+offset*3.2 pos(4)];
pos7=[pos(1) - offset*3.2 pos(2) pos(3)+offset*3.2 pos(4)];
%Determine the proper x-limits for the third and fourth axes
scale3 = pos3(3)/pos(3);
scale4 = pos4(3)/pos(3);
scale5 = pos5(3)/pos(3);
scale6 = pos6(3)/pos(3);
scale7 = pos7(3)/pos(3);
scale8 = pos8(3)/pos(3);
limx1 = get(ah(1),'xlim');
limx4 = [limx1(1) limx1(1)+scale4*(limx1(2)-limx1(1))];
limx3 = [limx1(2)-scale3*(limx1(2)-limx1(1)) limx1(2)];
limx6 = [limx1(1) limx1(1)+scale6*(limx1(2)-limx1(1))];
limx5 = [limx1(2)-scale5*(limx1(2)-limx1(1)) limx1(2)];
limx8 = [limx1(1) limx1(1)+scale8*(limx1(2)-limx1(1))];
limx7 = [limx1(2)-scale7*(limx1(2)-limx1(1)) limx1(2)];
%Create ax(3) & ax(4)
ah(3)=axes('Position',pos3,'box','off',...
'Color','none','XColor','k','YColor','g',...
'xtick',[],'xlim',limx3,'yaxislocation','left');
ah(4) = axes('Position',pos4,'box','off',...
'Color','none','XColor',cfig,'YColor','r',...
'xtick',[],'xlim',limx4,'yaxislocation','right');
ah(5)=axes('Position',pos5,'box','off',...
'Color','none','XColor','k','YColor',[1,0,.5],...
'xtick',[],'xlim',limx5,'yaxislocation','left');
ah(6)=axes('Position',pos6,'box','off',...
'Color','none','XColor','k','YColor',[0,0.5,0],...
'xtick',[],'xlim',limx6,'yaxislocation','right');
ah(7)=axes('Position',pos7,'box','off',...
'Color','none','XColor','k','YColor',[0.5,0.1,0],...
'xtick',[],'xlim',limx7,'yaxislocation','left');
ah(8)=axes('Position',pos8,'box','off',...
'Color','none','XColor','k','YColor',[0,0.7,0.7],...
'xtick',[],'xlim',limx8,'yaxislocation','right');
linkaxes([ah(8) ah(7) ah(6) ah(5) ah(4) ah(3) ah(2) ah(1)],'x'); % Link to have common X axes.
lh3 = line(x3,y3,'Color','g','Parent',ah(3));
limy3=get(ah(3),'YLim');
lh4 = line(x4,y4,'Color','r','Parent',ah(4));
limy4=get(ah(4),'YLim');
lh5 = line(x5,y5,'Color',[1,0,.5],'Parent',ah(5));
limy5=get(ah(5),'YLim');
lh6 = line(x6,y6,'Color',[0,0.5,0],'Parent',ah(6));
limy6=get(ah(6),'YLim');
lh7 = line(x7,y7,'Color',[0.5,0.1,0],'Parent',ah(7));
limy7=get(ah(7),'YLim');
lh8 = line(x8,y8,'Color',[0,0.7,0.7],'Parent',ah(8));
limy8=get(ah(8),'YLim');
%Label all y-axes
set(get(ah(1),'ylabel'),'string',ylabels{1})
set(get(ah(2),'ylabel'),'string',ylabels{2})
set(get(ah(3),'ylabel'),'string',ylabels{3})
set(get(ah(4),'ylabel'),'string',ylabels{4})
set(get(ah(5),'ylabel'),'string',ylabels{5})
set(get(ah(6),'ylabel'),'string',ylabels{6})
set(get(ah(7),'ylabel'),'string',ylabels{7})
set(get(ah(8),'ylabel'),'string',ylabels{8})
0 Comments
Answers (0)
See Also
Categories
Find more on Combine Multiple Plots 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!