MATLAB: Top x-axis disappeared in plotyy

plotyy

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?

Best Answer

OK, now we have enough to diagnose the real problem -- turns out didn't need so much but without something that could run to illustrate your concern didn't know exactly what the issue really, really was/is...
Anyway,
That's the symptom I described "way back when"... :)
A) box on will restore the top x-axis if it isn't displayed. As noted, why it's left 'on' in some instances and 'off' in others is buried in the internals of HG that have no way to ascertain but restoring it to 'on' will fix the issue. If you want the axis shown, then it can't hurt to force it irrespective. This one is, as I think I mentioned also, probably worth an official bug/interpretation request to TMW.
B) This is the other one I previously outlined. Unfortunately, you can't eliminate the dual tick marks on the right axes as long as you force having a different number of tick marks and non-consistent spacing because there are two independent axes objects and TMW didn't include the facility to separate the tick marks on the LH and RH axes (or top or bottom either, for that matter) as independent properties; they're both on or both off.
ADDENDUM It just dawned on me the reason for A -- it's because the internal logic is "smart". It recognizes the discord between y tick mark locations on the two axes and so turns off the box for both axes so the duplicated and non-matching y-ticks don't confuse. BUT, this has the side-effect of turning off the upper x-axis as well.
I turned your function into a script so had access to the variables at the command line (or you can just use the debugger and stop inside) but try
set(ax(2),'box','on')
to see what happens, then toggle it back off and switch axes.
I don't think the lengths have anything to do with it other than whether by happenstance you end up with the same number of y ticks or not; that will, I'm virtually certain, be the key point.
I've got the "high-priced consultant" coming in at noon; gotta' run now but I'll try to check back later on to see if you've other issues/concerns.
Oh, one last thought -- I don't have time now to do it, if you really, really, really must have the top x axis, you can either just draw the tick marks if that's all needed (no labels) or add yet another axis object that overlays these with x-axis at the top. I think that'll work altho there may be some issues of transparency to make all levels show thru. To see "issues", try
axes(ax(2))
axes(ax(1))
and likely the legend will become occluded.
Oh, one last thought, given your moniker... :) I've complained for almost 30 yr and TMW hasn't fixed it yet, but the mismatched formatting on the axis tick labels just bugs me no end. I'd add
ytk=get(ax(2),'ytick').'; % retrieve tick values
set(ax(2),'yticklabel',num2str(ytk,'%.1f')) % set with consistent formatting
You can, of course, dispense with the temporary ytk by folding into the num2str argument but it's far easier to write initially as above. BTW, do NOT forget the transpose; you won't like the results otherwise! :)
Good luck, hope that 'splains the problems/issues...they're pretty much inherent w/ HG. An enhancement request never hurts.
ADDENDUM
As is so often the case, the real solution just came to me--
set(ax(2),'xaxislocation','top','xticklabel','')
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.