In Seaborn, we can adjust the font size of the labels in a plot with the help of the sns.set()
function. However, the font_scale
parameter of the sns.set()
function scales the font size of all the elements in the plot, including tick labels, axes labels, titles, and legends, rather than selectively adjusting only the labels. To specifically modify the font size of the axes labels, i.e., x-axis and y-axis labels, we can utilize the functions from the matplotlib
such as set_xlabel()
, set_ylabel()
, plt.xlabel()
, and plt.ylabel()
to set the font size of the labels.
Note: The
fontsize
parameter ofset_xlabel()
sets the label font size for selected plots in a figure, while inplt.xlabel()
, it sets font size globally for the whole figure.
We’ll use the following syntax to change the label font size:
# Using the font_scale parametersns.set(font_scale=1.0)# Using Matplotlib to change specifically label font sizes using plt.xlabel()plt.xlabel("sepal_length", fontsize=20)plt.ylabel("petal_length", fontsize=20)# Using Matplotlib to change specifically label font sizes using set_xlabel()ax.set_xlabel("sepal_length", fontsize=20)ax.set_ylabel("petal_length", fontsize=20)
The default value of font_scale
is
Note: The
sns
function is an alias of Seaborn.
We'll plot scatterplot
for a sample dataset provided by Seaborn and will analyze the working of font_scale
parameter of sns.set()
function. First, let's look at a plot without any font settings:
# Importing librariesimport seaborn as snsimport matplotlib.pyplot as plt# Loading the iris datasetiris_df = sns.load_dataset("iris")# Setting the figure size but not the font sizesns.set(rc = {'figure.figsize':(20,8)})# To Do# Plotting the scatterplotsns.scatterplot(x="sepal_length", y="petal_length", data=iris_df)# show plotplt.show()
As we can see, the labels are not legible because we have not yet set the font size. Once we use font_scale
we will notice the clear difference in both plots:
# Importing librariesimport seaborn as snsimport matplotlib.pyplot as plt# Loading the iris datasetiris_df = sns.load_dataset("iris")# Setting the figure sizesns.set(rc = {'figure.figsize':(20,10)})# Setting the font size by 2.5sns.set(font_scale=2.5)# Plotting the scatterplotsns.scatterplot(x="sepal_length", y="petal_length", data=iris_df)# Showing plotplt.show()
The text that was illegible because of the small font size has now become legible by using sns.set(font_scale=2.5)
. However, the text, in addition to the x-axis and y-axis label, has also increased in size. Let's see an example where we are using the set_xlabel()
and set_ylabel()
, and plt.xlabel()
and plt.ylabel()
to only increase the labels font sizes:
# Importing librariesimport seaborn as snsimport matplotlib.pyplot as plt# Loading the iris datasetiris_df = sns.load_dataset("iris")# Setting the figure sizesns.set(rc = {'figure.figsize':(20,8)})# Setting the font size by 2.5# sns.set(font_scale=2.5)# Plotting the scatterplotax = sns.scatterplot(x="sepal_length", y="petal_length", data=iris_df)# Increasing the font size# Uncommenting the following lines to see usage of plt.xlabel()# plt.xlabel("sepal_length", fontsize=40)# plt.ylabel("petal_length", fontsize=40)# Increasing the font size using set_xlabel()ax.set_xlabel("sepal_length", fontsize=40)ax.set_ylabel("petal_length", fontsize=40)# Showing the plotplt.show()
Here is a line-by-line explanation of the code above:
Lines 2–3: We import the seaborn
and matplotlib
libraries.
Line 12: We set the font size of the labels by giving a positive float value to the font_scale
parameter.
Line 15: We draw the scatterplot of the iris dataset using the sns.scatterplot()
function.
Lines 19–20: We can increase the font size using the plt.xlabel()
and plt.ylabel()
functions. Here, it is commented but we can uncomment it to check its utility.
Lines 23–24: We are using set_xlabel()
and set_ylabel()
to increase the font size to
Line 27: We display the plot using the plt.show()
function.
Free Resources