6

I am using the new seaborn.objects API available in v0.12.0. None of the examples show how to move the legend position. For example, moving the legend to the bottom. Is there a way to do this?

Using sns.move_legend, as shown in this answer results in a TypeError.

import seaborn as sns
import seaborn.objects as so

healthexp = sns.load_dataset('healthexp')

p = so.Plot(healthexp, "Year", "Spending_USD", color="Country")
p.add(so.Line())
sns.move_legend(p, "upper left", bbox_to_anchor=(.55, .45), title='Species')

Traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [24], in <cell line: 3>()
      1 p = so.Plot(healthexp, "Year", "Spending_USD", color="Country")
      2 p.add(so.Line())
----> 3 sns.move_legend(p, "upper left", bbox_to_anchor=(.55, .45), title='Species')

File ~\anaconda3\envs\py10\lib\site-packages\seaborn\utils.py:444, in move_legend(obj, loc, **kwargs)
    442 else:
    443     err = "`obj` must be a seaborn Grid or matplotlib Axes or Figure instance."
--> 444     raise TypeError(err)
    446 if old_legend is None:
    447     err = f"{obj} has no legend attached."

TypeError: `obj` must be a seaborn Grid or matplotlib Axes or Figure instance.
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Abiel
  • 5,251
  • 9
  • 54
  • 74
  • 5
    the comment by r-beginners unfortunately has nothing to do with the new `seaborn.objects` system – Jaleks Nov 07 '22 at 17:28
  • duplicate: https://stackoverflow.com/questions/73633322/how-can-i-customize-the-legend-with-seaborn-0-12-objects – Ruggero Turra Feb 01 '23 at 17:26
  • Does this answer your question? [How can I customize the legend with Seaborn 0.12 objects?](https://stackoverflow.com/questions/73633322/how-can-i-customize-the-legend-with-seaborn-0-12-objects) – Ruggero Turra Feb 01 '23 at 17:26

0 Answers0