• Javascript
  • Python
  • Go

Changing the Size of Figures with Matplotlib

Matplotlib is a powerful library for data visualization in Python. With its various functionalities and customization options, it has become...

Matplotlib is a powerful library for data visualization in Python. With its various functionalities and customization options, it has become the go-to tool for many data analysts and scientists. One of the key features of Matplotlib is its ability to create visually appealing figures that effectively convey the underlying data.

However, when creating figures, one may encounter the need to change their size. This could be for various reasons, such as fitting the figure in a report or presentation, or simply to make it more visually appealing. In this article, we will explore the different ways to change the size of figures in Matplotlib.

First, let's start by creating a basic figure using Matplotlib. We will use the popular "scatter plot" as an example. The following code will generate a scatter plot with random data points:

```

# Importing necessary libraries

import numpy as np

import matplotlib.pyplot as plt

# Creating random data points

x = np.random.rand(50)

y = np.random.rand(50)

# Plotting the scatter plot

plt.scatter(x, y)

plt.show()

```

The output of this code will be a scatter plot with the default size. Now, let's see how we can change the size of this figure.

1. Using the `figsize` Parameter

Matplotlib allows us to specify the size of the figure using the `figsize` parameter. This parameter takes in a tuple of two values, where the first value represents the width of the figure and the second value represents the height. Let's modify our code to change the size of the figure to 8 inches by 6 inches:

```

# Setting the figure size to 8x6 inches

plt.figure(figsize=(8,6))

# Plotting the scatter plot

plt.scatter(x, y)

plt.show()

```

We can see that the figure size has now changed, making it bigger and more visible. This method is particularly useful when creating figures for reports or presentations, as it allows us to control the size of the figure according to our needs.

2. Using the `rcParams` Method

Another way to change the size of figures in Matplotlib is by using the `rcParams` method. This method allows us to set the default values for various parameters in Matplotlib, including the figure size. We can use the `rcParams` method to set the figure size as follows:

```

# Importing the rcParams module

from matplotlib import rcParams

# Setting the figure size to 8x6 inches

rcParams['figure.figsize'] = 8,6

# Plotting the scatter plot

plt.scatter(x, y)

plt.show()

```

This method is useful when we want to change the size of all the figures in our code, rather than just one specific figure.

3. Using the `set_size_inches` Method

The `set_size_inches` method can also be used to change the size of figures in Matplotlib. This method takes in two parameters, where the first parameter represents the width of the figure and the second parameter represents the height. Let's use this method to change the size of our scatter plot to 10 inches by 8 inches:

```

# Plotting the scatter plot

plt.scatter(x, y)

# Setting the figure size to 10x8 inches

plt.gcf().set_size_inches(10,8)

plt.show()

```

The `gcf()` method returns

Related Articles

Accessing MP3 Metadata with Python

MP3 files are a popular format for digital audio files. They are small in size and can be easily played on various devices such as smartphon...

Bell Sound in Python

Python is a popular programming language used for a variety of applications, from web development to data analysis. One of the lesser-known ...