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

Add a move_legend convenience function #2643

Merged
merged 3 commits into from
Aug 15, 2021
Merged

Add a move_legend convenience function #2643

merged 3 commits into from
Aug 15, 2021

Conversation

mwaskom
Copy link
Owner

@mwaskom mwaskom commented Aug 15, 2021

This addresses issues discussed in #2280, along with some of the issues in #2231

It is a somewhat hack-ish solution. Because matplotlib legends don't offer public
control over their location, this copies data from an existing legend to a new
object, and then removes the original legend. I am hopeful that there will be
upstream changes that make legend repositioning more natural, but this is
a reasonable stopgap measure to alleviate a common seaborn pain-point.

It is also in a sense more powerful than the name suggests, because it lets one
update other legend parameters, which is also not otherwise possible with
public methods on an existing legend object.

Example in action:

ax = sns.histplot(penguins, x="bill_length_mm", hue="species")
sns.move_legend(ax, "lower center", bbox_to_anchor=(.5, 1), ncol=3)

image

This addresses issues discussed in #2280, along with some of the issues in #2231

It is a somewhat hack-ish solution. Because matplotlib legends don't offer public
control over their location, this copies data from an existing legend to a new
object, and then removes the original legend. I am hopeful that there will be
upstream changes that make legend repositioning more natural, but this is
a reasonable stopgap measure to alleviate a common seaborn pain-point.
@mwaskom mwaskom added this to the v0.11.2 milestone Aug 15, 2021
@codecov
Copy link

codecov bot commented Aug 15, 2021

Codecov Report

Merging #2643 (57a08c9) into master (091f4c0) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head 57a08c9 differs from pull request most recent head 85e6947. Consider uploading reports for the commit 85e6947 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2643      +/-   ##
==========================================
+ Coverage   97.51%   97.53%   +0.01%     
==========================================
  Files          17       17              
  Lines        6402     6439      +37     
==========================================
+ Hits         6243     6280      +37     
  Misses        159      159              
Impacted Files Coverage Δ
seaborn/utils.py 95.20% <100.00%> (+0.59%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 091f4c0...85e6947. Read the comment docs.

@mwaskom mwaskom merged commit f4a5076 into master Aug 15, 2021
@mwaskom mwaskom deleted the move_legend branch August 15, 2021 19:41
mwaskom added a commit that referenced this pull request Aug 15, 2021
This addresses issues discussed in #2280, along with some of the issues in #2231

It is a somewhat hack-ish solution. Because matplotlib legends don't offer public
control over their location, this copies data from an existing legend to a new
object, and then removes the original legend. I am hopeful that there will be
upstream changes that make legend repositioning more natural, but this is
a reasonable stopgap measure to alleviate a common seaborn pain-point.

(cherry picked from commit f4a5076)
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.

1 participant