bugGNU Octave - Bugs: bug #60792, legend function returns too few...

 
 

bug #60792: legend function returns too few objects in different order compared to Matlab

Submitted by:  Philip Nienhuis <philipnienhuis>
Submitted on:  Thu 17 Jun 2021 06:23:53 PM UTC  
 
Category:  Plotting Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Need Info Assigned to:  None
Originator Name:  Philip Nienhuis Open/Closed:  Open
Release:  dev Operating System:  Microsoft Windows

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

( Jump to the original submission)

Mon 21 Jun 2021 08:43:03 PM UTC, comment #8: 

(sorry away from my PC's until mid July)
No you didn't sound like a school teacher to me, no worries :-)

What you write seems obvious and is otherwise fully understandable.
But here we hit the unfortunate situation where Octave's graphics system is still compatible with a much older version of Matlab's where the constructs were fully documented and supported, but Octave's behavior has already been made compatible to a newer graphics system w/o prospect yet when Octave will follow to switch to object based graphics in the first place.

When I'm back I'll try to at least amend the order of returned obect handles of output arg #2; at first sight it looks like a simple reshape could go a long way. Maybe similarly for the other optional output args (haven't looked yet at those)

Philip Nienhuis <philipnienhuis>
Project Member
Sun 20 Jun 2021 06:59:30 PM UTC, comment #7: 

Sorry if my words sounded like schoolteacher.

The legend function were entirely rewritten to be compatible with its ML counterpart based on recent documentation only. Needless to explain how hard it would be to maintain compatibility with older undocumented Matlab interface, especially when Matlab it self discourages its use... How do I know that Matlab will maintain this interface in the next release? How do I know what kind of objects are used to draw icons?

Pantxo Diribarne <pantxo>
Project Member
Thu 17 Jun 2021 10:53:00 PM UTC, comment #6: 

Sure, but my program smetimes needs to fine tune a few individual legend items (color, linewidth, that sort of stuff). Not all legend items.
AFAICS Matlab r2020b gives nothing that yields access to those individual properties through the legend handle comparable to / compatible with Octave.  "Mess around" - your words, I know of no other way to get it done that works in Octave & Matlab r2014a and 2014b+.

Philip Nienhuis <philipnienhuis>
Project Member
Thu 17 Jun 2021 10:12:40 PM UTC, comment #5: 

Octave doesn't support HG2 objects but nevertheless its legend objects (still axes based) have the same legend specific properties. So you can basically do whatever you can do with ML legends, you just cannot use e.g. "lg = legend (); lg.FontSize = 14", you'll rather have to do the equivalent "lg = legend (); set (lg, 'fontsize', 14)", which also works in Matlab.

You should not have to mess with legend items objects directly which should be considered internal.

Pantxo Diribarne <pantxo>
Project Member
Thu 17 Jun 2021 08:30:13 PM UTC, comment #4: 

Oops, the last Octave part should read:

>> for ii=1:numel (hlc)  ## instead of numel (obj)
  get (hlc(ii), 'type')
end
ans = text
ans = line
ans = text
ans = line

Philip Nienhuis <philipnienhuis>
Project Member
Thu 17 Jun 2021 08:27:29 PM UTC, comment #3: 

In Octave you can do:

>> [hl] = legend (h(:), {'sin', 'cos'});
>> hlc = get (hl, "children")
hlc =
  -2.8608
  -4.7081
  -5.6784
  -7.1292
>> for ii=1:numel (obj)
  get (hlc(ii), 'type')
end
ans = text
ans = line
ans = text
ans = line

but Matlab r2020b says:

>> hlc = get (hl, 'children')
hlc =
  0×0 empty GraphicsPlaceholder array.
>>

so that doesn't help.

Philip Nienhuis <philipnienhuis>
Project Member
Thu 17 Jun 2021 08:19:16 PM UTC, comment #2: 

I just typed "help legend":

:

    [LEGH,OBJH,OUTH,OUTM] = legend(...) returns a handle LEGH to the
    legend axes; a vector OBJH containing handles for the text, lines,
    and patches in the legend; a vector OUTH of handles to the
    lines and patches in the plot; and a cell array OUTM containing
    the text in the legend.
:

That said, I now see that r2020b says:

Returning multiple outputs is not recommended
Not recommended starting in R2014b

Returning multiple outputs using this syntax is no longer recommended:

[lgd,icons,plots,txt] = legend(__)
This syntax creates a legend that does not support some functionality introduced in R2014b or later. For example, you cannot add a title to the legend or specify the number of legend columns. Also, the legend does not automatically update when you add or remove data series from the axes.

Instead, return the Legend object and set Legend Properties.

lgd = legend(__)

.. which is apparently related to their new object based graphics.
Well, Octave doesn't support that (yet?), so Matlab's suggested workaround won't work in Octave.

Philip Nienhuis <philipnienhuis>
Project Member
Thu 17 Jun 2021 07:42:52 PM UTC, comment #1: 

Can you post a link to the related doc? All I could find is a calling form with a single output legend object (not handle) :

https://nl.mathworks.com/help/matlab/ref/legend.html#d123e757928

Pantxo Diribarne <pantxo>
Project Member
Thu 17 Jun 2021 06:23:53 PM UTC, original submission:  

(I couldn't find an earlier mention of this in the bug tracker. hopefully it isn't a duplicate - there are many legend-related bugs in the tracker)

Consider this little script + results:
Octave-7.0.0 (crossbuilt June 15):

x = 1:10;
h = plot (x, sin (x), x, cos (x));
[~, obj] = legend (h(:), {'sin', 'cos'});
for ii=1:numel (obj)
  get (obj(ii), 'type')
end
ans = text
ans = line
ans = text
ans = line

... and the the same script in Matlab r2014a & r2020b:

>> x = 1:10;
h = plot (x, sin (x), x, cos (x));
[~, obj] = legend (h(:), {'sin', 'cos'});
for ii=1:numel (obj)
  get (obj(ii), 'type')
end
ans =
    'text'
ans =
    'text'
ans =
    'line'
ans =
    'line'
ans =
    'line'
ans =
    'line'

The Matlab docs state that for each plotted item three objects (handles) will be returned, i.e., a text object, line object and patch object.
Octave just returns the first two types, and with multiple plotted items the order is different than in Matlab.

This makes for interesting confusion to get code that does post-processing on legends running in both Matlab and Octave.

Philip Nienhuis <philipnienhuis>
Project Member

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

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 philipnienhuis (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-06-17 pantxo StatusNone => Need Info

    Back to the top


    Powered by Savane 3.6