16

In seaborn, how can you change just the x and y axis label font size? Instead of using the "set context" method, is there a way to specifically change just the axis labels? Here is my code:

def corrfunc(x, y, **kws):

    r = stats.pearsonr(x, y)[0] ** 2
    ax = plt.gca()
    ax.annotate("r$^2$ = {:.2f}".format(r),
                xy=(.1, .9), xycoords=ax.transAxes, fontsize=16)
    if r > 0.6:
        col = 'g'
    elif r < 0.6:
        col = 'r'
    sns.regplot(x, y, color=col)
    return r

IC_Plot = sns.PairGrid(df_IC, palette=["red"])
IC_Plot.map_offdiag(corrfunc)

IC_Plot.savefig("Save_Pair.png")
Suresh Raja
  • 735
  • 2
  • 9
  • 19
  • Use either `plt.xlabel('text', fontsize=size)` or `ax.set_xlabel('text', fontdict={'fontsize' : size})`. – bnaecker Apr 28 '17 at 00:56
  • Thanks, this works but for some reason it does not change the font on the last pair plot in both the x and y direction. – Suresh Raja Apr 28 '17 at 01:36
  • There are multiple subplots? You'll need to call the above method for every axis you want it to apply to, and remember that Matplotlib subplot numbering counts from 1 (unlike everything else in Python...). – bnaecker Apr 28 '17 at 02:15
  • this worked for me: https://stackoverflow.com/a/27714134/4115369 – Yibo Yang Aug 29 '18 at 15:51

1 Answers1

30

The easiest way to change the fontsize of all x- and y- labels in a plot is to use the rcParams property "axes.labelsize" at the beginning of the script, e.g.

plt.rcParams["axes.labelsize"] = 15

You may also set the font size of each individual label

for ax in plt.gcf().axes:
    l = ax.get_xlabel()
    ax.set_xlabel(l, fontsize=15)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712