How to delete/remove an extra yaxis (inserted by addaxis function), without removing the line that was created with the addaxis function.

6 views (last 30 days)
Hello. Sorry to bother you, but i think i have tried everything by now.. I have the following figure, in which the red and blue y-axis were created with the addaxis function (https://www.mathworks.com/matlabcentral/fileexchange/9016-addaxis). Due to the fact that i have same x- axis -intervals but different y-axis- intervals for the lines ploted (the 2 red lines have the same y intervals, the 2 blue lines have the same, the 2 black have the same) i used the addaxis function in order to be able to create this figure. But there are 2 extra y-axis, the one red and the one blue with no label, that i want gone, but with keeping the corresponding lines to those y-axis. Is there a possible way to do that, or another approach in order to create such a figure? Thank you very much in advance for your time!! Stay safe!!
  3 Comments
Vasilis V
Vasilis V on 16 Aug 2020
Hello Rik. Thank you for your answer!! That's what i am trying to do yeah but i don't seem to know how.. Do you have any ideas? Thanks again for your time!!!
Vasilis V
Vasilis V on 16 Aug 2020
Rik you were right, i worked like a charm!! Thank you so much for the idea!! Is there a way to accept your comment as an answer, because i couldnt found it. Thanks again!!!! Be safe!!

Sign in to comment.

Accepted Answer

dpb
dpb on 15 Aug 2020
Edited: dpb on 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.
  5 Comments
dpb
dpb on 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
dpb on 16 Aug 2020
BTW, if you're still having to do the scaling to make it work, attach the code that creates the axes and adds the plot; almost certainly by looking at the order in which things are created we can tell where you went wrong...and fixing it will also almost certainly be nearly trivial change(s).

Sign in to comment.

More Answers (1)

dpb
dpb on 16 Aug 2020
Edited: dpb on 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:
  1. Add all data to each axis in turn as it is created and remains current as shown above, or
  2. 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.
  5 Comments
Vasilis V
Vasilis V on 17 Aug 2020
The community here is really really lucky to have people like you!! Thanks for everything. I know your time is very valuable, and thank you for that!!
dpb
dpb on 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

Sign in to comment.

Categories

Find more on MATLAB 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!