Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add public method to update Legend object's loc property . #26089

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

Merged
merged 1 commit into from
Jun 16, 2023

Conversation

cocolato
Copy link
Contributor

@cocolato cocolato commented Jun 8, 2023

PR summary

Add public method to update Legend object's loc property .

Example:

from matplotlib import pyplot as plt
from matplotlib.legend import Legend


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

x = [-10.0, -9.657349547286204, -9.318462608835684, -9.031177432527166, -8.691618609025815, -8.407140700722843, -8.152708015644635, -7.839130676473357, -7.499034134688037, -7.172556788526309, -6.847257574849716, -6.552316320455642, -6.230727469453974, -5.914856113060868]
y = [4.5397868702434395e-05, 6.394971420131934e-05, 8.974373333525978e-05, 0.00011960725629360318, 0.00016795968412322188, 0.000223217496066253, 0.00028787162356623547, 0.00039385623135828983, 0.0005533125089980317, 0.0007667698609716984, 0.0010612377365216156, 0.0014247739486663552, 0.001964154207369101, 0.002691782877150404]
ax.plot(x, y, label="f(x)")
if ax.legend():
    # Now
    ax.get_legend().set_loc("right")

    # Or
    # ax.get_legend().set(loc="right")
    
    # Before

    # _loc_code = Legend.codes["right"] 
    # ax.get_legend()._set_loc(_loc_code)
    
plt.show()

PR checklist

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@cocolato cocolato force-pushed the feat/add_pub_set_loc4legend branch from e987305 to 1919d2e Compare June 14, 2023 13:40
@cocolato cocolato requested review from tacaswell and ksunden June 14, 2023 15:33
@cocolato cocolato force-pushed the feat/add_pub_set_loc4legend branch from 662d937 to 7132ab0 Compare June 15, 2023 10:01
@cocolato cocolato requested a review from ksunden June 15, 2023 15:21
@ksunden ksunden dismissed their stale review June 15, 2023 17:41

changes mad

Copy link
Member

@ksunden ksunden left a comment

Choose a reason for hiding this comment

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

I was slightly wondering about the _loc_used_default flag and whether that handling should be kept to __init__ or not. I ended up coming to the conclusion that being implemented here is probably fine.

As an aside:

That said, not introduced here, but I have some questions about handling of that variable, as for figure legends it will default to "upper right", but the usage of that variable is for a warning about "best" being potentially slow. I mean, the warning also requires a second to elapse, which I expect 99+% of the time won't be a problem for figure legends, but still a bit incongruous (but not new here, so not blocking).

I was also wondering about the set_offset=False behavior, but in running it it worked as expected, so unless proven otherwise I think it is fine.

@cocolato
Copy link
Contributor Author

cocolato commented Jun 16, 2023

set_offset=False becasue self._legend_box.set_offset must be called after self._init_legend_box, currently set_offset is done in __init__:

https://github.com/cocolato/matplotlib/blob/7132ab05a2037aea5f7d72aa9035e3042bdce10e/lib/matplotlib/legend.py#L556-L557

@ksunden
Copy link
Member

ksunden commented Jun 16, 2023

Yes, but after init when updating the location, as it is a public method to do this, not sure "don't update offset" is actually expected.

Perhaps it could be retained, but calling set_loc moved to after _init_legend_box (not sure, its a bit of a bootstrapping situation)?

@cocolato
Copy link
Contributor Author

Now I've removed the set_offset=False and move the set_loc after _init_legend_box!

@rcomer
Copy link
Member

rcomer commented Jun 16, 2023

Thanks @cocolato I think this is a really nice addition. I think it should have a "what's new" entry, and also a versionadded directive in the set_loc docstring. For details see:
https://matplotlib.org/devdocs/devel/coding_guide.html#new-features-and-api-changes

@rcomer
Copy link
Member

rcomer commented Jun 16, 2023

If you are confident with git, it would also be good if you could squash all the commits down to one, just to tidy up the history.
https://matplotlib.org/devdocs/devel/development_workflow.html#rewriting-commit-history

@cocolato cocolato force-pushed the feat/add_pub_set_loc4legend branch 3 times, most recently from 5196633 to a2ec436 Compare June 16, 2023 17:42
@rcomer rcomer added this to the v3.8.0 milestone Jun 16, 2023
@cocolato cocolato force-pushed the feat/add_pub_set_loc4legend branch from 1d5dc65 to 5aca651 Compare June 16, 2023 18:42
Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

Thanks @cocolato this looks good to me. Because there have been some changes since @ksunden’s approval I will leave this for him to check and merge if he’s still happy.

@ksunden ksunden merged commit 12b38a3 into matplotlib:main Jun 16, 2023
@ksunden
Copy link
Member

ksunden commented Jun 16, 2023

Congrats, @cocolato, hope to hear from you again!

@cocolato cocolato deleted the feat/add_pub_set_loc4legend branch June 19, 2023 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[ENH]: Add public method to update Legend object's loc property .
5 participants