2

I am using the following code to make a seaborn countplot:

sns.countplot(y="YearBuilt", data=df_bin)

This makes a perfectly normal countplot. The problem is, when I try making multiple subplots, the font size becomes too small, so I have to change it using this line of code:

plt.rcParams.update({'font.size': 37})

It works fine, but then every time I want to make just one plot, the fontsize is way too big, so I have to change it again. I can use this code to make the font size bigger for each plot manually:

plt.xlabel("count", size=37)
plt.ylabel("YearBuilt", size=37)

Is there no other way to change the font size of each plot, because one of the default labels for seaborn countplots is "count", so I don't think it really makes sense to reassign it just to change the fontsize. And even if I do this, the xticks and yticks are still too small.

Yusuf Saad
  • 83
  • 9
  • https://seaborn.pydata.org/tutorial/aesthetics.html#overriding-elements-of-the-seaborn-styles – Paul H Jan 11 '23 at 15:31
  • 1
    https://stackoverflow.com/questions/25328003/how-can-i-change-the-font-size-using-seaborn-facetgrid/25394017#25394017 – Paul H Jan 11 '23 at 15:33
  • Thanks, @Paul H. I tried the seaborn way but it doesn't seem to do anything. However, the one on stack overflow works. – Yusuf Saad Jan 11 '23 at 15:49

1 Answers1

0

You can actually use a context manager from matplotlib.pyplot module to temporarily change the font size for all elements of the plot, and then restore the original fonts size after the plot is created. This can help avoid manually changing the font size for each individual element.

import matplotlib.pyplot as plt
with plt.rc_context({"font-size":37}):
   sns.countplot(y="YearBuilt", data=df_bin)

So this will set the font size to 37 for all the elements of the plot, including the xlabel, ylabel, xticks, and yticks. After the countplot is created, the original font size will be restored, so the font size will be the default again for the next plot.

Paul H
  • 65,268
  • 20
  • 159
  • 136