Home > Software engineering >  How to change the palette-legend in seaborn pairplot
How to change the palette-legend in seaborn pairplot

Time:12-03

I've just learned that I can change axis-label font-size using sns.set_context. Is there an analogous way to change the content and size of the text in the 'palette-legend' on the right?

enter image description here

I'd like to enlarge the text and relabel the '0' and '1', which were used for matrix manipulation, back to descriptive text.

CodePudding user response:

You can use set_title() and set_text() to set the names of the legend title & labels. Similarly, use plt.setp() to change the font to the size you need it to be... an example is shown below.

penguins = sns.load_dataset("penguins")
g=sns.pairplot(penguins, hue="species")

g._legend.set_title("New Title") ## Change text of Title
new_labels = ['Label 1', 'Label 2', 'Label 3']
for t, l in zip(g._legend.texts, new_labels):
    t.set_text(l)  ## Change text of labels
    
plt.setp(g._legend.get_title(), fontsize=30) ## Set the Title font to 30
plt.setp(g._legend.get_texts(), fontsize=20) ## Set the label font to 20

plt.show()

enter image description here

  • Related