coding
figures
matplotlib

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

To change the size of figures drawn with Matplotlib, you can adjust the figsize parameter when creating the figure. This parameter is used to specify the dimensions of the figure in inches.


1. Using figsize in plt.figure()

You can set the figure size by passing the figsize parameter to plt.figure().

Example:

python
1import matplotlib.pyplot as plt
2
3plt.figure(figsize=(10, 6))  # Width: 10 inches, Height: 6 inches
4plt.plot([1, 2, 3], [4, 5, 6])
5plt.title("Figure with Custom Size")
6plt.show()
  • The figsize argument takes a tuple (width, height) where the dimensions are in inches.

2. Using figsize in plt.subplots()

When creating multiple subplots, you can also specify the figure size using figsize in plt.subplots().

Example:

python
1import matplotlib.pyplot as plt
2
3fig, axes = plt.subplots(2, 2, figsize=(12, 8))  # 2x2 grid, size 12x8 inches
4axes[0, 0].plot([1, 2, 3], [4, 5, 6])
5axes[0, 0].set_title("Subplot 1")
6
7axes[1, 1].plot([3, 2, 1], [6, 5, 4])
8axes[1, 1].set_title("Subplot 2")
9
10plt.tight_layout()  # Adjust spacing between subplots
11plt.show()

3. Changing the Size of an Existing Figure

If the figure is already created, you can modify its size using the set_size_inches() method.

Example:

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([1, 2, 3], [4, 5, 6])
5fig.set_size_inches(8, 4)  # Change size to 8x4 inches
6plt.show()

4. Setting Default Figure Size

You can configure the default figure size for all plots by updating the figure.figsize parameter in Matplotlib's configuration (rcParams).

Example:

python
1import matplotlib.pyplot as plt
2
3plt.rcParams["figure.figsize"] = (10, 5)  # Default figure size: 10x5 inches
4plt.plot([1, 2, 3], [4, 5, 6])
5plt.title("Default Sized Figure")
6plt.show()

This changes the default size for all figures in your session.


5. Saving Figures with a Specific Size

When saving a figure, you can also specify the size using the dpi (dots per inch) parameter, which controls the resolution.

Example:

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots(figsize=(10, 6))  # Set figure size
4ax.plot([1, 2, 3], [4, 5, 6])
5plt.savefig("figure.png", dpi=300)  # Save with 300 DPI resolution

Summary

MethodUse Case
plt.figure(figsize=(w, h))Create a single figure with a specific size.
plt.subplots(figsize=(w, h))Create multiple subplots with a specific size.
fig.set_size_inches(w, h)Change the size of an existing figure.
plt.rcParams["figure.figsize"] = (w, h)Set the default size for all figures.
plt.savefig("file.png", dpi=300)Save a figure with a specific resolution.

By using these methods, you can control the size of your Matplotlib figures for better visualization and presentation. 🚀


Course illustration
Course illustration

All Rights Reserved.