Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 자바스크립트
- 앱
- streamlit
- JQuery
- 프로젝트
- 스프링
- javascript
- kotlin
- HTML
- 자바
- 안드로이드
- 판다스
- Android
- 파이썬
- 오라클
- pandas
- 제이쿼리
- 시간
- CSS
- 기록
- 코틀린
- java
- MLB
- 어플
- Python
- gcp
- mlb stats api
- oracle
- 배포
- Spring
Archives
- Today
- Total
develope_kkyu
[Python] plotly를 이용해 오타니, 저지 2022시즌 타격 성적 비교 그래프 본문
728x90
https://developerkkyu37.tistory.com/64
[Python] MLB Stats API를 이용해 오타니, 저지 2022시즌 타격 성적 비교
패키지 설치 및 임포트 !pip install MLB-StatsAPI import statsapi 오타니와 저지 선수 id 검색 print(statsapi.lookup_player('ohtani')) print(statsapi.lookup_player('judge')) [{'id': 660271, 'fullName': 'Shohei Ohtani', 'firstName': 'Shohei',
developerkkyu37.tistory.com
plotly를 이용해서 2022 성적 비교 그래프 만들기
패키지 설치 및 임포트
!pip install plotly
import plotly.offline as pyo
import plotly.graph_objs as go
ohtani = statsapi.player_stat_data(660271, group="[hitting]", type="season")['stats'][0]['stats']
judge = statsapi.player_stat_data(592450, group="[hitting]", type="season")['stats'][0]['stats']
sr_o = pd.Series(ohtani, name = 'Ohtani')
sr_j = pd.Series(judge, name = 'Judge')
df
앞서 만들었던 데이터 프레임에서 타율, 출루율, 장타율, ops 데이터프레임과 득점, 2루타, 3루타, 홈런, 삼진, 안타, 타점 데이터프레임을 추출해보자.
df_hit1 = df.iloc[[12, 14, 15, 16]]
df_hit2 = df.iloc[[3, 4, 5, 6, 7, 10, -8]]


먼저, 득점, 2루타, 3루타, 홈런, 삼진, 안타, 타점 비교 그래프를 만들어서 비교해보자
ohtani = go.Bar(x=df_hit2['Ohtani'].keys(), y=df_hit2['Ohtani'].values,
marker = {'color': 'red'},
name='Ohtani')
judge = go.Bar(x=df_hit2['Judge'].keys(), y=df_hit2['Judge'].values,
marker = {'color': 'black'},
name='Judge')
data = [ohtani, judge]
layout = go.Layout(title='주요 타격 지표 비교')
fig = go.Figure(data=data, layout=layout)
pyo.iplot(fig)

다음으로, 타율, 출루율, 장타율, ops 비교 그래프를 만들어보자
ohtani = go.Bar(x=df_hit1['Ohtani'].keys(), y=df_hit1['Ohtani'].values,
marker = {'color': 'red'},
name='Ohtani')
judge = go.Bar(x=df_hit1['Judge'].keys(), y=df_hit1['Judge'].values,
marker = {'color': 'black'},
name='Judge')
data = [ohtani, judge]
layout = go.Layout(title='주요 타격 지표 비교')
fig = go.Figure(data=data, layout=layout)
pyo.iplot(fig)

데이터에 소수점을 인식하지 못해 이상한 그래프가 나오게 된다.
데이터의 값을 바꿔주고 그래프를 그려보자
df_hit1.loc['avg', 'Ohtani'] = 0.273
df_hit1.loc['avg', 'Judge'] = 0.311
df_hit1.loc['obp', 'Ohtani'] = 0.356
df_hit1.loc['obp', 'Judge'] = 0.425
df_hit1.loc['slg', 'Ohtani'] = 0.519
df_hit1.loc['slg', 'Judge'] = 0.686
df_hit1.loc['ops', 'Ohtani'] = 0.875
df_hit1.loc['ops', 'Judge'] = 1.111
df_hit1

저지가 MVP 받을만 했다...
728x90
'Python' 카테고리의 다른 글
[Python] ploty를 이용해 내셔널리그 골든글러브 최종 후보 3인 수비 지표 그래프 만들기 (0) | 2023.01.19 |
---|---|
[Python] MLB Stats API를 이용해 내셔널리그 골든글러브 후보 3인 수비 지표 비교(김하성 포함) (0) | 2023.01.19 |
[Python] MLB Stats API를 이용해 오타니, 저지 2022시즌 타격 성적 비교 (1) | 2023.01.19 |
[Python] pandas 예제 - 5 (0) | 2023.01.17 |
[Python] pandas 예제 - 4 (0) | 2023.01.05 |