Matplotlib Figure Size: Changing the Plot Size in Python

Estimated read time 3 min read

Introduction to Matplotlib Figure Size

Matplotlib, a popular data visualization library in Python, allows you to create various types of plots and charts. When working with Matplotlib, you may need to adjust the size of the plot to ensure optimal visibility and presentation. In this article, we will explore how to change the figure size in Matplotlib to customize the dimensions of your plots.

Understanding Matplotlib Figure and Axes

Before diving into changing the figure size, let’s understand the key concepts of Matplotlib:

  • Figure: In Matplotlib, a figure represents the entire window or page that contains one or more plots or visualizations. It acts as a container for all the elements of the plot.
  • Axes: An axes object is the region within the figure where the data is plotted. It contains the coordinate system, ticks, labels, and other graphical elements associated with the plot.

Changing Figure Size

To change the figure size in Matplotlib, you can use the figure function and specify the desired dimensions. Here’s an example:

pythonCopy codeimport matplotlib.pyplot as plt

# Create a figure with a specific size
plt.figure(figsize=(8, 6))

# Plot your data
plt.plot(x, y)

# Customize the plot
plt.title("My Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Show the plot
plt.show()

In the above code, we use the figure function and pass the figsize parameter to set the dimensions of the figure. The figsize parameter accepts a tuple representing the width and height of the figure in inches.

Changing Figure Size with Subplots

When working with multiple subplots, you can specify the figure size for each subplot individually or set a common figure size for all subplots. Here’s an example:

pythonCopy codeimport matplotlib.pyplot as plt

# Create subplots with a common figure size
fig, axes = plt.subplots(2, 2, figsize=(10, 8))

# Plot data on each subplot
axes[0, 0].plot(x1, y1)
axes[0, 1].plot(x2, y2)
axes[1, 0].plot(x3, y3)
axes[1, 1].plot(x4, y4)

# Customize each subplot
axes[0, 0].set_title("Plot 1")
axes[0, 1].set_title("Plot 2")
axes[1, 0].set_title("Plot 3")
axes[1, 1].set_title("Plot 4")

# Adjust spacing between subplots
plt.tight_layout()

# Show the plots
plt.show()

In this code snippet, we use the subplots function to create a 2×2 grid of subplots. We pass the figsize parameter to set the figure size for the entire grid of subplots.

Saving Figures with Custom Size

You can also save your Matplotlib figures with a specific size using the savefig function. Here’s an example:

pythonCopy codeimport matplotlib.pyplot as plt

# Create a figure with a specific size
plt.figure(figsize=(8, 6))

# Plot your data
plt.plot(x, y)

# Customize the plot
plt.title("My Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Save the figure with a specific size
plt.savefig("my_plot.png", dpi=300, bbox_inches='tight')

In the above code, we use the savefig function to save the

Mark Stain

My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.

You May Also Like

More From Author

+ There are no comments

Add yours