How do I change the size of figures drawn with Matplotlib?
To change the size of figures drawn with Matplotlib in Python, you can use the figure()
function and set the figsize
argument. This argument takes a tuple of (width, height)
in inches, and specifies the size of the figure in inches.
Here is an example of how you can use the figure()
function to change the size of a figure:
import matplotlib.pyplot as plt
# Set the figure size to 10 inches wide and 6 inches tall
plt.figure(figsize=(10, 6))
# Plot something
plt.plot([1, 2, 3, 4])
# Show the plot
plt.show()
Alternatively, you can use the set_size_inches()
method of the Figure
object to change the size of the figure after it has been created. Here is an example of how you can use this method:
import matplotlib.pyplot as plt
# Create a figure
fig = plt.figure()
# Set the figure size to 10 inches wide and 6 inches tall
fig.set_size_inches(10, 6)
# Plot something
plt.plot([1, 2, 3, 4])
# Show the plot
plt.show()
You can also use the rcParams
dictionary to set the default figure size for all figures in your script. To do this, you can use the matplotlib.pyplot.rcParams
dictionary and set the figure.figsize
key. Here is an example of how you can use the rcParams
dictionary to set the default figure size:
import matplotlib.pyplot as plt
# Set the default figure size to 10 inches wide and 6 inches tall
plt.rcParams['figure.figsize'] = (10, 6)
# Create a figure
fig = plt.figure()
# Plot something
plt.plot([1, 2, 3, 4])
# Show the plot
plt.show()