Skip to content

Add Checkboxes to Legend Controlling Subplot Visibility #16796

Closed

Description

@RiazCharania

I'd like to recommend a new feature that I'd like to see and would be willing to implement. When initializing the legend you could set an argument to true (that would probably be set to None by default) that would add interactive checkboxes to the entries to the legend. Clicking on these checkboxes would check/uncheck them and would toggle the respective entries on or off.

I've seen a similar feature on other applications I could link to if this wasn't clear.

Activity

  1. anntzer commented on Mar 16, 2020

    @anntzer
    Contributor
  2. RiazCharania commented on Mar 17, 2020

    @RiazCharania
    Author

    The functionality would be similar, but we had some issues with the example. After trying it a bit, we were unable to get Legend Picking to work on bars since it relies on get_lines(). Also, our plan is to also simplify the entire process for users by abstracting away the “onpick” function (example in Legend Picking), and instead have it as just an argument when you initialize the legend. Furthermore, the current implementation makes it hard for users to know if Legend Picking is enabled without trying it, as there is no visual indicator; we plan to add checkmarks beside label elements to make it clear.

  3. anntzer commented on Mar 17, 2020

    @anntzer
    Contributor

    Consider extending that example, then.

  4. timhoffm commented on Mar 17, 2020

    @timhoffm
    Member

    I'm very hesitant of adding this kind of functionality to core matplotlib. While I see the benefit of it, there are a number of non-interactive backends and we should be careful mixing such capability into the API.

    I recommend writing a function make_legend_interactive(legend) that abstracts away the logic, but does not need to be built into core matplotlib. This could be used to extend the example.

  5. RiazCharania commented on Mar 21, 2020

    @RiazCharania
    Author

    Ah, that makes sense. We'll aim to extend the example then. Thanks for the feedback!

  6. RiazCharania commented on Mar 29, 2020

    @RiazCharania
    Author

    We've decided to create a custom handler that would consist of both a line/patch and a checkbox beside it. We've completed the main functionality (as a prototype of sorts) but are unsure of how we should ask users should interact with it.
    We haven't been able to find a way to use this method after the legend was initialized so we've been putting a legend on the plot using plt.legend(handlers, labels, handler_map={[handlers : customHandler()]}), we don't know if this would be ideal for users. We'd love to hear if you have any suggestions, thanks.

  7. timhoffm commented on Jul 16, 2020

    @timhoffm
    Member

    I don't think this made it to a pull request. And it's not important enough to be picked up by core devs. Closing as stalled.

    Anybody is welcome to contribute an example as discussed above.

  8. ecstrema commented on Aug 12, 2024

    @ecstrema

    IMO, this is where matplotlib lies behind the most: core interactivity. I know non interactive backends would have to account for this, but I do believe that adding some basic interactivity would be a win for most users.

    Also and importantly, some basic interactivity already exists for the legend: the draggable option. So why not add a "visibility_checkboxes" or "view_toggle" or the verbose "click_to_toggle_visiblity" option?

    Legend(Artist):
        def set_draggable(value: bool): ...
            # We already have this interactivity
        def set_toggles_visiblity(value: bool):
            # activate the event listeners for toggling the visibility of lines
  9. timhoffm commented on Aug 12, 2024

    @timhoffm
    Member

    @ecstrema Thanks for the feedback. Your argument is valid. I've created #28708 to track possible interactivity improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions