bugGNU Octave - Bugs: bug #52700, plotyy legend in reverse order

 
 

bug #52700: plotyy legend in reverse order

Submitter:  None
Submitted:  Tue 19 Dec 2017 09:53:26 PM UTC
   
 
Category:  Plotting Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Regression
Status:  Fixed Assigned to:  None
Originator Name:  Blindcurrent Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * dev
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Tue 19 Nov 2019 10:46:35 PM UTC, comment #11: 

@Pantxo: Correct.  The first example was so confusing that it fooled me again in to thinking something was wrong.  Closing report.

Rik <rik5>
Group administrator
Tue 19 Nov 2019 10:17:49 PM UTC, comment #10: 

I had used the example from comment #5 because as you said, the one from comment #1 was misleading. What you show is the expected result, but you affect the first label ("sine") to the horizontal line initially created and the second ("cosine") to the line created second (the sine).

Pantxo Diribarne <pantxo>
Group Member
Tue 19 Nov 2019 04:00:30 PM UTC, comment #9: 

There is still a problem with the color of the legend keys.  I used the test code from comment #1


x=0:.01:2*pi;
sine=sin(x);
cosine=cos(x);

figure
[AX,H1,H2]=plotyy([0 2*pi],[0 0], [0 2*pi],[0 0]);
hold on
plot(x,sine,'parent',AX(1),'b','linewidth',2);
plot(x,cosine,'parent',AX(1),'r','linewidth',2);
plot(x,1.1*sine,'parent',AX(2),'g:','linewidth',1); % just to use the right axis for something

leg=legend(AX(1),'sine','cosine');
set(leg,'location','northeast');


The file bad_key_color.png shows that the keys don't match the line objects.



Rik <rik5>
Group administrator
Tue 19 Nov 2019 12:26:41 PM UTC, comment #8: 

This should be working again.

Pantxo Diribarne <pantxo>
Group Member
Mon 18 Nov 2019 10:06:50 PM UTC, comment #7: 

Re-opening this bug as it fails again with the new legend.m code for the 6.1 release.

Rik <rik5>
Group administrator
Thu 21 Dec 2017 03:46:38 PM UTC, comment #6: 

Thank you for your suggestions and explanations. Assigning a handle to each curve and specifying which ones should appear in the legend is a very good idea to make sure the right lines appear in the legend in the right order.

Anonymous
Wed 20 Dec 2017 05:26:42 PM UTC, comment #5: 

I also agree that the script is confusing becasue there are 5 line objects for labeling spread across two axes.  I re-wrote the script to be a little clearer and avoid greating the initial two line objects in the plotyy command.


x=0:.01:2*pi;
sine=sin(x);
cosine=cos(x);

figure
#[AX,H1,H2]=plotyy([0 2*pi],[0 0], [0 2*pi],[0 0]);   # original
[AX,H1,H2]=plotyy(x, sine, x, 1.1*sine);
hold on
#plot(x,sine,'parent',AX(1),'b','linewidth',2);       # original
set (H1, 'color','b', 'linewidth', 2);
#plot(x,cosine,'parent',AX(1),'r','linewidth',2);     # original
plot (AX(1), x,cosine, 'r', 'linewidth', 2);
#plot(x,1.1*sine,'parent',AX(2),'g:','linewidth',1);  # original
set (H2, 'color', 'g', 'linestyle', ':', 'linewidth', 1);

leg=legend(AX(1),'sine','cosine');
set(leg,'location','northeast');


This is attached as file tst_plotyy2.m.

Note that on 4.2.1 the new script no longer puts a thin line into the legend, but the legend objects are reversed such that the sine label is on the cosine color and vice versa.  On the development branch this has been fixed and everything works correctly.  For that reason I'm going to mark this report as fixed and close it.

Also, even in 4.2.1 you can get this to work by simply being explicit about what you want.  If you know the particular objects you want to label, just tell the legend function what those are, rather than having it try to guess, and guess incorrectly.

In this case, the handle for the sine plot is already in H1 so change the cosine plot command to make sure you capture the handle


H3 = plot (AX(1), x,cosine, 'r', 'linewidth', 2);


And then when you call legend give it the objects you want to label


leg=legend([H1, H3], 'sine', 'cosine');




