[파이썬] seaborn 히트맵(Heatmap)

If you are looking to visualize your data in a clear and concise way, seaborn’s heatmap is a powerful tool to consider. A heatmap is a two-dimensional graphical representation of data where the values are represented by colors, making it easier to identify patterns and relationships.

Installation

Before we dive into the code, let’s make sure you have seaborn installed. You can install seaborn using pip, by running the following command in your terminal:

pip install seaborn

Example Usage

Now let’s look at an example of creating a heatmap using seaborn in Python:

import seaborn as sns
import matplotlib.pyplot as plt

# Create a 2D array of data
data = [[10, 20, 30, 40],
        [50, 60, 70, 80],
        [90, 100, 110, 120],
        [130, 140, 150, 160]]

# Create a heatmap using seaborn
ax = sns.heatmap(data, cmap="YlGnBu")

# Add labels and title
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Heatmap Example")

# Display the heatmap
plt.show()

In the above example, we start by importing the necessary libraries: seaborn and matplotlib.pyplot. We then create a 2D array called data with some arbitrary values.

Next, we use sns.heatmap() to create a heatmap of our data. The cmap parameter specifies the color palette to be used. In this example, we chose “YlGnBu” as our color scale.

Finally, we add labels and a title to our heatmap using plt.xlabel(), plt.ylabel(), and plt.title(). We display the heatmap using plt.show().

Customization

Seaborn’s heatmap offers a range of options to customize the appearance of your visualization. You can modify the color palette, adjust the axis labels, and even annotate the cells with the actual values.

For more information on how to customize your heatmap, you can refer to the seaborn documentation.

Conclusion

Seaborn’s heatmap provides a simple and effective way to visualize data patterns and relationships. With just a few lines of code, you can create informative and visually pleasing heatmaps in Python.

Whether you are working with correlation matrices, time-series data, or any other type of dataset, seaborn’s heatmap is a valuable tool to include in your data visualization toolbox.