How do we adjust the size of the plot in Seaborn?


The size of a plot refers to its width and height in units such as inches or centimeters. By adjusting the plot size, we can control how much space it occupies on the screen or in printed media. Seaborn provides several options for modifying the plot size to suit our needs.

Seaborn does not have a specific parameter called "figure size" in its API. When using Seaborn to create plots, we can specify the figure size by directly manipulating the Figure object using Matplotlib's functions or parameters.

Importing Required Libraries

Before proceeding with the approaches, we have to make sure whether the required libraries are installed in our python working environment. Once installed we have to import them by using the below line of code.

import seaborn as sns
import matplotlib.pyplot as plt

There are multiple approaches of adjusting the size of the plot in seaborn. Let’s see each approach in detail.

Adjusting the Size Using Matplotlib's Figure

Seaborn plots are created using Matplotlib's Figure and Axes objects. To adjust the plot size, we can directly manipulate the Figure object.

Example

In this example, we use 'plt.figure(figsize=(8, 6))' to create a Figure object with a width of 8 inches and a height of 6 inches. This sets the desired size of the plot.

import seaborn as sns
import matplotlib.pyplot as plt
# Create a figure with a specific size
plt.figure(figsize=(8, 6))
x = [1,22,10]
y = [4,56,67]
# Create a plot using Seaborn
sns.scatterplot(x)
plt.show()

Output

Adjusting the Size Using Seaborn's Function

Seaborn provides a convenient function called 'set_context()' that allows us to adjust the plot's overall style, including the size. The 'set_context()' function has a parameter called 'rc' that accepts a dictionary of Matplotlib parameters. We can specify the size of the plot using the 'figure.figsize' parameter.

Example

Here in this example, we are using 'sns.set_context("paper", rc={"figure.figsize": (8, 6)})' to set the plot context to "paper" and specify the desired size as (8, 6) inches. The plot created afterwards will reflect with this updated context.

import seaborn as sns
import matplotlib.pyplot as plt
# Create a figure with a specific size
plt.figure(figsize=(8, 6))
x = [1,22,10]
y = [4,56,67]
# Create a plot using Seaborn
sns.scatterplot(x)
# Set the context with a specific size
sns.set_context("paper", rc={"figure.figsize": (8, 6)})
# Create a plot using Seaborn
sns.scatterplot(x=x)
plt.show()

Output

Adjusting the Size Using Matplotlib's rcParams

Matplotlib has a set of global parameters called 'rcParams' which control various aspects of plot appearance. We can modify the 'figure.figsize' parameter in 'rcParams' to adjust the size of plots in Seaborn.

Example

In this example, 'plt.rcParams["figure.figsize"] = (8, 6)' sets the global parameter 'figure.figsize' to (8, 6) inches, which affects the size of subsequent plots created using Seaborn.

import seaborn as sns
import matplotlib.pyplot as plt
# Create a figure with a specific size
plt.figure(figsize=(3, 3))
y = [4,56,67]
# Create a plot using Seaborn
sns.scatterplot(y)
# Set the figure size using rcParams
plt.rcParams["figure.figsize"] = (600, 400)
# Create a plot using Seaborn
sns.scatterplot(y=y)
plt.show()

Output

Adjusting the Size of Subplots

If we have multiple subplots within a figure, we can control their individual sizes using the 'subplots()' function in Matplotlib.

Example

In this example, we are using 'fig, axes = plt.subplots(2, 2, figsize=(10, 8))' to create a figure with a 2x2 grid of subplots. The 'figsize' parameter specifies the overall size of the figure, while the individual subplots can be accessed through the 'axes' object and assigned specific plots.

# Create a figure with multiple subplots
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
# Create plots using Seaborn
sns.scatterplot(x=x, y=y, ax=axes[0, 0])
sns.histplot(data=x, ax=axes[0, 1])
sns.lineplot(data=y, ax=axes[1, 0])
sns.boxplot(data=y, ax=axes[1, 1])
plt.show()

Output

Saving the Plot with Desired Size

Once we have adjusted the size of our plot, we can save it to a file using the 'savefig()' function from Matplotlib, ensuring that the saved plot retains the desired size.

Example

In this example 'plt.savefig("output.png", dpi=100, bbox_inches="tight")' is used to save the plot as an image file. The 'dpi' parameter specifies the resolution, and 'bbox_inches="tight"' ensures that the saved plot includes the entire figure, without cropping any parts.

# Set the figure size
plt.figure(figsize=(6, 4))
# Create a plot using Seaborn
sns.scatterplot(x=x, y=y)
# Save the plot with desired size
plt.savefig("output.png", dpi=100, bbox_inches="tight")
plt.show()

Output

Updated on: 02-Aug-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements