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:
- The
figsizeargument 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:
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:
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:
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:
Summary
| Method | Use 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. 🚀

