[파이썬] seaborn 러그 플롯(Rug plot)

Seaborn is a powerful data visualization library in Python that is built on top of Matplotlib. It provides a high-level interface for creating beautiful and informative statistical graphics. One of the useful plots offered by Seaborn is the Rug plot, also known as the Strip plot.

What is a Rug plot?

A Rug plot is a one-dimensional plot that displays individual data points along an axis. It adds a small vertical tick or ‘rug’ for each data point, showing their distribution along the given axis. Rug plots are often used in combination with other plots to provide additional insights into the data.

Creating a Rug plot with Seaborn

Seaborn makes it straightforward to create a Rug plot. Let’s begin by importing the necessary libraries and loading a dataset:

import seaborn as sns

# Load the dataset
tips = sns.load_dataset("tips")

To create a Rug plot using Seaborn, we can utilize the sns.rugplot() function. Here’s an example that demonstrates its usage:

import seaborn as sns
import matplotlib.pyplot as plt

# Create a Rug plot
sns.set(style="darkgrid")
plt.figure(figsize=(8, 4))
sns.rugplot(data=tips, x="total_bill", height=0.3)
plt.title("Rug plot of Total Bill")
plt.xlabel("Total Bill")
plt.ylabel("Frequency")
plt.show()

In the above example, we set the plotting style to “darkgrid” using sns.set(). We then create a figure with specific dimensions using plt.figure(figsize=(8, 4)).

The sns.rugplot() function is used to create the Rug plot. We pass the tips dataset and specify which column to use for the data (x="total_bill"). Additionally, we can adjust the height of the ticks using the height parameter.

Finally, we add a title, labels for the x and y axes, and display the plot using plt.title(), plt.xlabel(), plt.ylabel(), and plt.show().

Customizing the Rug plot

Seaborn provides various options to customize the Rug plot according to your preferences. Here are a few examples:

Feel free to experiment with these options to create visually appealing and informative Rug plots.

Conclusion

Seaborn’s Rug plot is a handy visualization tool that can provide valuable insights into the distribution of data points along a single axis. It is relatively simple to create Rug plots using Seaborn, and the library offers various customization options to enhance the plot’s appearance. Whether used individually or in combination with other plots, Rug plots are an excellent addition to your data exploration toolkit in Python.

Happy plotting!