Plotly Bubble chart animation 만들기
Bubble차트를 animation으로 표현하는 방법을 정리합니다. 데이터는 gapminder를 사용합니다.
import plotly.express as px
df = px.data.gapminder()
df.sample(5 )
585
Ghana
Africa
1997
58.556
18418288
1005.245812
GHA
288
1315
Saudi Arabia
Asia
1987
66.295
14619745
21198.261360
SAU
682
894
Liberia
Africa
1982
44.852
1956875
572.199569
LBR
430
1679
Yemen, Rep.
Asia
2007
62.698
22211743
2280.769906
YEM
887
626
Guinea-Bissau
Africa
1962
34.488
627820
522.034373
GNB
624
데이터를 Animation으로 보여주기 위해서 데이터프레임의 정보를 확인합니다.
<class 'pandas.DataFrame'>
RangeIndex: 1704 entries, 0 to 1703
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 country 1704 non-null str
1 continent 1704 non-null str
2 year 1704 non-null int64
3 lifeExp 1704 non-null float64
4 pop 1704 non-null int64
5 gdpPercap 1704 non-null float64
6 iso_alpha 1704 non-null str
7 iso_num 1704 non-null int64
dtypes: float64(2), int64(3), str(3)
memory usage: 106.6 KB
데이터프레임의 year 컬럼을 이용해서 연도별 흐름을 animation으로 표현할 수 있을 것 같습니다. 좀 더 자세히 year컬럼을 확인합니다.
display(len (df['year' ].unique()))
fig = px.histogram(df, x= 'year' , nbins = 60 )
fig.show()
데이터프레임의 year컬럼은 총 12개의 unique value를 갖고 있습니다. 히스토그램으로 표시하면 5년 단위로 값을 갖는 것을 알 수 있습니다. 이제 year 정보를 이용해서 animation을 구성합니다.
fig = px.scatter(df, x= 'gdpPercap' , y= 'lifeExp' , color= 'continent' , size= 'pop' ,
hover_name= 'country' , log_x= True , animation_frame= 'year' ,
animation_group= 'country' , range_x= [100 , 70000 ], range_y= [25 ,90 ])
fig.show()
시간의 흐름에 따라서 x축과 y축에 표시되는 데이터의 위치가 변경됩니다. 데이터가 최초와 최종 animation단계에서 모두 그래프에 표시될 수 있도록 range_x와 range_y를 이용하여 축의 범위를 수정합니다.
fig = px.bar(df, x= "continent" , y= "pop" , color= "continent" , hover_name= 'country' ,
animation_frame= "year" , animation_group= "country" , range_y= [0 ,4000000000 ])
fig.show()
위와 같이 Bar차트도 동일하게 animation_frame과 animation_group을 설정하여 시간의 흐름에 따른 인구 변화를 나타낼 수 있습니다. hover_name을 country로 설정해서 animation_group의 country 정보를 확인할 수 있습니다.
Date
Title
Author
Aug 18, 2024
Folium에서 Google Map 사용 방법
gabriel yang
Aug 16, 2024
Folium의 Choropleth 지도
gabriel yang
Aug 15, 2024
Folium 마커 생성과 색상 변경하기
gabriel yang
Oct 8, 2023
Plotly line, shape 그리기
gabriel yang
Oct 5, 2023
Plotly Funnel(깔대기) 차트 만들기
gabriel yang
Oct 4, 2023
Plotly 차트의 축 tick 회전 및 폰트 변경하기
gabriel yang
Oct 3, 2023
Plotly 불릿차트
gabriel yang
Oct 2, 2023
Plotly Hover 설정하기
gabriel yang
Sep 30, 2023
Plotly Axis 포멧 변경하기
gabriel yang
Sep 28, 2023
Plotly 마커 모양 변경하기
gabriel yang
Sep 25, 2023
Plotly Time Series 날짜 범위 UI 사용하기
gabriel yang
Sep 22, 2023
Plotly Line Plot만들기
gabriel yang
Sep 21, 2023
Plotly Histogram Plot만들기
gabriel yang
Sep 18, 2023
Plotly Box Plot만들기
gabriel yang
Sep 17, 2023
Plotly Treemap 만들기
gabriel yang
Sep 17, 2023
Plotly Bubble chart 만들기
gabriel yang
Sep 13, 2023
Plotly Subpolt 만들기
양성모
No matching items