Skip to content

[ENH]: Reverse legend  #24712

Closed
Closed

Description

@timhoffm

Problem

Taken from #19591 (comment).

There are cases in which one may want the legend items to be drawn in reverse oder.

Proposed solution

Add a parameter reverse to Legend.

Implementation-wise this should be as simple as reversing

for orig_handle, label in zip(handles, labels):
.

In this simple form, reverse would be a creation-time setting, which could not be changed afterwards. But I think this is ok for now. Reversing at runtime would need a bit more code restructuring.

Activity

  1. zmwaris1 commented on Dec 14, 2022

    @zmwaris1

    Hi, @timhoffm I would like to take up this issue, can you assign this to me please?

  2. jklymak commented on Dec 14, 2022

    @jklymak
    Member

    @zmwaris1 we do not assign issues. Thanks!

  3. melissawm commented on Dec 15, 2022

    @melissawm
    Member

    @zmwaris1 feel free to work on it by submitting a pull request. Cheers!

  4. zmwaris1 commented on Dec 15, 2022

    @zmwaris1
  5. linked a pull request that will close this issueReverse legend #24759on Dec 23, 2022
  6. pseyfert commented on Dec 27, 2022

    @pseyfert
    Contributor

    Sorry I didn't catch this earlier but with respect to what I had done in #19591 (and i didn't highlight that in the description, totally my fault) there's a different treatment of multiple plot calls in #24759 that came up in my production code.

    What got merged in #24759 reverses the entire legend, in #19591 I only reverse a stackplot. What I do in prod is call plot once with a line plot, and then call plot with a stack plot that is entirely below the previous line. (Other than the stack plot, I also want to see if the stack fills up the entire area up until the line or if there is some space left between). And for that I actually wanted that only the stack parts in the legend get filled top-most above bottom most, but the entire stack plot should still appear in the legend below line. Or more generally, subsequent calls to plot should fill the legend top to bottom like usual, and all the legend entries from a single call to plot (like a stack plot) should be reversed among themselves.

  7. jklymak commented on Dec 27, 2022

    @jklymak
    Member

    @pseyfert At some point you are going to need to do things manually. Stackplots do not naturally only have one order to the legend entries.

  8. story645 commented on Dec 27, 2022

    @story645
    Member

    @pseyfert would an expansion/additional subsection of controlling the legend enteries section of the tutorial help? I'm thinking something like this if we expect stackplot to be the common use case?

    # make data
    x = np.arange(0, 10, 2)
    ay = [1, 1.25, 2, 2.75, 3]
    by = [1, 1, 1, 1, 1]
    cy = [2, 1, 2, 1, 2]
    y = np.vstack([ay, by, cy])
    
    # plot
    fig, ax = plt.subplots()
    
    stack_handles = ax.stackplot(x, y, labels=['bottom', 'middle', 'top'])
    
    line_up, = ax.plot([1, 2, 3, 4, 5, 6,7,8], label='Line 2')
    line_down, = ax.plot([8,7,6,5,4,3, 2, 1], label='Line 1')
    
    ax.legend(handles = [line_down, *stack_handles[::-1], line_up])
    plt.show()

    And also/instead mayb update the reverse legend parameter doc to link, something like

    reversebool, default: False

    If True, the legend labels are displayed in reverse order from the input. If False, the legend labels are displayed in the
    same order as the input. For more complex orderings, see :doc:=/tutorials/intermediate/legend_guide.html#controlling-the-legend-entries

  9. added this to the v3.7.0 milestone on Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    New feature🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions