Matplotlib
figure size
data visualization
Python
plotting

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.

In scientific and data analyses, presenting data in a clear and readable manner is crucial. Matplotlib, a comprehensive library for creating static, interactive, and animated visualizations in Python, provides various ways to customize the appearance of plots, including changing the size of figures. This article provides a detailed explanation on how to adjust the figure size using Matplotlib.

Changing Figure Size in Matplotlib

Matplotlib provides several methods to set or alter the size of figures, both globally (for all figures in a session) or locally (for individual figures). Key techniques include setting the size at figure creation, adjusting size afterward, and modifying size configuration.

Method 1: Set Size at Figure Creation

The most straightforward way to set the size of a figure is by defining it at the time of its creation using the figsize parameter in plt.figure() or plt.subplots().

  • plt.figure() Example:
python
1import matplotlib.pyplot as plt
2
3plt.figure(figsize=(width, height))  # width and height in inches
4plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
5plt.show()
  • plt.subplots() Example:
python
fig, ax = plt.subplots(figsize=(width, height))
ax.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()

Method 2: Adjust Size Post Creation

Sometimes you might need to adjust the figure size after it's been created. This can be done using set_size_inches() method on a figure object.

Example:

python
1fig, ax = plt.subplots()  # Default figure size
2ax.plot([1, 2, 3, 4], [10, 20, 25, 30])
3
4fig.set_size_inches(width, height)  # Adjust the size later
5plt.show()

Method 3: Change Default Figure Size

To apply a consistent size to all figures in a session, change the default figure size within Matplotlib’s configuration using plt.rcParams. This approach is advantageous for batch processing or when generating multiple figures with uniform dimensions.

Example:

python
plt.rcParams["figure.figsize"] = (width, height)  # Set the default figure size
# All subsequent plots will follow this size

Units and Compatibility

The default unit for figure size in Matplotlib is inches. This is suitable for a variety of purposes such as preparing plots for publication, where size specifications might be needed. However, if you prefer other units like centimeters, manual conversion is straightforward:

1 inch = 2.54 cm

Importance of Aspect Ratio

Maintaining an appropriate aspect ratio for readability is crucial in figure scaling. Aspect ratio refers to the width-to-height proportion of a figure. Distorting this can lead to misrepresented data. Thus, when altering a figure's size, ensure the aspect ratio remains unchanged unless distortion is required for specific presentation needs.

Summary Table

The table below summarizes key commands used in modifying Matplotlib figure sizes:

MethodUsageCode Example
Set size at creationfigsize parameter in plt.figure or plt.subplotsplt.figure(figsize=(width, height))
Adjust size post creationset_size_inches() methodfig.set_size_inches(width, height)
Change default figure sizeplt.rcParams configurationplt.rcParams["figure.figsize"] = (width, height)

Additional Considerations

  • Resolution and DPI: When adjusting figure size, consider the dpi (dots per inch) setting, which affects the clarity of the plot when saved. Use plt.savefig('filename.png', dpi=resolution) to specify resolution.
  • Interactivity and Backend: The ability to resize figures dynamically might depend on the Matplotlib backend being used. For interactive platforms like Jupyter, %matplotlib inline or %matplotlib notebook can influence rendering.

By leveraging these techniques, you can effectively control figure sizes in Matplotlib, enhancing the visual delivery of data. Adjusting figure size not only improves aesthetics but also ensures that your data's story is communicated accurately and effectively.


Course illustration
Course illustration

All Rights Reserved.