Change Seaborn Plot Size

  1. Use the seaborn.set() Function to Change the Size of a Seaborn Plot
  2. Use the rcParams Function to Change the Size of a Seaborn Plot
  3. Use the matplotlib.pyplot.figure() Function to Change the Size of a Seaborn Plot
  4. Use the matplotlib.pyplot.gcf() Function to Alter the Size of a Seaborn Plot
  5. Use the height and aspect Parameters to Alter the Size of a Seaborn Plot

Usually, plots and figures have some default size, or their dimensions are determined automatically by the compiler.

In this tutorial, we will discuss how to change the size of a seaborn plot in Python.

Use the seaborn.set() Function to Change the Size of a Seaborn Plot

The seaborn.set() function is used to control the theme and configurations of the seaborn plot.

The rc parameter of the function can be used to control the size of the final figure. We will pass a dictionary as the value to this parameter with the key as figure.figsize and the required dimensions as the value.

See the following code.

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(rc = {'figure.figsize':(15,8)})
p = sns.lineplot(data = df)

Use the rcParams Function to Change the Size of a Seaborn Plot

Similar to the seaborn.set() function, the rcParams in the matplotlin.pyplot module is used to control the style of the plot. We can use the figure.figsize parameter here to change the size of the figure.

For example,

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]})

from matplotlib import rcParams

rcParams['figure.figsize'] = 15,8
p = sns.lineplot(data = df)

Use the matplotlib.pyplot.figure() Function to Change the Size of a Seaborn Plot

The matplotlib.pyplot.figure() function is used to activate a figure. We can use it before plotting the required seaborn plot. To change the size of the plot, we can use the figsize parameter and give it the desired value for height and width.

For example,

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]})


plt.figure(figsize = (15,8))
p = sns.lineplot(data = df)

Use the matplotlib.pyplot.gcf() Function to Alter the Size of a Seaborn Plot

The matplotlib.pyplot.gcf() function is used to get an instance of the current figure. We can use the set_size_inches() method with this instance to alter the final size of the plot.

This method works for Facetgrid type objects also.

For example,

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]})


p = sns.lineplot(data = df)
plt.gcf().set_size_inches(15, 8)

Use the height and aspect Parameters to Alter the Size of a Seaborn Plot

Different plots in the seaborn module like lmplot, catplot, factorplot, jointplot already have the parameters height and aspect to control the size of the plotted figure.

The following code shows how to use these parameters.

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]})


p = sns.factorplot(data = df,height=8, aspect=15/8)
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.