📜  matplotlib 子图 - Python (1)

📅  最后修改于: 2023-12-03 15:32:51.659000             🧑  作者: Mango

Matplotlib Subplots in Python

Matplotlib is a versatile library that allows you to create a wide variety of plots and visualizations in Python. One of the features of Matplotlib is the ability to create subplots, which allow you to create multiple plots in the same figure. In this tutorial, we'll take a look at how to create subplots in Matplotlib.

Creating Subplots

To create subplots in Matplotlib, you use the plt.subplots() function. This function returns a tuple containing a figure and a two-dimensional array of subplots. The syntax for creating subplots looks like this:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows=2, ncols=2)

This code creates a 2x2 grid of subplots. You can adjust the number of rows and columns by changing the nrows and ncols parameters, respectively. The axs variable is a two-dimensional array that holds references to each of the subplots. You can use these references to create plots on each of the subplots.

Plotting on Subplots

Once you have created subplots, you can plot data on them just like you would with a regular plot. To plot on a specific subplot, you index into the axs array using the row and column indices. For example, to plot on the top-left subplot, you would use axs[0, 0]. Here's an example that creates a scatter plot on each of the subplots:

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(nrows=2, ncols=2)

for ax in axs.flat:
    x = np.random.rand(10)
    y = np.random.rand(10)
    ax.scatter(x, y)

This code creates a random scatter plot on each of the subplots. The axs.flat attribute flattens the two-dimensional axs array, allowing us to easily iterate over each of the subplots.

Customizing Subplots

You can customize the subplots just like you would with a regular plot. Here are a few examples:

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(nrows=2, ncols=2)

# Set the title for each subplot
for i, ax in enumerate(axs.flat):
    ax.set_title(f"Subplot {i+1}")

# Set the x and y labels for the entire figure
fig.text(0.5, 0.04, "X-axis", ha="center")
fig.text(0.04, 0.5, "Y-axis", va="center", rotation="vertical")

# Adjust the spacing between subplots
fig.subplots_adjust(hspace=0.4, wspace=0.4)

# Create a plot on the bottom-right subplot
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
axs[1, 1].plot(x, y)
axs[1, 1].set_xlabel("Time")
axs[1, 1].set_ylabel("Intensity")
axs[1, 1].set_title("Sine Wave")

This code sets the title for each subplot, sets the axis labels for the entire figure, adjusts the spacing between subplots, and creates a plot on the bottom-right subplot.

Conclusion

In this tutorial, we have learned how to create subplots in Matplotlib and how to plot on them. We have also seen a few examples of how to customize subplots. With this knowledge, you should be able to create complex visualizations that utilize multiple plots in a single figure.