NeuroAgent

How to Change Matplotlib Figure Size

Learn 3 methods to change Matplotlib figure size: figsize parameter, rcParams, and set_size_inches(). Convert units, control global/local settings, and master subplot sizing for perfect visualizations.

Question

How do I change the size of figures drawn with Matplotlib?

NeuroAgent

To change the size of figures drawn with Matplotlib, you can use three primary methods: the figsize parameter in plt.figure() or plt.subplots(), setting global defaults with plt.rcParams['figure.figsize'], or modifying existing figures with set_size_inches(). The figsize parameter accepts a tuple of (width, height) in inches, and you can convert between different units using appropriate conversion factors. Each method has specific use cases depending on whether you need to set global defaults, create individual figures with specific sizes, or modify existing figures.

Contents

Basic Methods for Changing Figure Size

The most straightforward way to change figure size in Matplotlib is by using the figsize parameter when creating a new figure. This parameter accepts a tuple specifying the width and height of the figure in inches.

Method 1: Using plt.figure()

python
import matplotlib.pyplot as plt

# Create a figure with custom size (width=10 inches, height=6 inches)
plt.figure(figsize=(10, 6))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title('10x6 inch figure')
plt.show()

Method 2: Using plt.subplots()

For figures that will contain subplots, you can specify the size directly in the subplots() function:

python
# Create a figure with subplots and custom size
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes[0,0].plot([1, 2, 3], [1, 2, 3])
axes[0,1].plot([1, 2, 3], [3, 2, 1])
axes[1,0].plot([1, 2, 3], [2, 3, 1])
axes[1,1].plot([1, 2, 3], [1, 3, 2])
plt.suptitle('12x8 inch figure with subplots')
plt.tight_layout()
plt.show()

According to the official Matplotlib documentation, the default figure size is [6.4, 4.8] inches when no figsize is specified.

Different Units and Conversions

Matplotlib primarily uses inches for figure sizing, but you can easily work with other units like centimeters or pixels by using appropriate conversion factors.

Converting from Centimeters

Since 1 inch = 2.54 centimeters, you can convert cm to inches by dividing by 2.54:

python
# Create a figure with size specified in centimeters
width_cm = 20
height_cm = 15
width_in = width_cm / 2.54
height_in = height_cm / 2.54

plt.figure(figsize=(width_in, height_in))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title(f'{width_cm}x{height_cm} cm figure')
plt.show()

Converting from Pixels

To convert from pixels to inches, you need to divide by the DPI (dots per inch) setting. The default DPI is typically 100:

python
# Create a figure with size specified in pixels
width_px = 800
height_px = 600

# Convert pixels to inches using the current DPI
dpi = plt.rcParams['figure.dpi']
px = 1 / dpi  # conversion factor: pixels to inches

plt.figure(figsize=(width_px * px, height_px * px))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title(f'{width_px}x{height_px} pixel figure')
plt.show()

As noted in the Matplotlib documentation on figure size units, because of the default rcParams['figure.dpi'] = 100, one can mentally divide the needed pixel value by 100.

Global vs Local Figure Size Settings

You can control figure size at different levels - globally for all figures or locally for specific figures.

Global Settings with rcParams

To set the default figure size for all figures in your script or notebook, use plt.rcParams:

python
import matplotlib.pyplot as plt

# Set global default figure size
plt.rcParams['figure.figsize'] = (8, 6)
plt.rcParams['figure.dpi'] = 120

# All subsequent figures will use these defaults
plt.figure()
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title('Global size setting (8x6 inches)')
plt.show()

# You can override the global setting for specific figures
plt.figure(figsize=(12, 8))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title('Overridden size (12x8 inches)')
plt.show()

Important Note for Jupyter Notebook Users: As mentioned in the MLJar blog post, you should set rcParams in a separate cell below imports. Setting rcParams in the same cell as matplotlib import will not make any change.

Local Settings

For one-off figure sizes, the local approach using figsize is more appropriate as it doesn’t affect other parts of your code:

python
# Create a figure with default size
plt.figure()
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title('Default size figure')

# Create another figure with custom size
plt.figure(figsize=(15, 10))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title('Custom 15x10 inch figure')
plt.show()

Best Practices and Common Scenarios

When to Use Each Method

Use Global Settings (rcParams) when:

  • You want consistent figure sizes throughout your entire project
  • You’re working in a notebook and want to set defaults once
  • You need to maintain consistent sizing across multiple visualizations

