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

introduce set_legend_title() and make legend object available via get_legend_obj() #3214

Closed
wants to merge 4 commits into from

Conversation

berndreinhold
Copy link

Introduced set_legend_title() which modifies the legend title without having to recreate the legend as in move_legend() - it is thereby a bit more lightweight. It also takes a prop-argument to modify e.g. title fonts.

For more elaborate modifications get_legend_obj() can be used to get a legend object. get_legend_obj() contains an extraction of code found within move_legend(), so that it can be called both from move_legend() and set_legend_title() or by itself.

This is related to:

Introduced tests for both functions, modeled after test_move_legend*()
"make tests" gave very similar results compared to results before the modification:
1943 passed, 14 skipped, 7 xfailed, 652 warnings

…he more expensive move_legend(). get_legend_obj() extracted from move_legend()
…, so that it becomes available as a module function.
…e lines of move_legend(). Changed test_move_legend_input_checks() to test_get_legend_obj_input_checks(), since this common code was extracted from move_legend() into get_legend_obj()
@mwaskom
Copy link
Owner

mwaskom commented Jan 6, 2023

Hi, I don't see how this addresses the two linked issues, which have to do with the Plot interface. Modification of legend titles on existing matplotlib objects is already relatively straightforward, e.g.

ax = sns.histplot(penguins, x="bill_length_mm", hue="species")
ax.legend_.set_title("Kind of penguin")

image

@berndreinhold
Copy link
Author

Thanks for pointing out the accessibility of legend for axes (and FacetGrid). And I had missed that the reference to Plot, was specifically to https://seaborn.pydata.org/generated/seaborn.objects.Plot.html. This renders this PR obsolete.

@mwaskom
Copy link
Owner

mwaskom commented Jan 6, 2023

Also while the Plot legend customizability remains lacking in general, it's actually already possible to control the legend title through Plot.label:

(
    so.Plot(penguins, x="bill_length_mm", color="species")
    .add(so.Bars(), so.Hist())
    .label(color="Kind of penguin")
)

@berndreinhold
Copy link
Author

Thanks for your quick answers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants