You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add Checkboxes to Legend Controlling Subplot Visibility #16796
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.
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.
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.
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.
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):
defset_draggable(value: bool): ...
# We already have this interactivitydefset_toggles_visiblity(value: bool):
# activate the event listeners for toggling the visibility of lines
Activity
anntzer commentedon Mar 16, 2020
I think this is pretty close to https://matplotlib.org/gallery/event_handling/legend_picking.html?
RiazCharania commentedon Mar 17, 2020
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.
anntzer commentedon Mar 17, 2020
Consider extending that example, then.
timhoffm commentedon Mar 17, 2020
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.RiazCharania commentedon Mar 21, 2020
Ah, that makes sense. We'll aim to extend the example then. Thanks for the feedback!
RiazCharania commentedon Mar 29, 2020
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.
timhoffm commentedon Jul 16, 2020
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.
ecstrema commentedon Aug 12, 2024
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
draggableoption. So why not add a "visibility_checkboxes" or "view_toggle" or the verbose "click_to_toggle_visiblity" option?timhoffm commentedon Aug 12, 2024
@ecstrema Thanks for the feedback. Your argument is valid. I've created #28708 to track possible interactivity improvements.