- sns.heatmap()
- 히트맵 예시

- attributes
sns.heatmap(
fruit_dict, #values to make heatmap with
cmap=cmap, #colormap
annot=True, #annotation여부
linewidths=.5, #칸 사이사이 줄 두께
fmt='.1%' #퍼센트 표시 포매팅
)
example: step by step
#making df
#the amount of packs sold per day in a supermarket
fruit_season = {
'Watermelon': [54,1037,515,6,34],
'Strawberry': [936,142,575,2009,56],
'Orange': [1511,2521,3498,2501,65],
'Season': ['Spring', 'Summer', 'Autumn', 'Winter','etc']
}
fruit_dict = pd.DataFrame(fruit_season)
fruit_dict

fruit_dict=fruit_dict.set_index('Season')
fruit_dict

#일부 데이터만 뽑아서 히트맵 제작시
season_order= ['Spring', 'Summer', 'Autumn', 'Winter']
fruit_order = ['Watermelon', 'Strawberry']
fruitbySeason = fruit_dict.loc[season_order, fruit_order]
fruitbySeason

#히트맵 제작을 위한 소수점 변환, 한 열을 다 더하면 1이됨
fruitbySeason = fruitbySeason.div((fruitbySeason.sum(axis=0)),axis=1)
fruitbySeason

#히트맵 제작
plt.figure(figsize=(15,5))
cmap = plt.matplotlib.colors.LinearSegmentedColormap.from_list("", ['#c9d8ff', '#8cb2ff','#208fff'])
sns.heatmap(fruitbySeason, cmap=cmap, annot=True, linewidths=.5, fmt='.1%')
plt.suptitle('Fruit Packs Sold by Season', fontfamily='serif', fontsize=15)
plt.title('Different fruits are favored in different seasons.', fontfamily='serif', fontsize=12)
plt.show()

기존 전체데이터로도 히트맵 만들어보기
fruit_dict = fruit_dict.div((fruit_dict.sum(axis=0)),axis=1)
fruit_dict

plt.figure(figsize=(15,5))
cmap = plt.matplotlib.colors.LinearSegmentedColormap.from_list("", ['#c9d8ff', '#8cb2ff','#208fff'])
sns.heatmap(fruit_dict, cmap=cmap, annot=True, linewidths=.5, fmt='.1%')
plt.suptitle('Fruit Packs Sold by Season', fontfamily='serif', fontsize=15)
plt.title('Different fruits are favored in different seasons.', fontfamily='serif', fontsize=12)
plt.show()

하지만 제목이 어디론가 가고있죠..? 어디가니 너ㅋㅋㅋ 제목 위치를 수정해줍시다
plt.figure(figsize=(15,5))
cmap = plt.matplotlib.colors.LinearSegmentedColormap.from_list("", ['#c9d8ff', '#8cb2ff','#208fff'])
sns.heatmap(fruit_dict, cmap=cmap, annot=True, linewidths=.5, fmt='.1%')
plt.suptitle('Fruit Packs Sold by Season', fontfamily='serif', fontsize=15, x=0.44)
plt.title('Different fruits are favored in different seasons.', fontfamily='serif', fontsize=12)
plt.show()
suptitle에 x= 넣어줌

짜잔
'Python_Wiki > Python_Library' 카테고리의 다른 글
| library 업데이트에 따른 경고문 + 대체한 입력 방법 (0) | 2025.06.06 |
|---|---|
| matplotlib: word cloud 워드 클라우드 (2) | 2025.06.06 |
| matplotlib: 꺾은선 그래프 line chart(fill_between) (0) | 2025.06.05 |
| matplotlib/seaborn: bar chart 막대 그래프 (0) | 2025.06.05 |
| matplotlib: pie chart 파이차트 (0) | 2025.06.05 |