How to change the figure size in seaborn
Seaborn is a Python visualization library built on top of matplotlib that offers a high-level interface for creating visually appealing and statistical visuals. We will use seaborn to change the figure size of a plot.
Syntax
To change the figure size in seaborn, the following syntax is used:
width = 20
height = 8
sns.set(rc = {'figure.figsize':(width, height)})
Here, sns.set() is being used to set the figure size of the plot. Although it can also be used to set the other properties of the plot.
Note:
snsis an alias of seaborn
Code example
Let’s create a scatter plot of tips dataset by loading it from seaborn. The width and height are currently set as and , respectively. But these values can be changed according to the required size of the figure. Try different sizes of the figure by changing the width and height in the following code:
# importing librariesimport seaborn as snsimport matplotlib.pyplot as plt# Load tips datasettips = sns.load_dataset("tips")# setting the figure sizewidth = 20height = 8sns.set(rc = {'figure.figsize':(width,height)})# setting the font of plotsns.set(font_scale=1.5)# plotting the scatterplotsns.scatterplot(x="total_bill", y="tip", data=tips)# show plotplt.show()
Code explanation
Here is a line-by-line explanation of the code above:
-
Lines 9–11: We set the size of the figure by using
sns.set(). Here,widthandheightis set as and , respectively. -
Line 14: We draw the scatter plot of the
tipsdataset using thesns.scatterplot()function. -
Line 17: The
plt.show()function is used to display the plot.
Free Resources