Use Local Settings (figsize) when:

  • You need different sizes for different types of plots
  • You’re creating a script that might be used as part of a larger system
  • You need precise control over individual figure dimensions

Common Figure Sizes and Use Cases

python
# Publication quality figures (often 300 DPI)
plt.rcParams['figure.dpi'] = 300
plt.rcParams['figure.figsize'] = (5, 4)  # Common journal figure size

# Presentation slides (larger, lower DPI)
presentation_figsize = (10, 8)
presentation_dpi = 100
plt.figure(figsize=presentation_figsize, dpi=presentation_dpi)

# Web display (aspect ratio considerations)
web_figsize = (12, 6)  # Wide aspect ratio for web
plt.figure(figsize=web_figsize)

According to the Towards Data Science guide, you can modify the size of a figure without using the figure environment by updating matplotlib.rcParams, which is an instance of RcParams for handling default Matplotlib values.

Working with Subplots

When working with subplots, you have several options for controlling figure size:

Method 1: Specify Size in plt.subplots()

python
# Create subplots with custom figure size
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
for i, ax in enumerate(axes.flat):
    ax.plot([1, 2, 3], [i+1, i+2, i+3])
plt.suptitle('15x10 inch subplot figure')
plt.tight_layout()
plt.show()

Method 2: Adjust Individual Subplot Sizes

You can also control the size of individual subplots using GridSpec:

python
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(12, 8))
gs = gridspec.GridSpec(2, 2, figure=fig, width_ratios=[1, 2], height_ratios=[1, 1])

ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, :])

ax1.plot([1, 2, 3], [1, 2, 3])
ax2.plot([1, 2, 3], [3, 2, 1])
ax3.plot([1, 2, 3, 4], [1, 4, 2, 3])

plt.suptitle('Custom subplot layout')
plt.tight_layout()
plt.show()

Modifying Existing Figures

If you already have a figure created and want to change its size, you can use several methods:

Method 1: set_size_inches()

python
# Create a figure with default size
fig = plt.figure()
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title('Original size')

# Modify the figure size
fig.set_size_inches(10, 6)
plt.draw()  # Required to update the display
plt.title('Modified size (10x6 inches)')
plt.show()

Method 2: set_figwidth() and set_figheight()

For modifying just one dimension:

python
fig = plt.figure(figsize=(6, 4))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])

# Change only the width
fig.set_figwidth(10)
plt.draw()
plt.title('Changed width to 10 inches')
plt.show()

Method 3: set_dpi()

To change the resolution without changing the physical size:

python
fig = plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])

# Change DPI (affects pixel dimensions but not physical size)
fig.set_dpi(150)
plt.draw()
plt.title('DPI changed to 150')
plt.show()

As noted in the MLJar blog post, if you already have a figure created (or it was created by another package) then you can manipulate its size and dpi with set_figwidth, set_figheight, set_size_inches, set_dpi functions.

Conclusion

Changing figure sizes in Matplotlib is straightforward once you understand the available methods and their appropriate use cases. Here are the key takeaways:

  1. Use figsize parameter when creating individual figures with specific dimensions - this is the most common and direct approach
  2. Use rcParams for setting global defaults that apply to all figures in your project
  3. Understand unit conversions - inches are the default, but you can easily convert from centimeters (divide by 2.54) or pixels (divide by DPI)
  4. Choose the right method for subplots - use figsize in plt.subplots() or GridSpec for complex layouts
  5. Modify existing figures using set_size_inches(), set_figwidth(), set_figheight(), or set_dpi() methods

For best results, consider your specific use case: publication-quality figures often require higher DPI settings while web displays might benefit from different aspect ratios. Always test your figures in their final display environment to ensure they look as intended.


Sources

  1. How do I change the size of figures drawn with Matplotlib? - Stack Overflow
  2. Matplotlib Figure Size – How to Change Plot Size in Python with plt.figsize() - freeCodeCamp
  3. How to Change the Size of Figures in Matplotlib? - GeeksforGeeks
  4. matplotlib.pyplot.figure — Matplotlib 3.10.7 documentation
  5. Figure size in different units — Matplotlib 3.10.7 documentation
  6. Change plot size in Matplotlib - Python - GeeksforGeeks
  7. The 3 ways to change figure size in Matplotlib - MLJar
  8. Change Figure Size in Matplotlib - Stack Abuse
  9. How To Change The Size Of Figures In Matplotlib | Towards Data Science
  10. Mastering Figure Size Units in Matplotlib | by LabEx | Medium