Explain Codes LogoExplain Codes Logo

How to change the figure size of a seaborn axes or figure level plot

python
matplotlib
seaborn
plotting
Alex KataevbyAlex Kataev·Nov 7, 2024
TLDR

Modify Seaborn plot size using plt.figure(figsize=(width, height)) for axes-level plots and height and aspect parameters for figure-level plots.

Axes-level example with sns.barplot:

import seaborn as sns import matplotlib.pyplot as plt plt.figure(figsize=(10, 6)) # Define figure size in inches sns.barplot(...)

Figure-level example with sns.catplot:

sns.catplot(..., height=5, aspect=2) # Set height and aspect ratio (width/height)

Quick setup for A4-sized plots

Getting ready for print? Here's how you set up your Seaborn plots for A4 paper size:

a4_dims = (11.7, 8.27) # A4 size dimensions in inches sns.set(rc={'figure.figsize':a4_dims}) # "A4" setting for all your plots

Or before specific plots for clarity and control:

fig, ax = plt.subplots(figsize=a4_dims) # Adjust size of your artboard sns.barplot(..., ax=ax) # Start painting 🎨

Customization using rcParams

Want to strut consistency over all your matplotlib plots? Befriend the rcParams dictionary:

from matplotlib import rcParams rcParams['figure.figsize'] = a4_dims # All subsequent plots will be this size

Persistently pleasing plots

Let's create a plot that fits perfectly to your ratio preferences:

aspect_ratio = 1.5 height_inches = 8 # Preferred height figsize = (height_inches * aspect_ratio, height_inches) plt.figure(figsize=figsize) # Presto! 🎩

Getting cold feet after creating the plot? No worries, you're in control:

fig = plt.gcf() # get current figure fig.set_size_inches(11.7, 8.27) # tweak until it's just right

Sizing figure-level plots

Dive deeper into Seaborn's figure-level functions like lmplot, catplot, and jointplot;

sns.catplot(..., size=4, aspect=2) # Size - the height of facet in inches, Aspect - ratio of width to height

But, if you seek absolute control over your plot sizes, let's turn to Matplotlib's methods:

g = sns.jointplot(...) g.fig.set_size_inches(11.7, 8.27) # Seaborn, meet Matplotlib 🤝

Post-customization

After sizing, let's trim artwork for some visual polish.

sns.despine() # Trimming unnecessary spines for a neat look

All set? Save it for posterity:

fig.savefig('plot_a4.png', format='png', dpi=300) # Preserving your masterpiece 🎭

Keeping your plot game strong

Here are some aspects to keep in mind for pristine plots:

Loading data

Load your dataset and then straight into Seaborn plots:

data = custom_load_data('data.csv') # Your data loading function sns.lineplot(x='time', y='value', data=data) plt.show()

Mix it up with plot types

Unleash the power of various Seaborn plot types:

  • sns.violinplot() for comparing distributions.
  • sns.scatterplot() for relationships revelation.
  • sns.heatmap() for those hot correlation matrices.

Join the plot discussion

Keep up-to-date with GitHub issues and StackOverflow discussions. They're treasure troves for tips, troubleshooting and memes.