Visitar URL original
clone legend settings when regenerating legend in Qt figure by dsandeep0138 · Pull Request #17794 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20230308163725/https://github.com/matplotlib/matplotlib/pull/17794
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone legend settings when regenerating legend in Qt figure #17794

Closed
wants to merge 6 commits into from

Conversation

dsandeep0138
Copy link

Fixes #17775

@tacaswell tacaswell added this to the v3.4.0 milestone Jun 29, 2020
@tacaswell
Copy link
Member

Thanks for working on this!

Would it be possible to collect the public state we need and create a kwargs dictionary to pass to the legend call rather than updating after the fact? If it is possible we should try to avoid using private attributes (even internally).

Something like

kwargs = {k: getattr(old_legend, f'get_{k}')() for k in [...]}
ax.legend(**kwarg)

@dsandeep0138
Copy link
Author

dsandeep0138 commented Jun 29, 2020 •

@tacaswell Thanks for the review. The problem is not all the legend attributes have get function, so we may need to get the values from the attributes. Even then, we have some attributes which carry a lot of information (and are useful) like _legend_box (has text info and others), but this is not an argument to ax.legend().

So, I feel updating later, might be a better way than getting kwargs and calling ax.legend(*kwargs). Or the best way could be to have get_ functions for the rest (i.e., markerscale, scatterpoints, scatteryoffsets etc.) and modify _get_loc to get_loc(). Please let me know your thoughts.

@tacaswell
Copy link
Member

That is good point that we do not expose public accessors for everything so we need to reach in to the private attributes.

I still think it is better to assemble a kwargs dictionary and pass it in to the lgeend call because some of the values (like handler_map) are used in non-trivial ways as part of the init function(s) and we don't want to re-implement that logic here.

@dsandeep0138
Copy link
Author

@tacaswell I've made the changes. Please take a look. I have a query: What is the expected behavior when there is no legend initially, and then we tick re-generate legend option? Do we draw the legend?

@tacaswell
Copy link
Member

The current behavior is to generate a legend using the default kwargs which I think makes sense.

@tacaswell
Copy link
Member

This is looking on the right path!

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks correct and I interactively tested the original issue.

@tacaswell
Copy link
Member

https://codecov.io/gh/matplotlib/matplotlib/src/master/lib/matplotlib/backends/qt_editor/figureoptions.py

Ideally we would want some sort of automated test on this, but we don't currently have any coverage of this part of the code and it would be unfair to ask @dsandeep0138 to get that going in order to get this change in.

That said, if you want to take on how to test this @dsandeep0138 (either here or in a follow on PR) that would be great :)

@dsandeep0138
Copy link
Author

https://codecov.io/gh/matplotlib/matplotlib/src/master/lib/matplotlib/backends/qt_editor/figureoptions.py

Ideally we would want some sort of automated test on this, but we don't currently have any coverage of this part of the code and it would be unfair to ask @dsandeep0138 to get that going in order to get this change in.

That said, if you want to take on how to test this @dsandeep0138 (either here or in a follow on PR) that would be great :)

I'm figuring out how to write a test for this! More clearly, I'm trying to figure out how to tick the checkbox (to re-generate legend) from the test module, but I'm stuck and not really sure of how to do this :) I will try to figure it out as part of another PR. Thank you!

@tacaswell
Copy link
Member

If you can get a hold of the Qt objects there should be a setChecked method (https://doc.qt.io/qt-5/qabstractbutton.html#checked-prop which are the c++ docs, but the method names are the same it pytq/pyside as they are very direct wrappers) on the widget that is the tick box.

You may have better luck fabricating the input to this function and then directly calling it.

@dsandeep0138
Copy link
Author

@anntzer and @tacaswell, I have included the set_draggable changes. Can you please review and approve? Thank you.

@dsandeep0138 dsandeep0138 requested a review from anntzer July 3, 2020 23:08
@anntzer anntzer removed their request for review July 4, 2020 07:48
kwargs['mode'] = old_legend._mode
kwargs['title'] = old_legend.get_title().get_text()
kwargs['frameon'] = old_legend.get_frame_on()
kwargs['bbox_to_anchor'] = old_legend._bbox
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work without bbox_transform?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @QuLogic, bbox_transform was used and then loc/bbox_to_anchor was updated. Since we directly update 'loc' and 'bbox_to_anchor' from the old legend, we don't need to do bbox_transform again. Please let me know if you have any questions, or some suggestions from your end.

Copy link
Member

@QuLogic QuLogic May 15, 2021 •

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bbox_to_anchor and bbox_transform are combined to produce Legend._bbox_to_anchor though, yet you've only saved bbox_to_anchor in Legend._bbox. What does that have to do with loc?

@anntzer anntzer removed their request for review October 9, 2020 07:32
@QuLogic QuLogic added the status: needs clarification Issues that need more information to resolve. label Jan 14, 2021
@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 22, 2021
@jklymak jklymak marked this pull request as draft May 8, 2021 22:12
@jklymak
Copy link
Member

jklymak commented May 8, 2021

Hi @dsandeep0138 I know this languished for a while, but you need to ping for PRs to get reviewed. This is also waiting on an answer to @QuLogic comment above...

@jklymak
Copy link
Member

jklymak commented Oct 1, 2021

Closing as orphaned. Sorry this didn't get in!

@jklymak jklymak closed this Oct 1, 2021
@QuLogic QuLogic removed this from the v3.6.0 milestone Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clone more legend settings when regenerating in Qt figure options dialog
7 participants