Changing the current axes causes a line that is a child on another axes to disappear.

Hello, I have a plot with two axes objects. On one axes, I use plot to plot an x-y plot. On the other axes, I use the line function to draw a line. When I set the current axes to the first one, the second plot (line) disappears. How do I fix this? BTW, I can get the line back by setting the 2nd axes as the current one. Thanks.

2 Comments

What is
get(gcf, 'Renderer')
showing for you?
Also is the line drawn a 3 dimensional line?
opengl. The line is just x-vs-y, not 3D. Thanks.

Sign in to comment.

 Accepted Answer

I'm moving my comments to a separate thread, since I think the behavior dpb is talking about is a bit more nuanced than your situation.
In your example
close all
hFig = figure;
hAx1 = axes('parent', hFig, 'XAxisLocation', 'top', ...
'YAxisLocation','left','color','none');
hAx2 = axes('parent', hFig, 'XAxisLocation', 'top', ...
'YAxisLocation','right','color','none');
plot(hAx1, 1:10, 1:10);
line(1:10, 0.2 * (1:10).^2, 'parent', hAx2, 'color', 'red');
set(gcf,'color',[1 1 1])
you set the color of the first axis to 'none', but then call the plot command. High-level plotting functions like plot (as opposed to low-level ones like line) make several changes to the axis when the hold state is off (the default unless you've issued a hold on command prior), including erasing all previous plotted objects and resetting the axis properties. So when you call plot, the color of hAx1 is reset to the default white.
So either use line for your first plot:
close all
hFig = figure;
hAx1 = axes('parent', hFig, 'XAxisLocation', 'top', ...
'YAxisLocation','left','color','none');
hAx2 = axes('parent', hFig, 'XAxisLocation', 'top', ...
'YAxisLocation','right','color','none');
line(1:10, 1:10, 'parent', hAx1);
line(1:10, 0.2 * (1:10).^2, 'parent', hAx2, 'color', 'red');
set(gcf,'color',[1 1 1])
Or set the transparent axis color after calling plot (better if you want all the other adjustments plot makes, such as axis limits, etc)
close all
hFig = figure;
hAx1 = axes('parent', hFig, 'XAxisLocation', 'top', ...
'YAxisLocation','left','color','none');
hAx2 = axes('parent', hFig, 'XAxisLocation', 'top', ...
'YAxisLocation','right','color','none');
plot(hAx1, 1:10, 1:10);
hAx1.Color = 'none';
line(1:10, 0.2 * (1:10).^2, 'parent', hAx2, 'color', 'red');
set(gcf,'color',[1 1 1])

3 Comments

Good catch on plot resetting axes 'color', too; I forgot to check that. You can probably get the same place you got by using 'nextplot','add' at the same time create the axes, too.
BUT I still can't reproduce the plotyy behavior regarding access to both lines w/o physically changing the axes focus and using set(gcf,'color','w') has the undesirable side effect of turning the border area outside the plot area white, also...
Thanks all for the answers/comments/help - it works.

Sign in to comment.

More Answers (2)

When you create the second axes, use 'Color','none' to make the first axes also visible. See the example of Using multiple-x-axes-and-y-axes

2 Comments

I have already done so. The problem is as follows: I have the first xy ploy associated with the first axes and the 2nd xy line (made using the line function) associated with the 2nd axes. All is fine at this point. I then try to use a cursor to mark off points on the plot. By default, maybe because the 2nd xy line was created last, I am able to put cursors only on the 2nd line. So, I set the 1st axes as the 'current axes' using axes(firstAxesH) ans I am then able to put cursors on the 1st xy line. However upon execution of the axes command (stepping through), the 2nd xy line becomes invisible - this is the problem. Hope I have explained the scenario adequately. Thanks for your responses so far.
As Walter says, mayhaps it is a bug in the new HG2; which version? Or, post an example that creates the problem so others can 'spearmint and see...

Sign in to comment.

Possibly you are using R2014b or later and the SortMethod axes property might make a difference.
Or it could be a bug.

5 Comments

Matlab (R2015b)
Example:
close all
hFig = figure;
hAx1 = axes('parent', hFig, 'XAxisLocation', 'top', 'YAxisLocation','left');
hAx2 = axes('parent', hFig, 'XAxisLocation', 'top', 'YAxisLocation','right','color','none');
plot(hAx1, 1:10, 1:10);
line(1:10, 0.2 * (1:10).^2, 'parent', hAx2, 'color', 'red');
Now, click on the cursor icon and by default you will be able to put a cursor on the second plot. Then type in the Matlab command line, axes(hAx1) and you will see the second plot disappear and you will be able to put a cursor on the 1st plot. axes(hAx2) will show the 2nd plot and you will be able to put a cursor on it
You've created two axes, the first with a white background and the second with a clear background. When you place the first on top (as occurs when you make it the current axis), the white background hides the line in the lower axis.
If you plan on switching between the two axes, make sure neither have a background color.
OK, that's order dependency and the visibility issue occurs in R2012b as well (I illustrated w/ plotyy to see if there were anything being done internally that helped besides the 'color','none' trick).
There's one difference, however, with R2012b I could place a data cursor on either line w/o having to select the axes first; however, if did make LH axes current then the hiding feature came thru.
You can see both lines with the undesirable effect that the background becomes the default by setting both axes 'color' property to 'none'. You can then fix that with the resulting undesirable effect on the outside frame also changing by
set(gcf,'color',[1 1 1])
I don't know there's a way to independently set the area outside the axes bounding box, though...
All in all, just another of the "warts" in HG that generally doesn't bite too much, but when it does...but maybe a true whizard in HG will know the innards well enough to post a workaround.
Maybe I don't understand your suggestions but the following is what I tried - I still face the same problem.
close all
hFig = figure;
hAx1 = axes('parent', hFig, 'XAxisLocation', 'top',
'YAxisLocation','left','color','none');
hAx2 = axes('parent', hFig, 'XAxisLocation', 'top',
'YAxisLocation','right','color','none');
plot(hAx1, 1:10, 1:10);
line(1:10, 0.2 * (1:10).^2, 'parent', hAx2, 'color', 'red');
set(gcf,'color',[1 1 1])
% I also tried it with set(gcf,'color',[1 1 1]) % immediately after hFig = figure;
I wouldn't go so far as to say they were "suggestions"; more like simply "observations" with another/earlier revision to try to isolate what might have been introduced with HG2 as a bug vs what was earlier behavior.
To be clear, in the following sequence under R2012b--
figure
hAx1 = axes('XAxisLocation', 'top', 'YAxisLocation','left');
plot(hAx1, 1:10, 1:10);
hAx2 = axes('XAxisLocation', 'top', 'YAxisLocation','right','color','none');
line(1:10, 0.2 * (1:10).^2, 'parent', hAx2, 'color', 'red');
axes(hAx1) % here hAx2 is occluded
set(hAx1,'color','none') % both lines appear on gray background
set(gcf,'color','w') % now whole figure bkgd is white both lines show
axes(hAx2) % toggling which axis is gca makes no difference
axes(hAx1) % on visibility
If I use the above I do still have the symptom that can't use the data cursor except on the current axis in focus.
Now, if instead of plot and line on the two axes, I use
figure
hAx=plotyy( 1:10, 1:10,1:10,0.2 * (1:10).^2);
I (a) see both lines on white background, and (b) can use data cursor on each line w/o physically switching back and forth the current axis.
HOWEVER,
axes(hAx(1)) % does cause the RH axis line to be occluded
axes(hAx(2)) % gets it back visible
so there's still something regarding order and all which isn't fully clear as to the details of what's going on internally. Particularly, I can't at this time grok the difference between the two cases for the cursor accessibility; as far as I knew all plotyy did was hide the creation of the 2nd axes for you and align the tick marks to be "pretty" by matching the number of ticks on each y-axis.
It used to be an m-file; now it's a p-file so can't see the innards; mayhaps they've used some other undocumented "tricks" as well, now.

Sign in to comment.

Asked:

on 30 Dec 2015

Commented:

dpb
on 31 Dec 2015

Community Treasure Hunt

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

Start Hunting!