bugGNU Octave - Bugs: bug #66728, legend inconsistencies

 
 

bug #66728: legend inconsistencies

Submitter:  Muhali <muhali>
Submitted:  Tue 28 Jan 2025 05:54:01 PM UTC
   
 
Category:  Plotting Severity:  3 - Normal
Priority:  5 - Normal Item Group:  None
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * 9.3.0 Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 29 Jan 2025 01:26:23 PM UTC, comment #4: 

comment #2:

> * Issue 2: Now that I read the help text for legend more carefully, I see it is already clearly stated that unless and axes handle HAX is specified as first argument, it will "Display a legend for the current axes...". Which in our case is the latest created axes, ax(3).


You are right. But I had expected that text near where the HOBJS are mentioned.

> * Issue 3: I couldn't find any mention of a restriction in ML doc saying that a legend associated to a given axes should only label graphics objects pertaining to this axes. Does ML also allow graphics objects from different figures in a single legend? E.g., does the following work and if so what does happen?


Yes, apparently. But the 3 legends all appear in the first figure, although gcf = 2. But note that Matlab only offers axis objects as a first argument, no other graphic handles.

Muhali <muhali>
Wed 29 Jan 2025 01:15:20 PM UTC, comment #3: 

I don't see any error when I run those commands in Matlab R2024b.

See the attached images for the result.



Markus Mützel <mmuetzel>
Group administrator
Wed 29 Jan 2025 12:43:27 PM UTC, comment #2: 

@muhali:

  • Issue 2: Now that I read the help text for legend more carefully, I see it is already clearly stated that unless and axes handle HAX is specified as first argument, it will "Display a legend for the current axes...". Which in our case is the latest created axes, ax(3).
  • Issue 3: I couldn't find any mention of a restriction in ML doc saying that a legend associated to a given axes should only label graphics objects pertaining to this axes. Does ML also allow graphics objects from different figures in a single legend? E.g., does the following work and if so what does happen?



figure;
h1 = plot (1:10)
hold on
title F1
ax = gca;
figure;
h2 = plot (1:10, "-r");
title F2
legend ([h1 h2], "first", "second")
h3 = plot (ax, rand (1,10), "-g")



Pantxo Diribarne <pantxo>
Group Member
Tue 28 Jan 2025 09:38:35 PM UTC, comment #1: 

So there are at least 4 issues:

> it is not clear why axes #2 and #3 are indented (and #1 is not).

1. This looks like a bug in `subplot` rather than `legend`, I think. The first, multi-line, axes is stuck. Even trying to add a fat `ylabel ({"BIG", "BIG"}, "fontsize", 50)` doesn't work. With a single-line first axes it works as intended.

> 'legend (HOBJS, ...)', which is meant for the current axis but is not mentioned

2. That should indeed be documented.
3. Does ML  explicitly say that you can ask a legend to handle objects (lines here) belonging to other axes than the current one (or the one specified by HAX argument)? The legend code has clearly not been written to handle that and I am very surprised that this even (looks like it) works.

> Interestingly, Matlab plots the legend to axis #1

4. Looks like a WTF Matlab situation

Marking confirmed at least for issue 2 and 3. See bug #66729 which I think should cover issue 1.

Pantxo Diribarne <pantxo>
Group Member
Tue 28 Jan 2025 05:54:01 PM UTC, original submission:  

The following code reveals multiple issues with the legend command:

col = {"r" "g" "b"} ;
for j = 1 : 3
   if j < 2
      ax(j) = subplot(4, 1, [1 2]) ;
   else
      ax(j) = subplot(4, 1, 1+j) ;
   endif
   h(j) = plot(randn(1, 5), col{j}) ;
endfor
hl = legend(h, {"rrrrr" "ggggg" "bbbbb"}, "location", "eastoutside") ;

First, it is not clear why axes #2 and #3 are indented (and #1 is not). Then the legend command as used here is motivated by the help text 'legend (HOBJS, ...)', which is meant for the current axis but is not mentioned. The text should obviously be legend (HAX, HOBJ, ...) which works.
Interestingly, Matlab plots the legend to axis #1. And if one does legend (ax(2), h(1:2), ...) Matlab plots to legends to and indents axis #2, while octave indents again #2 and #3.

Muhali <muhali>

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #56827:  figure1.png added by mmuetzel (19KiB - image/png)
file #56828:  figure2.png added by mmuetzel (15KiB - 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 mmuetzel (Updated the item)
  • -email is unavailable- added by pantxo (Posted a comment)
  • -email is unavailable- added by muhali (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 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2025-01-29 mmuetzel Attached File- Added figure1.png, #56827
        Attached File- Added figure2.png, #56828
    2025-01-28 pantxo StatusNone Confirmed

    Back to the top

    Powered by Savane 3.14-9aa3.
    Corresponding source code