MATLAB: 3 y-axis on subplot

MATLABmutliple y-axis

Hello,
I have data from 24 islands in the Pacific region and my goal is to subplot 3 parameters per island. I found some useful functions on FileExchange (yyy-axis notably) but it doesn't scale right when I subplot all my islands.
Thanks 🙂 !

Best Answer

The easiest solution for you will probably be to use the function from FileExchange and fix the scaling. Nevertheless, here is a code I stitched together for plotting with three actual y-axes. I had some trouble exporting the results, but made it work with print to pdf. Let me know if you find any bugs. Make sure to set the upper XLim about 20% higher than the extent of your actual data.
fig1=figure(1)
%%Create 3 axes
Ax1=axes;
Ax2=copyobj(Ax1,fig1);
Ax3=copyobj(Ax1,fig1);
%%Set axes colors
AxColors={[0 0 0],[1 0 0],[0 0 1]};
%%Set XLim here
xmax=8; %set ~20% higher than max xdata
xmin=0;
%%Plot whatever here
axes(Ax1);hold on
l1=ylabel('YLabel')
xlabel('XLabel')
x1=0:.1:2*pi;
y1=cos(x1);
h1=plot(x1,y1);
set(h1,'color',AxColors{1})
box off
drawnow
%%Second yaxis
axes(Ax2);hold on
l2=ylabel('YLabel')
h2=plot([0 5], [0 15]);
set(h2,'color',AxColors{2})
drawnow
%%Third yaxis
axes(Ax3);hold on
l3=ylabel('YLabel')
x3=0:.1:2*pi;
y3=3*sin(x3);
h3=plot(x3,y3);
set(h3,'color',AxColors{3})
drawnow
%%Fix some visuals
set([Ax1 Ax2 Ax3],'xlim',[xmin xmax])
set([Ax2 Ax3],'XColor','none');
set([Ax1 Ax2 Ax3],'Color','none');
XTicks=Ax1.XTick;
XTicks(XTicks>0.85*xmax)=[];
%%Set YAxis colors
set(Ax1,'YColor',AxColors{1});
set(Ax2,'YColor',AxColors{2});
set(Ax3,'YColor',AxColors{3});
%%Fix XTicks and XRuler
Ax1.XTick=XTicks;
Ax2.YRuler.FirstCrossoverValue = 1*xmax;
Ax3.YRuler.FirstCrossoverValue = .85*xmax;
drawnow
YMinAx1=get(Ax1,'YLim')
Ax1.XRuler.Axle.VertexData=single([0 .85*xmax;YMinAx1(1) YMinAx1(1);0 0]);
drawnow