How to Change the Size of Matplotlib Figures?
There are different ways to change the size of Matplotlib figures depending on the specific use case. Here are some common approaches:
- Set figure size when creating the plot:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(x, y)
This will create a figure with width 10 inches and height 5 inches.
- Use
rcParams
to set default figure size:
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (10, 5)
fig, ax = plt.subplots()
ax.plot(x, y)
This will set the default figure size to be 10 inches wide and 5 inches tall.
- Use the
set_size_inches
method to dynamically change the size of a figure:
fig, ax = plt.subplots()
ax.plot(x, y)
fig.set_size_inches(10, 5)
This will change the size of the current figure to be 10 inches wide and 5 inches tall.
- Use the
subplots_adjust
method to adjust the spacing around the figure:
fig, ax = plt.subplots()
ax.plot(x, y)
fig.subplots_adjust(left=0.2, right=0.8, top=0.8, bottom=0.2)
This will adjust the spacing around the plot within the figure. The values of left
, right
, top
, and bottom
specify the fraction of the figure width/height that is used for spacing.