[파이썬] 다중 축 설정

When it comes to data visualization, it is common to have multiple variables or metrics that need to be displayed on different scales. Multiaxis plotting allows us to create plots with multiple y-axes, each with a different scale, on a single figure. This can be particularly useful when we want to compare different variables that have different ranges or units.

In this blog post, we will explore how to set up multiple axes in Python using popular data visualization libraries such as Matplotlib and Seaborn. Let’s dive in!

Using Matplotlib

Matplotlib is a widely used library for data visualization in Python. It provides a comprehensive set of tools and functions to create high-quality plots. To create multiple axes in Matplotlib, we can use the twinx() and twiny() functions.

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax3 = ax1.twiny()

# Plot data on the first axis
ax1.plot(x1, y1, 'r-', label='Axis 1')

# Plot data on the second axis
ax2.plot(x2, y2, 'g-', label='Axis 2')

# Plot data on the third axis
ax3.plot(x3, y3, 'b-', label='Axis 3')

# Customize axes labels and legends
ax1.set_xlabel('X-axis 1 Label')
ax1.set_ylabel('Y-axis 1 Label')
ax2.set_ylabel('Y-axis 2 Label')
ax3.set_xlabel('X-axis 3 Label')

# Add legends
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
ax3.legend(loc='lower left')

plt.show()

In the code above, we first create a figure and an initial axis (ax1) using plt.subplots(). We then use the twinx() and twiny() functions to create additional axes (ax2 and ax3), which share the same x-axis and y-axis, respectively, with ax1. We can then plot the data on each axis separately using the appropriate plot() function.

By customizing the labels and legends of each axis, we can clearly differentiate between the variables being plotted. Finally, calling plt.show() will display the plot with multiple axes.

Using Seaborn

Seaborn is a Python library built on top of Matplotlib, offering a more intuitive and aesthetically pleasing interface for data visualization. Although Seaborn does not have built-in support for multiple axes like Matplotlib, we can still achieve the desired effect using its features in combination with Matplotlib.

import seaborn as sns
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()

# Plot data on the first axis using Seaborn
sns.lineplot(x1, y1, ax=ax1, color='r', label='Axis 1')

# Plot data on the second axis using Matplotlib
ax2.plot(x2, y2, 'g-', label='Axis 2')

# Customize axes labels and legends
ax1.set_xlabel('X-axis Label')
ax1.set_ylabel('Y-axis Label (Axis 1)')
ax2.set_ylabel('Y-axis Label (Axis 2)')

# Add legends
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')

plt.show()

In the code above, we use Seaborn’s lineplot() function to plot the data on ax1, which automatically sets up the appropriate style and color scheme. For the second axis (ax2), we use Matplotlib’s plot() function. The rest of the code is similar to the previous example.

By leveraging the strengths of both Seaborn and Matplotlib, we can create visually appealing plots with multiple axes that effectively convey information.

Conclusion

In this blog post, we explored how to set up multiple axes in Python using Matplotlib and Seaborn. Having multiple axes allows us to visualize multiple variables or metrics on different scales within a single plot. By customizing the labels, legends, and styles of each axis, we can create visually informative and appealing plots.

Whether you choose to use Matplotlib or Seaborn, the ability to create plots with multiple axes opens up a world of possibilities for data visualization. Experiment with different datasets and plot configurations to master this powerful technique.

Happy plotting!