MATLAB Answers

two extremely odd behavior using plotyy

2 views (last 30 days)
Leo Simon
Leo Simon on 24 Oct 2020
Commented: Leo Simon on 24 Oct 2020
My matlab version is 2020a. The following code has two problems. Are they really indended behavior??
1) there are eight lines, but the legend command insists that there are only seven
2) the first entries in YY{1,2} and YY{2,2} are identical, yet when plotted, the are different.
Can somebody please suggest a way to fix this, i.e., have the legend recognize all eight lines, and have the first elements of YY{1,2} and YY{2,2} be coincident?
Thanks very much for any suggestions
YY{1,1} = [ -0.1479019945775 -0.1207614728849 -0.0853912563830
-0.1332722959302 -0.1088113389903 -0.0769390531180
-0.1204885338805 -0.0983676376342 -0.0695513925373];
YY{2,1} = [ -0.1479019945775 -0.1207614728849 -0.0853912563830
-0.1315463790731 -0.1074015009182 -0.0759418727129
-0.1170135908441 -0.0955291160389 -0.0675440123853];
YY{1,2} = [ 1.8095706646177
1.6688286822702
1.5392601779117];
YY{2,2} = [ 1.8095706646177
1.6517555878194
1.5033917356162];
close all;
hold on;
[hPlotyy{1},plotyyAx{1,1},plotyyAx{2,1}]=plotyy([1:3],YY{1,1},[1:3],YY{1,2})
[hPlotyy{2},plotyyAx{1,2},plotyyAx{2,2}]=plotyy([1:3],YY{2,1},[1:3],YY{2,2});
legend

Answers (1)

Walter Roberson
Walter Roberson on 24 Oct 2020
hold on is holding the default ylim but some of your data is below that.
hold on does not affect all axes, only the current axes.
plotyy will reuse the current axes if hold is on for it, but it will always generate a new second axes. You therefore have three active axes. First plotyy uses the active axes because hold is already on, and generates ax2. Second plotyy reuses the current axes ax1 and generates ax3.
automatic range generation on the two unheld axes is not going to be the same as each other.
  1 Comment
Leo Simon
Leo Simon on 24 Oct 2020
Thanks very much Walter, I put back in the handles from my original code, presumably the 3rd axis is represented by one of the handles, but I don't understand how they really work. Could you explain how I would get all 8 lines in the legend, please?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!