90

I have plotted my data with factorplot in seaborn and get facetgrid object, but still cannot understand how the following attributes could be set in such a plot:

  1. Legend size: when I plot lots of variables, I get very small legends, with small fonts.
  2. Font sizes of y and x labels (a similar problem as above)
mwaskom
  • 46,693
  • 16
  • 125
  • 127
james pardon
  • 903
  • 1
  • 6
  • 4
  • 3
    The responses below are correct and should help you out, but I would say it sounds like you're plotting in the IPython notebook and so the issue isn't really anything to do with seaborn but that the notebook scales down figures if they end up larger than the div allotted to them. If you save the figure, everything should be the size that you expect. – mwaskom Dec 31 '14 at 00:32
  • 1
    Also if you are plotting a lot of variables on the columns and don't have a row variable, you could use `col_wrap` to "wrap" the facets onto multiple rows, which might help. – mwaskom Dec 31 '14 at 00:32

5 Answers5

146

You can scale up the fonts in your call to sns.set().

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.normal(size=37)
y = np.random.lognormal(size=37)

# defaults
sns.set()
fig, ax = plt.subplots()
ax.plot(x, y, marker='s', linestyle='none', label='small')
ax.legend(loc='upper left', bbox_to_anchor=(0, 1.1))

enter image description here

sns.set(font_scale=5)  # crazy big
fig, ax = plt.subplots()
ax.plot(x, y, marker='s', linestyle='none', label='big')
ax.legend(loc='upper left', bbox_to_anchor=(0, 1.3))

enter image description here

Paul H
  • 65,268
  • 20
  • 159
  • 136
  • Size is relative to the default matplotlib font size. For me it was better to change the font size right there with plt.rc() calls at the top of my script or Jupyter notebook. See https://stackoverflow.com/questions/3899980/how-to-change-the-font-size-on-a-matplotlib-plot – fviktor Sep 13 '17 at 18:50
  • 6
    This seems to override the chosen theme. – CookieMaster Feb 08 '21 at 19:24
  • 1
    This is bad because it changes the figure layout completely, not just the font. – SomJura Jul 27 '21 at 16:54
  • @SomJura if you have a fixed image size, your choices are to: a) adjust the layout so they fit or b) let them be cutoff by the figure boundaries. So not just "bad" but also, "necessary" and "practical" – Paul H Jul 27 '21 at 19:51
  • 1
    @PaulH I agree with this. The issue is rather that some more parameters are changed silently, under the hood, when `sns.set()` is invoked. For example the background colour of the plotting area is changed to seaborn default. @CookieMaster mentions something similar. – SomJura Jul 27 '21 at 22:57
51

The FacetGrid plot does produce pretty small labels. While @paul-h has described the use of sns.set as a way to the change the font scaling, it may not be the optimal solution since it will change the font_scale setting for all plots.

You could use the seaborn.plotting_context to change the settings for just the current plot:

with sns.plotting_context(font_scale=1.5):
    sns.factorplot(x, y ...)
achennu
  • 1,538
  • 1
  • 13
  • 18
  • 28
    @mwaskom says " `font_scale` is ignored if context is `None`, so you want `plotting_context("notebook", font_scale=5.5)` or similar." (http://stackoverflow.com/a/31865996/1135316) – Chris Warth Aug 07 '15 at 17:20
17

I've made some modifications to @paul-H code, such that you can independently set the font size for the x/y axes and legend:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.normal(size=37)
y = np.random.lognormal(size=37)

# defaults                                                                                                         
sns.set()
fig, ax = plt.subplots()
ax.plot(x, y, marker='s', linestyle='none', label='small')
ax.legend(loc='upper left', fontsize=20,bbox_to_anchor=(0, 1.1))
ax.set_xlabel('X_axi',fontsize=20);
ax.set_ylabel('Y_axis',fontsize=20);

plt.show()

This is the output:

enter image description here

Sergio Cobo
  • 479
  • 6
  • 13
9

For the legend, you can use this

plt.setp(g._legend.get_title(), fontsize=20)

Where g is your facetgrid object returned after you call the function making it.

Hielke Walinga
  • 2,677
  • 1
  • 17
  • 30
2

This worked for me

g = sns.catplot(x="X Axis", hue="Class", kind="count", legend=False, data=df, height=5, aspect=7/4)
g.ax.set_xlabel("",fontsize=30)
g.ax.set_ylabel("Count",fontsize=20)
g.ax.tick_params(labelsize=15)

What did not work was to call set_xlabel directly on g like g.set_xlabel() (then I got a "Facetgrid has no set_xlabel" method error)

phi
  • 550
  • 8
  • 11