Plotly Bubble chart animation 만들기
Bubble차트를 animation으로 표현하는 방법을 정리합니다. 데이터는 gapminder
를 사용합니다.
import plotly.express as px
df = px.data.gapminder()
df.sample(5 )
641
Haiti
Americas
1977
49.923
4908554
1874.298931
HTI
332
714
Indonesia
Asia
1982
56.159
153343000
1516.872988
IDN
360
967
Mauritania
Africa
1987
56.145
1841240
1421.603576
MRT
478
1316
Saudi Arabia
Asia
1992
68.768
16945857
24841.617770
SAU
682
1421
Spain
Europe
1977
74.390
36439000
13236.921170
ESP
724
데이터를 Animation으로 보여주기 위해서 데이터프레임의 정보를 확인합니다.
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1704 entries, 0 to 1703
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 country 1704 non-null object
1 continent 1704 non-null object
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 object
7 iso_num 1704 non-null int64
dtypes: float64(2), int64(3), object(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
Jan 1, 3000
전체 카테고리
gabriel yang
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