Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to change figure size in matplotlib

Understanding the Basics of Figure Size in Matplotlib

When you begin to visualize data using Python, one of the most popular libraries you will encounter is Matplotlib. It's like a magic wand for data scientists, allowing them to create a wide variety of graphs and charts with just a few lines of code. However, before you can create stunning visualizations, you need to understand how to control the basic aspects of these graphs, such as their size.

Imagine you're framing a picture to hang on your wall. The size of the frame determines how much of the picture is visible and how it will fit into the space on your wall. Similarly, in Matplotlib, the figure size determines how much space your plot will take up. If it's too small, details may be hard to see. If it's too large, it may not fit well into your report or screen.

Adjusting Figure Size in Matplotlib

To change the figure size in Matplotlib, you use the figure() function, which is kind of like telling the library, "Hey, I'm about to start drawing a picture, and I want it to be this big." The figure() function has an argument called figsize that takes a tuple of two values. These two values correspond to the width and height of the figure in inches.

Here's a simple example:

import matplotlib.pyplot as plt

# Create a figure with a specific size
plt.figure(figsize=(10, 5))

# Plot some data
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

# Show the plot

In this example, figsize=(10, 5) sets the width of the figure to 10 inches and the height to 5 inches. When you run this code, you'll see a window pop up displaying a line plot that's twice as wide as it is tall.

Setting Figure Size for Subplots

Sometimes you want to create a figure that contains multiple smaller plots, known as subplots. It's like a photo collage where each photo needs to fit nicely within its own space. Matplotlib allows you to create a grid of subplots within a single figure.

When working with subplots, you can still control the overall figure size using the figsize argument. However, you do this when you create the subplot grid using the subplots() function, not the figure() function.

Here's how you can create a 2x2 grid of subplots with a specific figure size:

import matplotlib.pyplot as plt

# Create a 2x2 grid of subplots with a specific figure size
fig, axs = plt.subplots(2, 2, figsize=(10, 8))

# Plot data in each subplot
axs[0, 0].plot([1, 2, 3, 4], [1, 4, 9, 16])
axs[0, 1].plot([1, 2, 3, 4], [1, 2, 3, 4])
axs[1, 0].plot([1, 2, 3, 4], [1, 3, 6, 10])
axs[1, 1].plot([1, 2, 3, 4], [10, 9, 8, 7])

# Show the plot

In this case, figsize=(10, 8) tells Matplotlib to make the entire grid of plots 10 inches wide and 8 inches tall. The individual subplots will automatically size themselves to fit within this space.

Dynamically Adjusting Figure Size

There might be times when you need to adjust the figure size after you've already created some plots. Perhaps you're adding more data and things are starting to look cramped. Matplotlib is flexible and allows you to change the figure size dynamically using the set_size_inches() method.

Here's an example of how to adjust the size of an existing figure:

import matplotlib.pyplot as plt

# Create a figure and plot some data
fig = plt.figure()
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

# Dynamically adjust the figure size
fig.set_size_inches(12, 6)

# Show the updated plot

By calling fig.set_size_inches(12, 6), we've changed the figure size to be 12 inches wide and 6 inches tall. This is akin to realizing your picture frame is too small and swapping it out for a larger one without having to redraw the picture.

Aspect Ratio and DPI

While the figure size controls the dimensions of your plot, there are a couple of other concepts that can affect how your plot looks: aspect ratio and dots per inch (DPI).

The aspect ratio is the relationship between the width and height of the figure. It's like the shape of your TV screen, where a widescreen TV has a different aspect ratio compared to an old square TV. In Matplotlib, you control the aspect ratio through the figsize argument by choosing different values for width and height.

DPI stands for "dots per inch" and refers to the resolution of your plot. A higher DPI means your plot will have more pixels per inch, which can make it look sharper, especially when printed. You can set the DPI when saving your figure using the savefig() function:

import matplotlib.pyplot as plt

# Create a figure and plot some data
plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

# Save the figure with a specific DPI
plt.savefig('my_plot.png', dpi=300)

In this example, saving the figure with dpi=300 will result in a high-resolution image that will look good even when printed on paper.

Conclusion: Flexibility and Creativity with Figure Size

Learning to adjust the figure size in Matplotlib is like learning to adjust the seat and mirrors in a new car. Once you've got it set up just right, the ride is much smoother and you can focus on the road ahead—or in this case, the data you're trying to visualize.

Remember, the key to creating effective visualizations is not just about making them look good on your screen. It's about communicating information clearly and effectively. By mastering the control of figure size, aspect ratio, and resolution, you can ensure that your plots convey the right message, no matter where they're displayed.

As you continue your journey in programming and data visualization, keep experimenting with these settings. Try different sizes and aspect ratios to see what works best for your data. With practice, you'll develop an intuitive sense for how to make your visualizations not just correct, but compelling. Happy plotting!