Change font size in a Seaborn plot in Python
This tutorial will guide you on changing font size in the seaborn plot in Python.
Here, we will learn about how to change the font size of elements all at once and change the font size of elements individually. It is quite simple and you have to follow the following steps according to your need.
Basic graph plotting in Seaborn
First, you should know how to plot a graph using seaborn and the basic functions of matplotlib library.
import matplotlib.pyplot as plt import seaborn as sns x=['jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec'] y=[1,2,3,4,5,6,7,8,9,10,11,12] a=sns.barplot(x,y) plt.title('monthly entries') plt.xlabel('month') plt.ylabel('entries') plt.show()
Changing the font size of elements all at once
Using matplotlib.pyplot.set()
function from matplotlib library.
For this, we use:
sns.set(font_scale=__)
where the default value of font_scale
is 1. Here, we are changing all the texts which include the x-axis label, y-axis label, chart title, and values written on the x and y axis altogether.
Input:
import matplotlib.pyplot as plt import seaborn as sns x=['jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec'] y=[1,2,3,4,5,6,7,8,9,10,11,12] a=sns.barplot(x,y) plt.title('monthly entries') plt.xlabel('month') plt.ylabel('entries') sns.set(font_scale=1.25) plt.show()
Output:
Changing the font size of elements individually
For this, we use:
sns.the-required-element-to-be-changed('the-title-of-the-element',fontsize=__)
where the default value of fontsize
is 10.
Suppose, if we want to change just the font size of the title of the plot, x-label, or y-label separately we can use the following code given below.
Input:
import matplotlib.pyplot as plt import seaborn as sns x=['jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec'] y=[1,2,3,4,5,6,7,8,9,10,11,12] a=sns.barplot(x,y) plt.title('monthly entries',fontsize=40) plt.xlabel('month',fontsize=20) plt.ylabel('entries',fontsize=20) plt.show()
Leave a Reply