[파이썬] seaborn 다양한 레이아웃과 구조의 그리드 사용법

Seaborn is a popular data visualization library in Python which is based on Matplotlib. It provides a high-level interface for creating aesthetically pleasing and informative statistical graphics. One of the key features of Seaborn is its ability to create versatile grid layouts and structures to effectively display multiple plots within a single figure.

In this blog post, we will explore the various layout and structure options supported by Seaborn’s grid functions.

Grid Layouts

Seaborn provides two main functions for creating grid layouts: FacetGrid and PairGrid.

FacetGrid

FacetGrid allows us to create a grid of subplots based on the levels of one or more variables in our dataset. This helps in visualizing the relationships between multiple variables in a structured manner.

To create a FacetGrid, we need to specify the dataset, the variable(s) on which to facet, and the type of plots to be used. Here’s an example:

import seaborn as sns
import pandas as pd

# Load dataset
data = pd.read_csv('data.csv')

# Create FacetGrid
g = sns.FacetGrid(data, col='category')

# Add plots to the grid
g.map(sns.boxplot, 'x', 'y')
g.map(sns.scatterplot, 'x', 'z')

# Show the grid
sns.plt.show()

In the above example, we create a FacetGrid based on the ‘category’ column of our dataset. We then add boxplots and scatterplots to the grid for the ‘x’, ‘y’, and ‘z’ variables. Finally, we display the grid using sns.plt.show().

PairGrid

PairGrid is another useful function provided by Seaborn to create grid layouts. It allows us to create a grid of subplots showing pairwise relationships between multiple variables.

Here’s an example of creating a PairGrid:

import seaborn as sns
import pandas as pd

# Load dataset
data = pd.read_csv('data.csv')

# Create PairGrid
g = sns.PairGrid(data)

# Add scatterplots to the grid
g.map_upper(sns.scatterplot)
g.map_lower(sns.kdeplot)
g.map_diag(sns.histplot)

# Show the grid
sns.plt.show()

In the above example, we create a PairGrid based on our dataset. We then add scatterplots, kernel density plots, and histograms to the grid using map_upper, map_lower, and map_diag, respectively. Finally, we display the grid using sns.plt.show().

Grid Structures

Seaborn also provides grid structures like JointGrid and ClusterGrid which allow us to create more complex grid layouts.

JointGrid

JointGrid helps in visualizing the joint distribution between two variables along with their individual distributions. It creates a grid with a central plot showing the joint distribution and marginal plots showing the individual distributions.

Here’s an example:

import seaborn as sns
import pandas as pd

# Load dataset
data = pd.read_csv('data.csv')

# Create JointGrid
g = sns.JointGrid(data=data, x='x', y='y')

# Add plots to the grid
g.plot(sns.scatterplot, sns.histplot)

# Show the grid
sns.plt.show()

In the above example, we create a JointGrid based on the ‘x’ and ‘y’ columns of our dataset. We then add a scatterplot and a histogram to the grid using plot. Finally, we display the grid using sns.plt.show().

ClusterGrid

ClusterGrid helps in visualizing hierarchical clustering of variables. It creates a grid with a central dendrogram plot showing the hierarchical clustering and marginal plots showing the individual distributions of each variable.

Here’s an example:

import seaborn as sns
import pandas as pd

# Load dataset
data = pd.read_csv('data.csv')

# Create ClusterGrid
g = sns.ClusterGrid(data=data)

# Add plots to the grid
g.plot(sns.dendrogram, sns.histplot)

# Show the grid
sns.plt.show()

In the above example, we create a ClusterGrid based on our dataset. We then add a dendrogram plot and a histogram to the grid using plot. Finally, we display the grid using sns.plt.show().

Conclusion

Seaborn provides a variety of options for creating versatile grid layouts and structures to effectively display multiple plots within a single figure. The FacetGrid, PairGrid, JointGrid, and ClusterGrid functions can be used to create different types of grid layouts based on our visualization requirements.

By utilizing these grid functions effectively, we can achieve a more organized and insightful representation of our data. Happy plotting with Seaborn!