Somehow the 2nd X-Axis is not what it seems

조회 수: 3 (최근 30일)
Harry MacDowel
Harry MacDowel 2011년 10월 1일
I am plotting a 2nd y-axis to my existing codes without any data on it so I am actually 'converting' from one to the other, as below:-
ylimit = [0,35];
ytic = [0 5 10 15 20 25 30 35];
plot(x,y);
ax1 = gca;
set(ax1,'ylim',ylimit,'ytick',ytic);
ax2 = axes('Position',get(ax1,'Position'),'YAxisLocation','right','Color','none','YColor','blue','YMinorTick','on');
set(ax2,'ylim',tan(degtorad(ylimit)),'ytick',tan(degtorad(ytic)),'xtick',get(ax1,'xtick'),'xticklabel',[]);
Everything looks good when it is plotted. BUT, when I am enlarging the figure, it seems like the 2nd y-axis is actually not aligning its position of x=0 with ax1, with a black line distinguishing it. I printed the graph using 'print' function and still I am seeing the same thing.
I tried a lot of other ways but still couldn't get it.
I would appreciate your help a lot! Thanks!
  댓글 수: 2
Jan
Jan 2011년 10월 1일
Please add a definition for x and y, such that we can run your code. Without this, I cannot understand the description of the error.
Harry MacDowel
Harry MacDowel 2011년 10월 1일
Sorry for the lack of information. I am doing a semilogx plot, then on top of the existing y axis I plot another y-axis on the right side.
Actually I solved the problem. Apparently by first setting the position of the figure plot as large as possible, then the second y-axis would not have the slight change when it is enlarged (a little, since I am using as big as the screen figure size) and shrunk.
Funny little bug.

댓글을 달려면 로그인하십시오.

답변 (3개)

Harry MacDowel
Harry MacDowel 2011년 10월 1일
The following code before the figure is plotted solve the apparent bug:-
set(figurehnd,'position',[1 47 1440 786]);
which is almost as large as the size of my screen.
Does anyone have any other approach or an explanation whether this is a bug of Matlab?
I leave this question open still.
  댓글 수: 1
Harry MacDowel
Harry MacDowel 2011년 10월 1일
AHA! I tried shrinking it to a quarter of its size then this time the x-axis of the 2nd y-axis appear to be underneath the existing one instead of over it, as stated in my question.
In fact I often wonder why Matlab does not provide an in-house function which allows users to define a 2nd y-axis on the right side of the plot easily.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 10월 2일
Looks like a case for plotyy() or linkaxes()
  댓글 수: 1
Harry MacDowel
Harry MacDowel 2011년 10월 2일
Tried them both and they just mess up the plot.

댓글을 달려면 로그인하십시오.


Harry MacDowel
Harry MacDowel 2011년 10월 2일
Anyway I tried several more methods including those suggested by Walter.
My conclusion of solving this problem is as follows:-
1) If one were to provide a 2nd y-axis which has exactly the ytick position and just different values (a conversion factor applied, for example the 1st y-axis is meter and the 2nd y-axis is feet), then the following code shall do:-
ylimit = [0,35];
ytic = [0 5 10 15 20 25 30 35];
y2str = num2str(tan(degtorad(ytic')));
ax1 = gca;
set(ax1,'ylim',ylimit,'ytick',ytic);
ax2 = axes('Position',get(ax1,'Position'),'YAxisLocation','right','Color','none','YColor','blue','YMinorTick','on');
set(ax2,'ylim',ylimit,'ytick',get(ax1,'ytick'),'xtick',get(ax1,'xtick'),'xticklabel',[],'yticklabel',y2str);
set(ax2,'YMinorTick','on');
The key is to replace the strings in second y-axis. In order to avoid the somehow-will-displace-a-little-upon-maximizing-or-minimizing-the-figure-window problem, just add:
set(figurehnd,'position',[1 47 1440 786]);
where the last 2 values are both the width of my screen and the height of the figure, measured from the upper corner of my taskbar to the top.
2) If you wish to do another scale, just follow my previous answer.

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by