조회 수: 3(최근 30일)

dpb
15 Aug 2020

편집: dpb
15 Aug 2020

The two blue and the two red axes have the same scale; there's no point in creating the extra ones; just plot both blue and both red lines when the respective axis is created.

You can do that by using a 2-column Y array for each or by "hold on" before adding the second line for each.

Once you created the axes and added the line; the line is a child of that axes and so if you remove that axes, you remove the line. You could make those two axes invisible so they wouldn't show, of course, but they would still be there and take up room for no purpose.

IOW, instead of removing something, don't create something unwanted/unneeded in the first place.

dpb
16 Aug 2020

"I tryied the rescaling idea that Rik commented above..."

That's the kludgy way -- the proper way is to put the plotted lines on their proper axis as illustrated below.

ANSWERS doesn't have a way to acknowledge Comments; most longtime participants as Rik are satisfied with the feedback their contribution was noted...

dpb
16 Aug 2020

dpb
16 Aug 2020

편집: dpb
16 Aug 2020

I just realized about the three axes, not just two and the use of the FEX submission -- I don't have it and don't want to mess with downloading, but to illustrate how to get the two values at the same scale on the proper axis with the first two using builtin MATLAB abilities --

% make some fake data that crudely approximates...

pos=[0 40 75 80 120 145];

t=linspace(0,30,6);

bp=polyfit(t,pos,2);

th=linespace(0,30,20);

phat=polyval(bp,th);

vel=[11 diff(pos)/5];

bv=polyfit(t,vel,2);

vhat=polyval(bv,th);

% build plot

hAx=axes; % create the base axes

hLL(1)=plot(t,pos,'k-','LineWidth',2.5); % plot the position data

hold on % add the fitted on

hLL(2)=plot(th,phat,'k--','LineWidth',2.5); % top of existing first axes

grid on % and annotate this one

ylabel('Position (m)') % while we're here and gca

yyaxis right % add the RH velocity axis

hRL(1)=plot(t,vel,'r-','LineWidth',2.5); % and plot the observed speed

hold on % again, add on top of gca

hRL(2)=plot(th,vhat,'r--','LineWidth',2.5);

ylabel('Speed (m/s)')

hRAx=gca; % get the gca (RH) axis handle

hRAx.YColor='r'; % to set its color

The above yields

where both red curves are on the RH axes and scaled per its scale. This happened because both were drawn while that axis was the current axis of focus.

I don't know precisely the interface for the addaxis function, but it will do the same -- subsequent plot commands will plot into gca by default so if you plot the observed data in sequence and then try to add the fits, you'll be changing focus and thereby creating the issues you're having.

The fix is one of two possibilities:

- Add all data to each axis in turn as it is created and remains current as shown above, or
- Save and use the axis handle as the target axis for the subsequent plot commands so they're added to the intended axis.

Either of those will, as suggested before, solve your problem.

dpb
17 Aug 2020

Glad to try to help...we in general all like to try to teach and the content of questions can add some interesting tidbits of ways to use MATLAB so it's entertainment for us as well... :)

As noted, if you would just post the sequence of commands you used, I'm certain somebody could point out the "why" you get what you are getting in the order in which you did things...remember that unless you specifically direct plot() to add to a given axis by passing the handle, it will plot() on to which ever axis is that returned by gca -- and that changes to be the last one created or can be modified by user intervention. So, the order of operations is important

Opportunities for recent engineering grads.

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

Start Hunting!
## 댓글 수: 3

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/580071-how-to-delete-remove-an-extra-yaxis-inserted-by-addaxis-function-without-removing-the-line-that-w#comment_974049

⋮## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/580071-how-to-delete-remove-an-extra-yaxis-inserted-by-addaxis-function-without-removing-the-line-that-w#comment_974049

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/580071-how-to-delete-remove-an-extra-yaxis-inserted-by-addaxis-function-without-removing-the-line-that-w#comment_974268

⋮## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/580071-how-to-delete-remove-an-extra-yaxis-inserted-by-addaxis-function-without-removing-the-line-that-w#comment_974268

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/580071-how-to-delete-remove-an-extra-yaxis-inserted-by-addaxis-function-without-removing-the-line-that-w#comment_974661

⋮## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/580071-how-to-delete-remove-an-extra-yaxis-inserted-by-addaxis-function-without-removing-the-line-that-w#comment_974661

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