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:
plt.subplots()Example:
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:
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:
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:
| Method | Usage | Code Example |
| Set size at creation | figsize parameter in plt.figure
or plt.subplots | plt.figure(figsize=(width, height)) |
| Adjust size post creation | set_size_inches() method | fig.set_size_inches(width, height) |
| Change default figure size | plt.rcParams configuration | plt.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. Useplt.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 inlineor%matplotlib notebookcan 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.

