본문 바로가기
Big Data/Visualization

sns.heatmap() 알파값 조정

by Wikinist 2023. 8. 25.

아래의 코드는 sns.heatmap()를 사용하여 히트맵을 그리고, 그 히트맵의 알파값을 조정하여 투명도를 설정하는 방법을 보여줍니다.

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Sample scaled data (replace this with your data)
scaled_data = np.random.randn(5, 5)
scaled_df = pd.DataFrame(scaled_data)

plt.figure(figsize=(8, 6))

# Create the heatmap using sns.heatmap()
heatmap = sns.heatmap(scaled_df, annot=False, cmap="RdBu", fmt=".2f")

# Get the first child of the heatmap (this corresponds to the color mapping)
color_mapping = heatmap.get_children()[0]

# Adjust the alpha value for the color mapping (0.0 to 1.0)
alpha_value = 0.7
color_mapping.set_alpha(alpha_value)

plt.title("Heatmap with Adjusted Transparency")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

plt.show()

이 코드에서 heatmap.get_children()[0]는 히트맵의 컬러 매핑을 나타냅니다. 컬러 매핑에 set_alpha() 메서드를 사용하여 투명도를 조정할 수 있습니다. alpha_value 변수의 값을 조정하여 원하는 투명도를 얻을 수 있습니다. 0에 가까울수록 더 투명하게, 1에 가까울수록 불투명하게 됩니다.

투명도가 조정된 히트맵을 출력하는 위 코드를 실행하면 투명도가 적용된 히트맵을 확인할 수 있을 것입니다.

해당 게시글은 ChatGPT의 도움을 받아 작성되었습니다.

'Big Data > Visualization' 카테고리의 다른 글

[scikit-plot] plot_confusion_matrix  (0) 2023.09.07
Matplotlib GridSpec  (0) 2023.08.25
Dash  (0) 2023.08.17
Plotly  (0) 2023.08.17
seaborn barplot()  (0) 2023.08.16