3 y-axis on subplot

3 views (last 30 days)
Isma Di Carlo
Isma Di Carlo on 24 Jul 2018
Answered: Tobias Koenig on 4 Oct 2020
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 :) !
  1 Comment
jonas
jonas on 25 Jul 2018
I attempted to create a figure with triple y-axis by using some undocumented features but ran into trouble exporting the results. I opened a new question to deal with that here . You may find the code useful anyway.

Sign in to comment.

Accepted Answer

jonas
jonas on 25 Jul 2018
Edited: jonas on 25 Jul 2018
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
  3 Comments
jonas
jonas on 25 Jul 2018
Edited: jonas on 25 Jul 2018
Hehe yea I know. I made a function out of it, see attachment. You can call it with:
plotyyy(x1,y1,x2,y2,x3,y3,xmin,xmax)
I also fixed a bug.
Surely you will find that exporting is difficult. However, I made it work with print command and pdf format.
Rigel Gómez Acata
Rigel Gómez Acata on 16 Sep 2020
Thanks jonas! Nice code :)

Sign in to comment.

More Answers (1)

Tobias Koenig
Tobias Koenig on 4 Oct 2020
Thanks jonas! This code helped me very mutch! I have stil some questions, maybe you can help me?.
1st. question:
I tried to add a legend for the threee plotted functions. This doesn't work. I merely can add one name in the legend.
2st. question:
the label of the blue axis is drawn on top of the axis. is it possible to draw the label as such as the other axis? (like in the middle of the axis and rotated 90 degrees?
do you have any idea to fix this problems?
Thanks :)

Products

Community Treasure Hunt

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

Start Hunting!