• Javascript
  • Python
  • Go

Zooming Subplots Together in Matplotlib/Pyplot

When it comes to creating visualizations in Python, Matplotlib is a powerful and popular choice among data scientists and analysts. This lib...

When it comes to creating visualizations in Python, Matplotlib is a powerful and popular choice among data scientists and analysts. This library offers a wide range of customization options and tools for creating high-quality graphs and charts. One useful feature of Matplotlib is the ability to create subplots, which allows us to display multiple plots within a single figure. In this tutorial, we will explore the concept of zooming subplots together in Matplotlib/Pyplot.

Subplots are useful when we want to compare multiple data sets or visualize different aspects of a dataset in one figure. However, sometimes we may want to zoom in on a particular area of a plot to get a closer look at the data. In Matplotlib, we can achieve this by using the zoom function. Let's first create a simple line plot with three subplots using the following code:

```python

import matplotlib.pyplot as plt

# Create figure and subplots

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 5))

# Generate data for plots

x = range(10)

y1 = [i**2 for i in x]

y2 = [i**3 for i in x]

y3 = [i**4 for i in x]

# Plot data on subplots

ax1.plot(x, y1)

ax2.plot(x, y2)

ax3.plot(x, y3)

# Add titles and labels

ax1.set_title('Subplot 1')

ax2.set_title('Subplot 2')

ax3.set_title('Subplot 3')

plt.tight_layout()

plt.show()

```

This code will generate a figure with three subplots, each displaying a different function of the x values. Now, let's say we want to zoom in on the data in Subplot 2. We can do this by adding the following lines of code before the `plt.show()` statement:

```python

# Set the limits for the x and y axes

ax2.set_xlim(3, 7)

ax2.set_ylim(20, 200)

# Zoom in on the plot

ax2.zoom(2.0)

```

Here, we are setting the limits for the x and y axes of Subplot 2 and then using the `zoom` function to zoom in on the plot. The `zoom` function takes in a zoom factor as a parameter, which determines the amount of zoom applied to the plot. A zoom factor of 1.0 means no zoom, while a value greater than 1.0 will zoom in, and a value less than 1.0 will zoom out.

Now, when we run the code, we can see that the data in Subplot 2 is zoomed in, making it easier to analyze and interpret. However, the other two subplots remain unchanged.

![Subplots with different zoom levels](https://i.imgur.com/4rbsV5f.png)

We can also use the `zoom` function to zoom in on multiple subplots at once. For example, let's say we want to zoom in on Subplots 1 and 3 together. We can achieve this by passing a list of subplot axes to the `zoom` function, as shown below:

```python

# Zoom in on subplots 1 and 3

ax1.zoom(2.0)

ax3.zoom(2.0)

```

This will apply the same zoom factor to both subplots, allowing us to analyze them in more detail.

![Subplots with same zoom level](https://i.imgur.com/B4NhqN8.png)

In addition to the `zoom` function, Matplotlib also provides a `pan` function that allows us to pan our subplots in any direction. This function takes in two parameters, the amount of panning in the x and y direction, respectively. Similar to the `zoom` function, a positive value will pan in the positive direction, while a negative value will pan in the negative direction.

Now, let's say we want to pan Subplot 3 to the right by 0.5 units and up by 0.2 units. We can do this by adding the following lines of code before the `plt.show()` statement:

```python

# Pan Subplot 3

ax3.pan(0.5, 0.2)

```

As a result, Subplot 3 will be shifted to the right and up, as shown in the image below.

![Subplots with panning](https://i.imgur.com/0kYXwKB.png)

In addition to the `zoom` and `pan` functions, Matplotlib also provides the `get_xlim` and `get_ylim` functions, which return the current limits for the x and y axes, respectively. These functions can be useful when we want to save

Related Articles