도찐개찐

[데이터시각화] 10. 다중그래프 본문

PYTHON/데이터분석

[데이터시각화] 10. 다중그래프

도개진 2023. 1. 2. 13:07

다중 그래프 그리기

  • subplot(행, 열, 번호)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
x = [1,2,3,4]
y = [2,3,5,10]

수평 다중 그래프

plt.subplot(1, 2, 1) # 1행 2열 영역중 1행 1열 영역
plt.plot(x, y, 'r')
plt.subplot(1, 2, 2) # 1행 2열 영역중 1행 2열 영역
plt.plot(x, y, 'b--')

plt.tight_layout()

수직 다중 그래프

plt.subplot(2, 1, 1) # 2행 1열 영역중 1행 1열 영역
plt.plot(x, y, 'r--')
plt.subplot(2, 1, 2) # 2행 1열 영역중 2행 1열 영역
plt.plot(x, y, 'b')
plt.tight_layout()

그래프에 텍스트 삽입하기

  • text(x, y, 텍스트)
  • annotate(텍스트, (x,y), 스타일)
x = [1,2,3,4]
y = [2,3,5,10]
style = {'color': 'red', 'weight': 'bold'}
plt.scatter(x, y)
plt.text(1, 2, 2, fontdict=style)
plt.text(2, 3, 3, fontdict=style)
plt.text(3, 5, 5, fontdict=style)
plt.text(4, 10, 10, fontdict=style)
Text(4, 10, '10')

plt.scatter(x, y)
for i in range(len(x)):
    plt.text(x[i], y[i], y[i], fontdict=style)

colors = ['red', 'orange', 'yellow', 'green']
plt.bar(x, y, color=colors)
for i in range(len(x)):
    plt.text(x[i] - 0.05, y[i] + 0.1, y[i], fontdict={'color':'blue', 'weight': 'bold'})

타이타닉 성별 생존자 시각화

titanic = sns.load_dataset('titanic')
ax = sns.countplot(data=titanic, x='alive', hue='sex')
for p in ax.patches:
    ax.annotate(int(p.get_height()), (p.get_x() + 0.15, p.get_height() + 10),
               color='red', weight='bold')
plt.show()

타이타닉 승선 항구별 생존자 시각화

# colors = ['red', 'orange', 'yellow', 'green']
# plt.bar(x, y, color=colors)
# for i in range(len(x)):
#     plt.text(x[i] - 0.05, y[i] + 0.1, y[i], fontdict={'color':'blue', 'weight': 'bold'})
  survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
886 0 2 male 27.0 0 0 13.0000 S Second man True NaN Southampton no True
887 1 1 female 19.0 0 0 30.0000 S First woman False B Southampton yes True
888 0 3 female NaN 1 2 23.4500 S Third woman False NaN Southampton no False
889 1 1 male 26.0 0 0 30.0000 C First man True C Cherbourg yes True
890 0 3 male 32.0 0 0 7.7500 Q Third man True NaN Queenstown no True

891 rows × 15 columns

728x90

'PYTHON > 데이터분석' 카테고리의 다른 글

[데이터분석] 12. 회귀분석  (0) 2023.01.02
[데이터분석] 11. 상관분석  (0) 2023.01.02
[데이터시각화] 09. 교차표  (0) 2023.01.02
[데이터분석] 08. 박스플롯  (0) 2023.01.02
[데이터시각화] 07. 산점도  (0) 2023.01.02
Comments