How to Change Font Size in Seaborn Plot
-
Use the
seaborn.set()
Function to Set Font Size in Seaborn Plot -
Use the
fontsize
Parameter to Alter Font Size in Seaborn Plot
In this tutorial, we will discuss how to alter the font size in seaborn plots.
Use the seaborn.set()
Function to Set Font Size in Seaborn Plot
We can change the configurations and theme of a seaborn plot using the seaborn.set()
function. To set the font size, we use the font_scale
parameter in this function. This parameter automatically alters the font of everything in the graph, from the legend to both the axis labels and everything.
See the code below to understand its use.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame(
{"Day 1": [7, 1, 5, 6, 3, 10, 5, 8], "Day 2": [1, 2, 8, 4, 3, 9, 5, 2]}
)
sns.set(font_scale=2)
p = sns.lineplot(data=df)
p.set_xlabel("X-Axis")
p.set_ylabel("Y-Axis")
p.set_title("Plot")
plt.legend(labels=["Legend_Day1", "Legend_Day2"])
If nothing is specified in the function, then it sets everything to the default value. Notice the use of different functions to add different elements and how the set()
function changes the size of everything at once.
Use the fontsize
Parameter to Alter Font Size in Seaborn Plot
At times, we use different functions to include different elements in a plot. We can alter the size of such elements as the labels of the axes, the legend, and the title by setting the fontsize
parameter to its desired value in their respected functions.
The following code will explain this.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
{"Day 1": [7, 1, 5, 6, 3, 10, 5, 8], "Day 2": [1, 2, 8, 4, 3, 9, 5, 2]}
)
sns.set()
p = sns.lineplot(data=df)
p.set_xlabel("X-Axis", fontsize=20)
p.set_ylabel("Y-Axis", fontsize=20)
p.set_title("Plot", fontsize=20)
plt.legend(labels=["Legend_Day1", "Legend_Day2"], fontsize=20)
As discussed in the previous method, the sns.set()
function is used before the plot to set everything to default.
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.
LinkedIn