0

I want to change the section headings of my plot legend. For the example below they read: 'smoker' and 'time' but let's say I want them to read 'Smoker' and 'Time of Day' respectively.

import matplotlib.pyplot as plt
import seaborn as sns

tips=sns.load_dataset('tips')

sns.relplot(
    data=tips,
    x='total_bill', y='tip', hue='smoker', style='time'
)

plt.show()

Any tips?

Edits

Thanks for linking: How to edit a seaborn legend title and labels for figure-level functions

I had only skimmed through it. After spending some more time with it, it still hasn't answered my question. The main difference being I want to change the subtitle values within a legend, rather than the legend title or the label values.

import matplotlib.pyplot as plt
import seaborn as sns

tips=sns.load_dataset('tips')

g = sns.relplot(
    data=tips,
    x='total_bill', y='tip', hue='smoker', style='time',
    facet_kws={'legend_out': True}
)

# can only set the title of the entire legend, not the subtitles
g._legend.set_title('Legend')
plt.show()

If a diagram is more helpful:

enter image description here

red = what I want to change (not answered by How to edit a seaborn legend title and labels for figure-level functions)
yellow = what is answered by How to edit a seaborn legend title and labels for figure-level functions

dcurrie27
  • 319
  • 3
  • 14
  • Change the values in the columns with `.map` – Trenton McKinney Sep 06 '22 at 16:16
  • 1
    In regards to the edit, the easiest option is to rename the columns, since that's where the name comes from. https://stackoverflow.com/q/11346283/7758804 – Trenton McKinney Sep 06 '22 at 17:14
  • 1
    `sns.relplot(data=tips.rename(columns={'smoker':'Smoker', 'time':'Time of Day'}), x='total_bill', y='tip', hue='Smoker', style='Time of Day')` is a fine workaround, yes. Thank you – dcurrie27 Sep 06 '22 at 17:27

0 Answers0