Top x-axis disappeared in plotyy
Show older comments
I am using plotyy. It worked nicely until I added a title to the plot. Now the top x-axis has disappeared. What to do in order to make it visible?
15 Comments
dpb
on 30 Jan 2016
Post an example of how you created the plot with the symptom...don't recall it ever failing w/ that symptom so no klew otomh, sorry...
If it's a complex plot, try going back to the beginning and adding features and see when the issue appears (presuming it's reproducible).
perfectionist
on 31 Jan 2016
perfectionist
on 31 Jan 2016
perfectionist
on 31 Jan 2016
Again, we need to SEE what you have actually done, not just have it described.
plotyy creates two axes, there is no such thing as "two ordinates but only one abscissa". By default the two x-axes overlay each other visually so there may be the appearance of only one, but the axes are independent and complete axis entities; the only default difference is that the second axis has its ordinate labels on the right rather than the left.
I presume you've modified to place one of the x-axes at the top but that's only a presumption. It's quite easy with multiple commands onto a given figure to inadvertently place a component on top of another which hides its visibility or to address the wrong axes handle inadvertently. But, it's impossible to diagnose a problem without seeing just what it was that was done to create the problem, sorry.
ADDENDUM
Regarding the previous comment where you mention the difference between two example plots -- in the doc here, those are
plotyy(t,y,t,y,'plot','stem')
plotyy(t,z1,t,z2,'semilogy','plot');
and the difference when execute those two types here is that for the latter one gets
>> get(gca,'box')
ans =
off
whereas for the former it is
>> get(gca,'box')
ans =
on
WHY TMW chose to do it that way w/ semilogy as one of the function handles to plotyy I don't know; it isn't a fignewton of semilogy itself; perhaps it is an inadvertent issue (aka a bug); perhaps there is some logic behind it but it escapes me at the moment what that might be, granted. Anyway, adding box on to the axes may well solve your issue.
perfectionist
on 31 Jan 2016
W/o data can't execute yours, but I don't see anything terribly untoward there. I duplicate of the basics of
x=1:10; y1=x.^2; y2=x*2;
plot(x,y1,'ro','markersize',6)
hold on
plot(x,y2,'bo','markersize',2)
hAx=plotyy(x,y1,x,y2)
Author='Anon'; TK=273.15;
set(gca,'Title',text('String',[char(Author),' ',num2str(TK),' K']));
I am, however, limited to R2012b; you have HG2 as can tell with the usage of the axis methods instead of set() but can't figure the ticks have much to do with anything altho again, you can leave as default to see if makes any difference.
As a general note, I'd probably do the above in the opposite order; that is, create the two axes initially by calling plotyy first, then plot into those specific axes handles...
hAx=plotyy(xextrap,yextrap,xextrap,tion);
set(hAx,'nextplot','add') % "HOLD ON" for multiple axes
plot(hAx(1),lgaXxsi,yepsilon,'ro','markersize',6)
plot(hAx(2),lgaXxsi,ynorm, 'bo','markersize',2)
...
Then whatever cleanup, additions one needs subsequently should be less complex I'd think as the two axes limits should stay in synch as one presumes the two datasets are of approximately same magnitudes.
perfectionist
on 31 Jan 2016
Sorry, I typed in comment box instead of pasting from command window; the 'NextPlot' property is indeed, 'add' to emulate the HOLD ON effect. It's done this way since have two axes and HOLD won't accept a vector of axes handles. It, i.e.,
set(hAx,'Nextplot','add')
is the same thing as executing
hold(hAx(1),'on')
hold(hAx(2),'on')
I just auto-rote typed 'on' for the hold form instead of 'add' for set. Shouldn't have any issue with
set(hAx,'nextplot','add')
If that continues, post the command used and the full, complete text of the indicated error.
As for the understanding, read the details under
doc hold
ADDENDUM
What do you get if you do something as trivial as the above? Does it still cause the issue? If not, that's also informative of where the issue must lie; it'd be in what's in your other case vis a vis the simpler.
Then, of course, the steps would be to add the additional details and see if the gremlins reappear.
perfectionist
on 1 Feb 2016
dpb
on 1 Feb 2016
OK, paste the example and attach the resulting plot illustrating the problem. To do the latter, "Save As" .jpg to a file and attach with the "Image" icon.
OH, just a thought; just for grins try changing the 'renderer' property to see if by any chance it's a figment of a graphics driver. Not likely, but always worth a try for such odd symptoms.
dpb
on 1 Feb 2016
"2. The ticks of the left ordinate do additionally occur on the right ordinate (as before, when the top x-axis did occur)"
This is normal and unavoidable if the two axes (left and right) don't have the same number of tick marks; as noted above, plotyy creates two totally independent axes, they're just overlaid on top of each other.
If shown, tick marks are shown on both the left and right (or top and bottom) axis lines and there's not any property that control these two independently; they're either on/off. By default, plotyy will create the same number of tick marks on each axis, this will cause the two to overlay each other. If either is changed, however, then the ranges and number may change and then the two axes appear and tick marks aren't coincident. Unfortunately, TMW has never bothered to add the details internally to HG axes objects to allow for this level of granularity of control.
Just guessing, again, since you've resolutely failed to provide enough information despite all the prodding that we can reproduce your plot to see the symptoms, the presumption I have at the moment is that the hold on an addition of additional data has caused the ranges to change but I still can't see your terminal from here to tell precisely what you've done, sorry...
perfectionist
on 1 Feb 2016
You still didn't give enough data to be able to easily recreate anything that matches -- don't know what the ranges are so can't do anything meaningful in trying to match what is causing your issues.
I do suspect it has to do with the previous comment but until can duplicate the symptom can't really provide a solution.
At a minimum, the ranges of each of the vectors you're plotting would suffice; simplest would be to have a complete sample can just run directly using (say) some random values over those ranges. Also, what are the bare minimum lines required before the symptom appears the x|ylabel I presume make no difference? Let's pare it down to the essence, iow.
Also, you didn't hit the "Attach" button in lower RH corner apparently; your figures didn't show up.
I had a much longer reply including trying some to try to guess-plicate the results but we're rural and have had a series of power outages this AM and it got me last time so I gave up.
I'm willing to help but give me some help here... :)
ADDENDUM
Besides the x ranges being same, it would likely help if the y ranges are consonant with your data as well. As said before, I'm suspecting it's tied in with the use of hold on as well as the changing of the axis limits; seeing it operate on those same limits is, perhaps, key in both reproducing and solving the problem. At least, I'm not willing to spend time trying without the detail I think needed to have a chance of success, particularly when it's would be so trivial to do so...
ADDENDUM 2
BTW,
xup=round(max(x));
will always be < max(x) unless max(x) is integer-valued or the fractional part is >0.5. So when your dx is 0, your x-limits won't cover the full data set. You're probably looking for
xup=ceil(max(x));
for positive values of x.
Also, you don't define the ylim variables you computed(?) I suppose also somewhat similar to the x-axis ones.
Again, as I've suggested a number of times, create a small, reproducible in its entirety w/o relying on something else we don't have example and paste it in.
Shouldn't take more than 10 lines or so...
ADDENDUM 3 :)
BTW, if you do post an example case and don't hear back for a while, "not to worry!". My next couple of days are pretty much spoke for so won't be having much time until later in the week...I'll try to catch a moment to check and if it seems easy-peasy maybe, but if it is going to take some digging, it'll likely have to wait...
perfectionist
on 2 Feb 2016
Accepted Answer
More Answers (2)
The subsequent answer buried at the end of the long previous discussion is, I think, so significant I'll repost it here alone--
The real solution just came to me--
set(ax('box','off') % turn box off both axes for sure...
set(ax(2),'xaxislocation','top', ...
'xticklabel',[]) % move RH x axis, clear tick labels
and you can "have your cake and eat it, too!" :)
This will put the RH axis at the top and have the same net effect on the top tick marks as does 'box','on' but without the side effect of turning on the other ytick. Setting the tick labels to null string keeps them from showing up; if leave on you'll need to make that fixup before using title so it is written above them, otherwise they'll clash for room.
With this, I created a figure with two subplots, one with and one without the upper labels...

