You can change the size of the figures drawn in matplotlib with the figsize parameter by using plt.figure()
Proper figure sizing in Matplotlib is crucial for clear data visualization. You can adjust figure size using plt.figure(), plt.subplots(), or plt.rcParams based on your needs. This blog explores these methods with examples to improve readability and plot quality.
Table of Contents:
There are mainly four main methods used for resizing the images drawn with matplotlib
The plt.figure() function in Matplotlib allows us to create a figure with custom dimensions using the figsize parameter. The values are specified in inches.
Example: Changing the Figure Size using plt.figure()
import matplotlib.pyplot as plt
# produce a figure of your custom size( width =5, height = 10)
plt.figure( figsize = ( 5, 10))
# Plot sample data
plt.plot(( 1, 2, 3, 4),( 10, 25, 20, 30))
# Show the plot ()
plt.show()
Output:
Explanation:
We import matplotlib.pyplot as plt. plt.figure (figsize = (10, 5)) sets the figure size to 5 wide and 10 tall. A line plot is created with plt.plot(). plt.show() displays the figure. This system is useful when setting the figure size for a single plot system.
Method 2: Using plt.subplots() – for multiple subplots or advanced customizations
Using figsize in plt.subplots() is another effective way to adjust the figure size is by using plt.subplots(). This function creates a figure and returns an axes object, allowing for more advanced customization.
Example: Using plt.subplots() to Set Figure Size
import matplotlib.pyplot as plt
# produce a figure and axes with a specific size
fig, axes = plt.subplots(figsize = ( 12, 6))
# Plot sample data
axes.plot((1, 2, 3, 4),(5, 15, 20, 25))
# Set a title for the plot
axes.plot("Custom Figure Size")
# Display the plot
plt.show()
Output:
Explanation:
The function plt.subplots(figsize = (12, 6)) creates a figure of size 12 wide and 6 tall. It returns the figure and axes, allowing further plot control. We use axes.plot() to plot data and axes.set_title() to add a title. Finally, plt.show() renders the plot. This method is useful when working with multiple subplots or advanced customizations.
Method 3: Using plt.rcParams() – for all plots should maintain a consistently
Using figsize in plt.rcParams() is another useful way of adjusting the size of the figure by setting the global default image size for all the images. This ensures that all figures in the script follow the same dimensions.
Example: Using plt.rcParams() to set the size of the figure
import matplotlib.pyplot as plt
# Set default figure size globally
plt.rcParams["figure.figsize"] = (14, 7)
# Create a plot without specifying figsize
plt.plot([1, 2, 3, 4], [10, 40, 20, 30])
# Display the plot
plt.show()
Output:
Explanation:
plt.rcParams[“figure.figsize”] = (14, 7) sets a default figure size for all plots in the script, so the plot will follow this size without explicitly setting figsize. This method is helpful for maintaining consistent plot sizes across notebooks or projects.
Method 4: Using set_figheight() and set-figwidth() function
We can set the height and width of a figure in Matplotlib without using the figsize argument. Instead, you can use the set() function with figheight and figwidth, or use the set_figheight() and set_figwidth() functions.
Example:
import matplotlib.pyplot as plt
# Create a figure
fig = plt.figure()
# Set height and width individually
fig.set_figheight(10)
fig.set_figwidth(5)
# Plot sample data
plt.plot([1, 2, 3, 4], [10, 25, 20, 30])
# Show the plot
plt.show()
Output:
Conclusion
Each of the above methods provides flexibility depending on the use case, whether customizing an individual plot or maintaining consistency across multiple figures. Understanding these approaches allows better control over the visualization of the figure.