question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Legend position gets overwritten / can not be set

See original GitHub issue

When plotting with the new seaborn.objects Plot method including a legend (like .add(Line(), legend=True)), the position of the legend gets hardcoded to specific positions (https://github.com/mwaskom/seaborn/blob/master/seaborn/_core/plot.py#L1612-L1624), which differ depending on the usage of pyplot (legend gets pulled on top of the right side over the graph’s content) or not (e.g. in a Jupyter notebook, or on .save() ,the legend gets place to the right side outside of the plot graph area).

If the users want a different position of the legend (possible especially in the case of pyplot usage, where the legend may easily be generated over important data points), they have no possibility to move it to another place. The old (=not seaborn.objects) method move_legend() does not work for Plot objects, neither can the seaborn objects .theme() approach be used, with setting matplotlib rc params (which would be legend.loc in conjunction with bbox_to_anchor of the matplotlib legend), because the loc is hardcoded, and will such be overwritten, additionally there is no possibility at all for the users to set bbox_to_anchor for the legend.

The PR https://github.com/mwaskom/seaborn/pull/3128 solves this issue, through staying at the current default behavior, if no additional parameters are given, but respecting the matplotlib rc params entry for legend.loc if it is given like .theme({"legend.loc": "upper left"}). Additionally it gives the new possibility to set the bbox_to_anchor of the legend, like .theme({"legend_loc__bbox_to_anchor": (0.1, 0.95)}) (where the kind this is implemented might not be ideal).

This gives the users full control over the legend position.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jalsticommented, Nov 8, 2022

Sorry, thinking-typo, fixed to ‘pyplot’

0reactions
jalsticommented, Nov 11, 2022

NP, your welcome 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does legend('Position') overwrite strings? - MathWorks
When I try to reset the position of a legend with a legend('Position',...) command, the legend string changes to "Position".
Read more >
Legend overwritten by plot - matplotlib - Stack Overflow
I want to put labels for both the lineplot and the markers in red. However the legend is not appearning because its the...
Read more >
Controlling legend appearance in ggplot2 with override.aes
Changing the legend appearance without changing the plot appearance can be done using the override.aes argument in guide_legend().
Read more >
legend | highcharts API Reference
When proximate , the legend items will be placed as close as possible to the graphs they're representing, except in inverted charts or...
Read more >
legend - ApexCharts.js
Allows you to overwrite the default legend items with this customized set of labels. Please note that the click/hover events of the legend...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found