In the close spacing of the subplot the upper title still interferes with the labels altho it didn't as a single plot if added after the setting of the xaxis position property.
Also with subplot I had to set 'box','off' explicitly on LH axis whereas for the full-size plot the internal logic left both 'off'.
I dropped the legend for lack of room and of no real interest to the question at hand, anyway...
8 Comments
perfectionist
on 3 Feb 2016
Just removing
set(ax,'box','on')
isn't enough.
If you
set(ax,'box','off')
the double y-ticks will disappear, I'm certain.
As noted, there's a case where the default result is with one 'on', one 'off' which will leave the double y-ticks visible for the axis which is 'on' (typically what I've observed here is LH 'off', RH 'on'. With this solution as opposed to the previous of using 'on' to force the x-ticks but living with the y-tick being visible, you must ensure both are 'off'.
I agree with your assessment of HG; as compared to several other alternatives it remains klunky at best and is quite frustrating at times.
As said, at this point having retired from technical consulting after returning to family farm it's cheap entertainment for me...I'm easily amused! :) OTOH, when trying to solve a problem for a real application or client, it can be a royal pit[proverbial]a[ppendage].
The appearance dependent on the parameter has to do with how the intervals are set and what the triggers in the number and spacing of the ticks and which condition ends up (of the four possible) with respect to 'box' state.
To see this, for diagnostics put in
get(ax,'box')
after the plotyy and before any set call to see the state as left.
ADDENDUM
I just ran the specific test here between the two values of the x-axis extension factor and do see the same effect with default behavior for 'box' as you describe. The state at that point is
>> get(ax,'box')
ans =
'on'
'off'
>>
which is the LH axis box is 'on' so its ticks will show on the RH axis as well as the base ticks for the second (RH) axis. Since the second axis box is 'off', its opposite axis ticks won't show so the LH axis looks normal.
At this point if one executes
set(ax(1),'box','off')
the extra ticks on the RH side go away but the top x-axis disappears because now NEITHER axis box is 'on' so the opposite (now top) x axis ticks aren't shown for either.
I recommend using
set(ax,'box','off')
however, to ensure they're both off just in case HG were to ever return the other state of either both on or RH on and LH off
Now, if one subsequently moves the x-axis for one or the other axis to the 'top' position, then you have the desired result. The end sequence is then
plot(lgaXxsi,yepsilon,'ro','markersize',6);
hold on;
plot(lgaXxsi,ynorm,'bo','markersize',2);
ax=plotyy(xextrap,yextrap,xextrap,tion);
set(ax,'box','off') % turn both boxes off for sure...
axis(ax(1),[xlimitA xlimitZ ylimitAA ylimitZZ]);
set(ax(1),'XTick',[-40.0:2.0:40.0])
set(ax(1),'YTick',[-2:0.2:2])
set(ax(2),'xaxisloc','top','xticklabel',[])
axis(ax(2),[xlimitA xlimitZ 0.0 1.0]);
set(ax(2),'YTick',[0:0.2:1])
xlabel(ax(1),'lg a(X)');
ylabel(ax(1),'epsilon [mV/K]');
ylabel(ax(2),'t(ion)')
title(ax(2),[char(Author),' ',num2str(TK),' K']);
I rearranged a little to basically do what needs to be done on each axis together as a group with the plotting and limits, etc., done first then follow with the labels and titles, etc. It doesn't really matter, just seems a little more organized to me in that order.
The key here to get the effect you're looking for are the two lines that
- Turns the 'box' off for both axes for certain, and
- Moves the second axis x-axis to the top
Within the latter I've shown eliminating the labels for it; if want them then just remove the 'xticklabel' parameter/value pair.
NB: the 2nd axis handle in text -- this is the key to making sure the title doesn't overlap the x-axis labels if they are displayed; by default gca is LH axis and so is written to it which has the x-axis at bottom so doesn't know to leave room. I'd forgotten to include the handle earlier.
perfectionist
on 4 Feb 2016
Peculiar, indeed.
That would seem to indicate a bug in HG2, then. Just out of curiosity, what do
get(ax,'box') % and
get(ax,'xaxislocation')
return?
It's also somewhat puzzling what brackets would need removing (in fact, which could be removed without resulting in a syntax error). Which line(s) would that have been and what was the end line in place?
What's the result of
>> ax=plotyy(xextrap,yextrap,xextrap,tion);
>> get(ax,'box')
ans =
'off'
'off'
>> get(ax,'xaxisloc')
ans =
'bottom'
'bottom'
>> set(ax(2),'xaxisloc','top')
from the command line? Above is here and the resulting plot is as expected/desired...if the result of
>> get(ax,'box')
includes an 'on' response, then set it 'off' and see if that causes the x-axis to disappear, too. That would definitely be a bug and should be reported to TMW. If there were a 'box', 'on' result, that would be the case where also there would be a duplicate set of y-axis ticks showing, of course, as well.
ADDENDUM
Oh, just one more thought came to me -- I wonder it it's a layering issue where one axis is overlaying the other and doesn't let it show through and that's different between HG and HG2 for some reason. See what happens if move the x-axis for ax(1) to 'top' instead of ax(2). Either way, if the above simple test show up as you describe still, it's an issue TMW needs to fix; that should not be. And, if it does, it should, it seems, also be so for random x,y data irrespective of anything else.
perfectionist
on 4 Feb 2016
Ah, that's just an editor suggestion; didn't think about tick vectors as I don't have the methods here so I had to convert those to set. OK, that's understood.
OK, I understand the sequence to the point "the top x-axis really disappears." That's what should happen at that point.
If you at that point
set(ax(2),'xaxisloc','top')
does the top x-axis not then appear?
If, at that point it isn't visible I'm pretty much w/o being able to put my hands on the actual system convinced it is a bug.
"With set(ax(1 ),'xaxisloc','top'); the bottom axis disappears. Yes, I think it's an overlay issue."
This was my last idea; swap the two such that
get(ax,'box') % and
get(ax,'xaxislocation')
(will hopefully) return
'off'
'off'
'top'
'bottom'
instead of what I'd been attempting which was the last two items switched. But, either way, need to make sure your end result is with one top, one bottom or there's no chance.
If at that point it still won't show both x-axes I think it should be packaged and submitted as bug to TMW at the official support site www.mathworks.com from all I can tell from here lacking HG2.
It does work as expected here w/ R2012b which is still original HG, not HG2.
Sorry I don't have any better news; it should work I'm convinced.
dpb
on 4 Feb 2016
Just as sanity check I redid the test here from command line...
>> ax1=plotyy(xextrap,yextrap,xextrap,tion);
>> get(ax1,'xaxisloc')
ans =
'bottom'
'bottom'
>> set(ax1(2),'xaxisloc','top')
>> get(ax1,'xaxisloc')
ans =
'bottom'
'top'
>> set(ax1,{'xaxisloc'},{'top';'bottom'})
>> get(ax1,'xaxisloc')
ans =
'top'
'bottom'
>> set(ax1,{'xaxisloc'},{'bottom';'top'})
>> get(ax1,'xaxisloc')
ans =
'bottom'
'top'
>>
Swapping back and forth makes no difference, both are viewable either way and the above is the following figure...
<<

