Mastering Figure Sizes in Matplotlib: A Comprehensive Guide
Published on
Understanding how to control the size of the figures in Matplotlib, a popular Python data visualization library, is crucial to producing high-quality plots. In this comprehensive guide, we'll explore several methods to set figure sizes in Matplotlib, whether you're creating a new figure or want to modify an existing one.
Understanding Matplotlib Figure Sizes
Matplotlib operates in a way similar to MATLAB. When you create a figure, you can specify the size of the figure in inches. The figure()
function is used to create a new figure and it takes the figsize
argument, which represents the width and height of the figure in inches. It is typically provided as a tuple (width, height).
Here is a simple example of creating a figure with a specified size:
from matplotlib.pyplot import figure
## Create a new figure with a specific size (width=8 inches, height=6 inches)
figure(figsize=(8, 6))
In the above code snippet, the figure()
function is called with the figsize
parameter set to (8, 6). This creates a figure that is 8 inches wide and 6 inches tall.
Adjusting Figure Size After Creation
Sometimes, you may want to adjust the size of a figure after it's been created. The Matplotlib library provides the set_size_inches()
function to make this possible.
Here's how to use this function:
import matplotlib.pyplot as plt
## Create a new figure
fig = plt.gcf()
## Set figure size to 18.5x10.5 inches
fig.set_size_inches(18.5, 10.5)
## Save the figure
fig.savefig('figure.png', dpi=100)
In this code snippet, the gcf()
function (which stands for 'get current figure') is used to get the current figure, and then its size is set using the set_size_inches()
method.
Leveraging plt.rcParams
Another way to set the figure size in Matplotlib is by using the plt.rcParams
parameter, especially useful when you're using plt.plot()
and don't want to use the figure environment.
Here's a simple example:
import matplotlib.pyplot as plt
## Set figure size to 20x3
plt.rcParams["figure.figsize"] = (20,3)
In this code snippet, the rcParams
dictionary is used to set the size of the figure. This approach is extremely handy when plotting inline, such as within a Jupyter notebook.
Setting Figure Size in Centimeters
While the figsize
tuple accepts inches, if you want to set the size in centimeters, you'll need to convert the dimensions from centimeters to inches (since 1 inch equals 2.54 cm).
Here's how to do it:
## Set figure size in centimeters
width_cm = 20
height_cm = 10
## Convert cm to inches
width_in = width_cm/2.54
height_in = height_cm/2.54
## Create a new figure with specified size
figure(figsize=(width_in, height_in))
In this example, we first specify the width and height in centimeters. These dimensions are then converted to inches by dividing them by 2.54. Finally
, the converted dimensions are used to create a new figure with the specified size.
Using rcParams
for Figure Size Adjustment
It's important to note the flexibility and power of the rcParams
approach. This strategy allows you to define default settings for your figures, ensuring consistency across all plots in your script or notebook. In the next section, we'll see how to leverage rcParams
to adjust figure sizes dynamically.
Dynamically Changing Figure Size with rcParams
As mentioned earlier, rcParams
is not only useful for setting a one-time figure size, but it also provides an efficient way to adjust figure sizes for the entire coding environment dynamically. This approach is especially beneficial when working on larger projects, where you may require consistent plotting styles and dimensions.
The global figure size can be set back to default using the rcParamsDefault
attribute. Here's how:
plt.rcParams["figure.figsize"] = plt.rcParamsDefault["figure.figsize"]
In this code, the rcParamsDefault
attribute contains the default settings, effectively resetting the figure size to the original dimensions.
Controlling Figure Sizes Using Pandas
Pandas, a library used for data manipulation and analysis, often works in tandem with Matplotlib for visualizations. If you're looking to adjust the figure size while working with Pandas, you can directly set the figsize
parameter within the plot function as follows:
df['some_column'].plot(figsize=(10, 5))
In this example, df
is a Pandas dataframe, and we're creating a plot for 'some_column' with a size of 10x5 inches.
You can also set the figure size when creating subplots:
fig, ax = plt.subplots(figsize=(10, 5))
df['some_column'].plot(ax=ax)
Adjusting the Default Figure Size
Want to change the default figure size setting? No problem. Matplotlib makes this easy by allowing you to update the default settings:
import matplotlib
matplotlib.rc('figure', figsize=(10, 5))
In this code, the matplotlib.rc
function is used to change the default figure size. Now, all plots created will have a size of 10x5 inches.
Alternative to Matplotlib: Visualize Data with PyGWalker
Besides using Matplotlib to visualize your pandas dataframe, here is an alternative, Open Source python library that can help you create data visualization with ease: PyGWalker (opens in a new tab).
No need to complete complicated processing with Python coding anymore, simply import your data, and drag and drop variables to create all kinds of data visualizations! Here's a quick demo video on the operation:
Here's how to use PyGWalker in your Jupyter Notebook:
pip install pygwalker
import pygwalker as pyg
gwalker = pyg.walk(df)
Alternatively, you can try it out in Kaggle Notebook/Google Colab:
PyGWalker is built on the support of our Open Source community. Don't forget to check out PyGWalker GitHub (opens in a new tab) and give us a star!
Frequently Asked Questions:
-
How do I set the figure size in Matplotlib?
You can set the figure size in Matplotlib using the
figure()
function with thefigsize
parameter. Thefigsize
parameter accepts a tuple, representing the width and height of the figure in inches. -
How do I adjust the size of an existing figure in Matplotlib?
To adjust the size of an existing figure, use the
set_size_inches()
function. This function allows you to set the width and height of the current figure. -
Can I set the figure size in Pandas plots?
Yes, you can adjust the figure size directly within the Pandas
plot()
function using thefigsize
parameter.