0

I am tyring to use seaborn for a pairplot, i want to:

  • Change x,y label size
  • Change legennd size
  • Change size of points
import seaborn as sns
sns.set_theme(style="ticks")

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

when using matplotlib you can do it with:

plt.legend(fontsize=20)

however it is creating a second legend for some reason: second legend from plt.legend

EDIT

Using

import seaborn as sns
import matplotlib.pylab as plt
sns.set_theme(style="ticks")

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

plt.setp(ax.get_legend().get_texts(), fontsize='22') # for legend text
plt.setp(ax.get_legend().get_title(), fontsize='32') # for legend title

plt.show()

Does not work:

Out:

AttributeError                            Traceback (most recent call last)
<ipython-input-72-6ed6c69c9141> in <module>()
      6 ax=sns.pairplot(df, hue="species")
      7 
----> 8 plt.setp(ax.get_legend().get_texts(), fontsize='22') # for legend text
      9 plt.setp(ax.get_legend().get_title(), fontsize='32') # for legend title
     10 

AttributeError: 'PairGrid' object has no attribute 'get_legend'
Leo
  • 1,176
  • 1
  • 13
  • 33

1 Answers1

1

Your second attempt is closer. As mentioned in this comment, something like the following:

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

plt.setp(g._legend.get_title(), fontsize=20)
plt.setp(g._legend.get_texts(), fontsize=20)
BigBen
  • 46,229
  • 7
  • 24
  • 40
  • Ok, great it works, and can this format be used for the axis labels? i am guesing it is something like: plt.setp(g.axis.get_texts(), fontsize=20) – Leo Oct 15 '20 at 13:32
  • Perhaps [this approach](https://stackoverflow.com/questions/36573789/python-seaborn-facetgrid-change-xlabels). – BigBen Oct 15 '20 at 13:33
  • Ok great, i guess im going to use something other than seaborn as the code becomes complicated if i have to select each label individually. – Leo Oct 15 '20 at 13:45
  • @David8 - [another option](https://stackoverflow.com/questions/31864770/change-font-size-of-facet-titles-using-seaborn-facetgrid-heatmap) and [another](https://stackoverflow.com/a/27714134/9245853)... Not a seaborn expert by any means myself, so learning as I go. Ignore my previous comment. – BigBen Oct 15 '20 at 13:52