>>
dpb
on 4 Feb 2016
Oh, just one last thought...
What does
get(ax,'color')
return when x-axis not visible on the base trial? Wonder if by some chance the 'none' setting for the second axes could have gotten mucked upon....but then would think the lines wouldn't show up on ax(1), either....real long shot.
Image Analyst
on 4 Feb 2016
0 votes
Perhaps you'd like this, or an adaptation of it:

4 Comments
perfectionist
on 4 Feb 2016
Image Analyst
on 4 Feb 2016
Well, it was worth a shot. I didn't read the whole extensive discussion above, so I don't know the background, but it sounds like you want plotyy and that is what dpb is working with you on. Good luck.
I just looked at the source for plotxx and it's a very simple two-axes routine the guts of which boil down to
ax(1)=gca;
....
ax(2)=axes('Position',get(ax(1),'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','r','YColor','k');
set(ax,'box','off')
the settings for the 2nd axis vis a vis the first are identical as to what have been doing with plotyy (other than the hardcoded axis colors which is pretty user-belligerent there).
It's looking to me more and more like there's an issue with HG2. Do you have it, IA? Can/would you try perfectionist's sample above if so? Given the longwinded conversation, here's the link to the comment where the basic function was posted and there's a link to the data file at the bottom so you don't have too much hunting to do to find it.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!