오답노트

[seaborn] 편리한 차트 본문

Python/seaborn

[seaborn] 편리한 차트

권멋져 2022. 8. 11. 20:31

countplot

범주형 데이터를 bar 형태로 나타낼때는 집계를 먼저해야한다.

하지만 conutplot은 범주형 데이터를 입력하면 집계 작업후 bar 형태로 출력한다.

 

sns.countplot(x="Embarked", data=titanic, hue = 'Survived')
plt.show()

1.1 countplot

barplot

평균을 비교하는 bar 차트를 출력한다.

가운데 직선은 신뢰구간을 의미한다.

 

sns.barplot(x="Embarked", y="Fare", data = titanic)
plt.show()

 

2.1 barplot

 

heatmap

두 범주를 집계한 결과를 색의 농도로 표현한다.

집계와 피봇을 먼저 작업해야한다.

범주의 개수가 여러개 일 때 유용하게 사용할 수 있다.

 

집계는 pandas의 groupby 함수로

피봇은 pandas의 pivot 함수 구한다.

temp1 = titanic.groupby(['Embarked','Pclass'], as_index = False)['PassengerId'].count()
temp2 = temp1.pivot('Embarked','Pclass', 'PassengerId')
print(temp1)
print('---')
print(temp2)

'''

  Embarked  Pclass  PassengerId
0        C       1           85
1        C       2           17
2        C       3           66
3        Q       1            2
4        Q       2            3
5        Q       3           72
6        S       1          127
7        S       2          164
8        S       3          353
---
Pclass      1    2    3
Embarked               
C          85   17   66
Q           2    3   72
S         127  164  353

'''


sns.heatmap(temp2, annot = True)
plt.show()

3.1 heatmap

'Python > seaborn' 카테고리의 다른 글

[seaborn] 복합 차트  (0) 2022.08.11
[seaborn] seaborn 과 차트들  (0) 2022.08.11