(file #42691)

Rik <rik5>
Group administrator
Wed 20 Dec 2017 05:04:04 PM UTC, comment #4: 

It's not uncommon for a bug to manifest differently for different systems.  The first step is confirming the bug and trying to characterize it.  I'm testing on linux.  Typically, the more inconsistent bugs are related to a bad pointer or index, i.e., some kind of randomness about it.

Is it certain that this works in Matlab?  The example generates an initial plot with simple lines on it:


[AX,H1,H2]=plotyy([0 2*pi],[0 0], [0 2*pi],[0 0]);


So, I take it that means AX(1) has one plot object and AX(2) has one plot object.  Note that the left axis AX(1) is highlighted with a thin cyan line (and the right axis AX(2) a thin brown line).

At that point hold is turned on.

Next, a second and third plot object are added to the first axis:


plot(x,sine,'parent',AX(1),'b','linewidth',2);
plot(x,cosine,'parent',AX(1),'r','linewidth',2);


and a second plot object is added to the second axis:


plot(x,1.1*sine,'parent',AX(2),'g:','linewidth',1); % just to use the right axis for something


So, at that point we have 3 plot objects on AX(1) and 2 plot objects on AX(2).

Now, we specify just two legend entries for that first axis:


leg=legend(AX(1),'sine','cosine');


So, my suspicion is that there is something not quite programmed consistently with regard to what to do in that situation.  On my system, somehow the legend example line is being grabbed for the first legend entry.

How about if I make the legend instead:


leg=legend(AX(1),'extra line','sine','cosine');


I then see what looks like proper legend labeling and samples.  [BTW, that dotted green line is extremely difficult to see on my system.  The ratio of white space to green dot is rather large.]

The initial plot of lines from zero to two-pi seems somewhat extraneous.  One could instead do:


x=0:.01:2*pi;
sine=sin(x);
cosine=cos(x);

figure
[AX,H1,H2]=plotyy(x,sine,x,1.1*sine);
set(H1,'color','b','linewidth',2);
set(H2,'color','g','linestyle',':','linewidth',1);
hold on
plot(x,cosine,'parent',AX(1),'r','linewidth',2);
xlim(AX(1), [0 2*pi]);
xlim(AX(2), [0 2*pi]);

leg=legend(AX(1),'sine','cosine');
set(leg,'location','northeast');


After those changes, I then see what I think you originally had in mind.

Anyway, what I see from your original code kind of makes sense in terms of the legend.  The 'sine' text entry is actually labeling that initial flat line from zero to two-pi.  The 'cosine' text entry is actually labeling the second plot object which is the sine function.  So, the question is, Why is your system producing one in which the first line is red, weight 2?

Oh, you are testing on Version 4.2.1?  This issue may have been fixed already.  As I said, I recall a group of us having debugged some of this plotyy legend behavior in the not-too-distant past.  It's probably difficult for you to build the development version for Windows. (I know nothing about the Octave-for-Windows build process or who archives what where.)

Dan Sebald <sebald>
Wed 20 Dec 2017 10:09:37 AM UTC, comment #3: 

I don't quite understand your statement about the colours of the legend line examples being wrong. They appear as blue and red just like they are supposed to. Do you see the mismatch in the attached figure files, or did you run the code on your computer?

Further, I did not find the issue with the reversed legend order reported in other bug reports, including the ones you cite.

Anonymous
Tue 19 Dec 2017 11:31:39 PM UTC, comment #2: 
Dan Sebald <sebald>
Tue 19 Dec 2017 11:27:48 PM UTC, comment #1: 

The legend line examples aren't matching here.  cosine comes out blue (correct), while sine comes out thin cyan (not red).  Both for gnuplot and FLTK.  This reminds me of some kind of plot bug a few months back in which we had multiple colors on multiple axes and at some point we decide it was good enough because of the confusion it created.

Dan Sebald <sebald>
Tue 19 Dec 2017 09:53:26 PM UTC, original submission:  

A legend added to a plot created with the plotyy command comes out wrong when it is located in the northeast. In this one location, the order of the plotstyle entries (not the string labels) is reversed.

Please see this example code together with the attached figures:


x=0:.01:2*pi;
sine=sin(x);
cosine=cos(x);

figure
[AX,H1,H2]=plotyy([0 2*pi],[0 0], [0 2*pi],[0 0]);
hold on
plot(x,sine,'parent',AX(1),'b','linewidth',2);
plot(x,cosine,'parent',AX(1),'r','linewidth',2);
plot(x,1.1*sine,'parent',AX(2),'g:','linewidth',1); % just to use the right axis for something

leg=legend(AX(1),'sine','cosine');
set(leg,'location','northeast');


The legend is correct in all locations other than northeast, including all the outside versions.

I am using Windows 7 and have installed Octave using the w64 installer. I have found the issue to occur in the versions 4.2.0 and 4.2.1.

This bug may be related to https://savannah.gnu.org/bugs/?50497 or https://savannah.gnu.org/bugs/?35314, but neither of these covers the location dependence.

Final note: I do not need a patch, because applying it is beyond my expertise. I am only reporting it so that it can be fixed in a future version of Octave. In the meantime, my workaround is to plot the graphs in reverse order so that the legend comes out right.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #47896:  bad_key_color.png added by rik5 (40KiB - image/png)
file #42691:  tst_plotyy2.m added by rik5 (575B - text/x-matlab)
file #42686:  northeast.png added by None (146KiB - image/png)
file #42687:  east.png added by None (146KiB - image/png)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by pantxo (Posted a comment)
  • -email is unavailable- added by rik5
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by sebald (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 15 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-11-19 rik5 StatusReady For Test Fixed
        Open/ClosedOpen Closed
    2019-11-19 rik5 Attached File- Added bad_key_color.png, #47896
    2019-11-19 pantxo StatusConfirmed Ready For Test
    2019-11-18 rik5 Item GroupIncorrect Result Regression
        StatusFixed Confirmed
        Open/ClosedClosed Open
        Release4.2.1 dev
        Operating SystemMicrosoft Windows Any
        Carbon-Copy- Added pantxo
    2017-12-20 rik5 Attached File- Added tst_plotyy2.m, #42691
        StatusNone Fixed
        Open/ClosedOpen Closed
    2017-12-19 None Attached File- Added northeast.png, #42686
        Attached File- Added east.png, #42687

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code