[파이썬] matplotlib 다중 그래프 그리기 (Subplots)

In data visualization, it is often useful to display multiple graphs side by side to compare and analyze data effectively. In matplotlib, this can be achieved using subplots. Subplots allow us to create a grid of multiple axes (i.e., graphs) within a single figure.

Let’s dive into the code and see how we can use subplots in matplotlib to create multiple graphs.

Importing the Required Libraries

First, we need to import the necessary libraries. In this example, we will be using matplotlib.pyplot for plotting and numpy to generate some random sample data.

import matplotlib.pyplot as plt
import numpy as np

Creating Subplots

To create subplots, we need to call the subplots() function. This function returns a figure object and an array of axes objects.

fig, axes = plt.subplots(nrows, ncols)

The nrows and ncols parameters specify the number of rows and columns in the subplot grid. For example, if we want a grid with 2 rows and 3 columns, we would pass nrows=2 and ncols=3 to the subplots() function.

Adding Data to Subplots

Once we have our subplots set up, we can start adding data to each individual subplot. We can access the axes objects in the axes array by indexing them.

Here’s an example of how we can generate random data and plot it in a 2x2 grid of subplots:

x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = np.exp(x)

# Create a 2x2 grid of subplots
fig, axes = plt.subplots(nrows=2, ncols=2)

# Plot data in the first subplot
axes[0, 0].plot(x, y1)
axes[0, 0].set_title("sin(x)")

# Plot data in the second subplot
axes[0, 1].plot(x, y2)
axes[0, 1].set_title("cos(x)")

# Plot data in the third subplot
axes[1, 0].plot(x, y3)
axes[1, 0].set_title("tan(x)")

# Plot data in the fourth subplot
axes[1, 1].plot(x, y4)
axes[1, 1].set_title("exp(x)")

# Adjust spacing between subplots
plt.tight_layout()

# Display the figure
plt.show()

In the above code, we generate four different datasets (y1, y2, y3, y4) and plot them in a 2x2 grid of subplots. We set the title for each subplot using the set_title() function. Finally, we use tight_layout() to adjust the spacing between subplots and show() to display the figure.

Customizing Subplots

Subplots offer a lot of flexibility for customization. You can adjust various aspects of the subplots such as padding, aspect ratio, grid lines, axis labels, legends, and so on.

To customize a specific subplot, you can use the respective axes object. For example, to set the x-axis label of the first subplot, you can use axes[0, 0].set_xlabel("X"). Similarly, you can use axes[0, 0].set_ylabel("Y") to set the y-axis label.

Conclusion

Subplots in matplotlib are a powerful tool for creating multiple graphs within a single figure. They allow us to compare and visualize different aspects of the data effectively. By using the subplots() function, we can create a grid of subplots and customize them as per our requirements.

If you want to generate more complex layouts of subplots, you can explore options like nested subplots or gridspec.

Hopefully, this tutorial has helped you understand how to use subplots in matplotlib. Start exploring and create stunning visualizations with multiple graphs!