seaborn
✔️ 비율막대그래프
from matplotlib import axes
# 그래프 객체 생성
fig = plt.figure(figsize = (12, 5))
ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)
# 막대 그래프 & 에러바
sns.barplot(x = 'sex', y = 'survived', data = titanic, ax = ax1)
sns.barplot(x = 'sex', y = 'survived', hue = 'class', data = titanic, ax = ax2)
sns.barplot(x = 'sex', y = 'survived', hue = 'class', dodge = False, data = titanic, ax = ax3)
# 줄 같이 생긴 것 = 에러바(표준편차, 표준오차, 신뢰구간 등)
plt.show()
옵션
- hue : 변수 추가
- dodge = False → 누적막대그래프 형식
- errorbar = None → 에러바 제거
✔️ 빈도막대그래프
# 그래프 객체 생성
fig = plt.figure(figsize = (12, 5))
ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)
# 막대 그래프
sns.countplot(x = 'class', data = titanic, ax = ax1)
sns.countplot(x = 'class', hue = 'who', data = titanic, ax = ax2)
sns.countplot(x = 'class', hue = 'who', dodge = False, data = titanic, ax = ax3)
plt.show()
옵션
- hue : 변수 추가
- dodge = False → 누적막대그래프 형식
- y변수 기입 x
✔️ 상자그림 & 바이올린그림
# 그래프 객체 생성
fig = plt.figure(figsize = (14, 10))
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)
# 상자그림
sns.boxplot(x = 'alive', y = 'age', data = titanic, ax = ax1)
sns.boxplot(x = 'alive', y = 'age', hue = 'sex', data = titanic, ax = ax2)
# 바이올린 그림
sns.violinplot(x = 'alive', y = 'age', data = titanic, ax = ax3)
sns.violinplot(x = 'alive', y = 'age', hue = 'sex',
data = titanic, ax = ax4)
plt.show()
✔️ 조인트 그림
# 한글 폰트 지정
plt.rc("font", family = 'NanumGothic')
# 조인트 그림 - 산점도(기본값)
jp1 = sns.jointplot(x = 'fare', y = 'age', data = titanic )
# 조인트 그림 - 산점도 + 회귀선
jp2 = sns.jointplot(x = 'fare', y = 'age', kind = 'reg', data = titanic )
# 조인트 그림 - 육각 산점도
jp3 = sns.jointplot(x = 'fare', y = 'age', kind = 'hex', data = titanic )
# 조인트 그림 - 밀도 함수
jp4 = sns.jointplot(x = 'fare', y = 'age', kind = 'kde', data = titanic )
# 제목
jp1.fig.suptitle("산점도")
jp2.fig.suptitle("산점도 + 회귀선")
jp3.fig.suptitle("육각 산점도")
jp4.fig.suptitle("밀도 함수")
plt.show()
✔️ 그리드 분할
# 그리드 분할 = 빈도표를 만들듯이
grid = sns.FacetGrid(data = titanic,
row = 'survived',
col = 'who')
# 그래프 넣기
grid.map(plt.hist, 'age')
plt.show()
✔️ pairplot
# 변수들을 2개씩 짝을 지어서 시각화
pair_data = titanic[['age', 'pclass', 'fare']]
sns.pairplot(pair_data)
plt.show()
지도 시각화
✔️ Folium
파이썬에서 제공하는 지도 시각화 도구
이름 → 주소 → 위도 경도(지오코딩)
# 서울 지도 만들기 - 기본 마커
seoul_map = folium.Map(location = [37.55, 126.98]
, zoom_start = 12 # 초기 확대 범위
, tiles = 'OpenStreetMap') # 지도 스타일
# 마커 표시
for name, lat, lng in zip(df.index, df.위도, df.경도):
folium.Marker([lat, lng], popup = name).add_to(seoul_map)
# 파일로 저장
seoul_map.save('./seoul_univ.html')
------------------------------------------------------------------
# 서울 지도 만들기 - 원형 마커
seoul_map = folium.Map(location = [37.55, 126.98], zoom_start = 12,
tiles = 'Stamen Terrain')
# 마커 표시
for name, lat, lng in zip(df.index, df.위도, df.경도):
folium.CircleMarker([lat, lng],
popup = name, # 튀어나오게 할 것
radius = 10, # 원 크기
fill_color = 'coral', # 원 내부 색상
fill = True, # 내부 채움 여부
fill_opacity = 0.3, # 투명도
color = 'black' # 원의 색상
).add_to(seoul_map)
# 파일로 저장
seoul_map.save('./seoul_univ1.html')
✔️ 단계구분도
단계별 색상이 구분된 맵 생성
- ColorBrewer code : 컬러코드
# 경기도 지도 만들기
ggd_map = folium.Map(location = [37.55, 126.98], zoom_start = 9,
tiles = 'Stamen Terrain')
# 단계 구분도 - 2007년
year = "2007"
folium.Choropleth(geo_data = bnd, data = df[year], # 지도 데이터 = 경기도 경계선
columns = [df.index, df[year]], # 인구 데이터 = 색깔로 표현한 값
fill_color = 'Reds', # ColorBrewer code
fill_opacity = 0.3, # 다각형 내부 색상 투명도
line_opacity = 0.5, # 선의 투명도
key_on = 'feature.properties.name' # 지도 데이터와 인구 데이터를 연결하는 내용 = 지역 이름
).add_to(ggd_map)
# 파일로 저장
ggd_map.save('./ggd_' + year + '.html
'Python' 카테고리의 다른 글
[Python][한국복지패널데이터] 주제 1_성별에 따른 월급 차이 (0) | 2023.06.08 |
---|---|
[Python] 데이터 전처리 (0) | 2023.06.04 |
[python] 데이터 전처리 - 널값 처리 (0) | 2023.06.01 |
[Python] raw string (0) | 2023.05.18 |
[파이썬] 날짜 관련 함수 (0) | 2023.04.15 |