Is plotyy producing jitter in the x-axis, or is it my code?

I've got a strange problem when I use the plotyy function in the following code;
% Plot Altitude and Versions with Time
figure('Name','Altitude and Versions','numbertitle','off');
[AX,H1,H2] = plotyy(State_Time, Alt, State_Time, Version, 'semilogy', 'plot');
% Add the title, legend, X-axis and Y-axes labels
title(Event_Title,'FontSize', 20);
legend(Source,'Location','NortheastOutside');
axes(AX(1));
ylabel('Altitude (m)','Fontsize', 20,'fontweight','b');
set(gca, 'XTickLabel',num2str(get(gca,'XTick')','%d'));
xlabel('UTC (Sec)','Fontsize', 20,'fontweight','b');
axes(AX(2));
ylabel('SV ID','Fontsize', 20,'fontweight','b');
% Set the markers, markersizes, and linestyles
set(H1,'Marker','o','MarkerSize',15);
set(H2,'Marker','+','MarkerSize',15);
set(H2,'LineStyle','none');
When the figure appears, it is as I would expect - with the exception of 2 items;
1. The x-axis UTC times have jitter in them. Think of the following line of text;
58570 58572 58574 58576 58578 58580
being overwritten with the following line of text;
5.8570 5.8572 5.8574 5.8576 5.8578 5.8580 x 10^4
The decimal notation is preferred. This is the first time I've used plotyy and I'm not sure if I'm violating some rule pertaining to the function itself.
2. I can seem to control the values of the 2nd y-axis (Version). I want only whole numbers to populate this axis. Currently, it is being autoscaled by MATLAB and I'm not sure how to get around it.
Any ideas?

1 Comment

Seems peculiar there was an edit after almost 3 yr of inactivity???
Is there some question unresolved here or just a cosmetic change, Brad?

Sign in to comment.

 Accepted Answer

dpb
dpb on 25 Sep 2013
Edited: dpb on 25 Sep 2013
It's your code... :)
[AX,H1,H2] = plotyy(State_Time, Alt, State_Time, SV_ID, 'semilogy', 'plot');
axes(AX(1));
set(gca, 'XTickLabel',num2str(get(gca,'XTick')','%d'));
The above writes the tick labels on the LH x-axis but there's no similar statement for the RH axes object. plotyy is two completely independent axes that just are overlaid on top of each other.
There has been much discussion before on why TMW doesn't automagically hide the RH axis x-ticks so there aren't two competing sets visible but there are some disadvantages to that by itself, too.
To solve your problem either
a) set both the same way
set(AX, 'XTickLabel',num2str(get(gca,'XTick')','%d'));
or b) hide the RH ticks entirely...
set(AX(2),'xtick',[])
in which case your previous code will have the desired appearances. All in all, the best solution is probably to use the a) form almost exclusively as it will keep the two in synch. This is particularly important if you decide to do something like zoom in on an axis; then the two won't coincide at all. The alternative way to do the latter is linkaxes but it only handles the range limits, not all the rest of the properties.

4 Comments

Good catch!! Earlier today I was troubleshooting this and tried:
set(AX(1), 'XTickLabel',num2str(get(gca,'XTick')','%d'));
Whoops.
Now, I have tried the correct method (yours), and got the proper x-axis UTC times (and RH y-axis values after editing the plot). However, I did notice that when I maximized the plot to the full screen, the x-axis UTC times repeat themselves (58570 - 58580 followed by 58570 - 58574 - all with 2 second increments). Is this a byproduct of setting the axes the same way?
No, that's owing to the fact you set a given number of xtick labels independent of the xtick values. When you zoomed, the autoscale feature drew more tick marks but now there aren't enough separate labels for them. The internal logic implemented in handle graphics is to recycle the ones it has; hence the symptoms you see.
Which is why it's better to retrieve xtick instead of xticklabel and you need to then retrieve them and reset the labels when the axes tick values/numbers change.
What is needed is a 'format' property that is settable so that such changes can be made without having to rewrite the labels themselves; just set the desired format string. I've suggested this a number of times in the distant past but never got anywhere and I've given it up as hopeless--you're welcome to hoist the banner one more time--it would be a very useful enhancement--and it's been >15 yr now since I last did the exercise.
I worked on a problem a month or two ago where a guy had jittery glitches in his plot. He changed renderers and said that fixed the problem.
dpb
dpb on 26 Sep 2013
Edited: dpb on 26 Sep 2013
That does happen on occasion, indeed...I've seen it rarely but it does/can happen w/ the double axes wherein for some reason the roundoff isn't identical for the two, apparently...I've always figured that it when it happens it's owing to the one being left the other right on position that ends up making for slightly different computations of position, etc., that once in a great while do fall into the FP roundoff pit...but that's purely conjecture.

Sign in to comment.

More Answers (0)

Products

Asked:

on 25 Sep 2013

Commented:

dpb
on 4 Sep 2016

Community Treasure Hunt

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

Start Hunting!