matplotlib icon indicating copy to clipboard operation
matplotlib copied to clipboard

ENH: Add legend toggle to figure options dialog box

Open mspacek opened this issue 7 years ago • 16 comments

Feature request

It would be nice if the user could toggle display of the legend (if any) in the figure options dialog box. Currently, it's possible to edit curve labels and regenerate the legend, but not toggle the display of the legend. This would be especially useful because sometimes the legend can obscure parts of the plot.

Apologies if this has been discussed somewhere already. I couldn't find anything here in GH issues.

Matplotlib version

  • Operating system: Xubuntu 16.04
  • Matplotlib version: 2.2.2, installed from pip
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.5.2
  • Jupyter version (if applicable):
  • Other libraries:

mspacek avatar Apr 23 '18 12:04 mspacek

I would support this in general.
In the meantime, you can make your legend draggable if that it desired. plt.legend().draggable(). While this will not turn it on or off it allows the user to drag it somewhere else in the plot where it might not disturb. Only drawback: It's kind of hard to get it back at the exact same position where it originally was after dragging.

ImportanceOfBeingErnest avatar Apr 23 '18 16:04 ImportanceOfBeingErnest

Wow, I didn't know about draggable(). Thanks! I wonder why it's disabled by default...

mspacek avatar Apr 25 '18 11:04 mspacek

I'd like to work on this issue. My interpretation of this feature would be to add a button to the toolbar to toggle the legend(s). My initial plan would be to set the alpha levels of the legend to 0/1 by pressing the button but there may be a better strategy. If anyone has any suggestions or corrections, do let me know.

wuaster avatar Mar 23 '20 00:03 wuaster

The legend has a set_visible() method.

timhoffm avatar Mar 23 '20 07:03 timhoffm

I've got the button to work and I can use it to toggle the legend on/off. For unit tests, I'm not sure how I would go about doing them. Since I want to stay consistent, I'm not sure how the other toolbar buttons/keypresses are tested. Is there an example of how I can write the tests? What I want to do is draw a figure with a legend, click the new toolbar button/press the toggle key, and then compare the figure afterward, how would I go about doing this?

wuaster avatar Apr 04 '20 17:04 wuaster

There are some examples of interactive tests in https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/tests/test_widgets.py . The way the interactive code works is that the GUI side of things generates an "event" which is then pushed through our callback machinery. It is possible to create those same events in the tests and push them through the machinery.

tacaswell avatar Apr 04 '20 17:04 tacaswell

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

github-actions[bot] avatar May 05 '23 01:05 github-actions[bot]

@wuaster any progress on this? Is there a PR?

mspacek avatar May 05 '23 06:05 mspacek

It is worth noting that although the dialog is created as part of the test, the callback where the settings are applied is currently not tested. Hence, one can maybe not expect that it will be feasible to test this as part of the test suite. (Although of course that would be really nice, but not really related to the issue.)

https://app.codecov.io/gh/matplotlib/matplotlib/blob/main/lib/matplotlib/backends/qt_editor/figureoptions.py

oscargus avatar May 05 '23 09:05 oscargus

@mspacek Sorry, no there isn’t.

wuaster avatar May 05 '23 13:05 wuaster

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

github-actions[bot] avatar Jul 15 '24 01:07 github-actions[bot]

Good first issue - notes for new contributors

This issue is suited to new contributors because it does not require understanding of the Matplotlib internals. To get started, please see our contributing guide.

We do not assign issues. Check the Development section in the sidebar for linked pull requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please collaborate on the work by reviewing it rather than duplicating it in a competing PR.

If something is unclear, please reach out on any of our communication channels.

github-actions[bot] avatar Jul 18 '24 02:07 github-actions[bot]

Relevant file to work on is here: https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/backends/qt_editor/figureoptions.py

Work to do:

  • figure out how to add a "legend visible" checkbox
  • implement the callback to toggle the legend visibility

I don't think we can fold this into the "(Re)generate legend" tick box, but you can probably adapt that code.

Despite my earlier comment, the main part of this is not something that we can easily test so "manual testing" will have to do for this work.

Good first issue because the code that needs to be changed is very isolated, but medium because you will have to understand and modify a declarative UI builder.

tacaswell avatar Jul 18 '24 02:07 tacaswell

hey @tacaswell i would like to take on the issue if its still open for assignement

RohitP2005 avatar Jan 08 '25 03:01 RohitP2005

Hi @RohitP2005 you are welcome to submit a pull request for any issue, but we do not assign them. Please see our contributors’ guide: https://matplotlib.org/devdocs/devel/index.html

rcomer avatar Jan 08 '25 10:01 rcomer

@tacaswell #30128 to add the ability to toggle the legend

ritvi-alagusankar avatar Jun 02 '25 14:06 ritvi-alagusankar