Plotly Animation 만들기

Plotly Animation 만들기

Data Visualization
Plotly Animation의 사용방법을 정리합니다.
Author

gabriel yang

Published

September 18, 2023

Plotly Bubble chart animation 만들기

Bubble차트를 animation으로 표현하는 방법을 정리합니다. 데이터는 gapminder를 사용합니다.

import plotly.express as px
df = px.data.gapminder()
df.sample(5)
country continent year lifeExp pop gdpPercap iso_alpha iso_num
1077 Nepal Asia 1997 59.426 23001113 1010.892138 NPL 524
1286 Rwanda Africa 1962 43.000 3051242 597.473073 RWA 646
1215 Philippines Asia 1967 56.393 35356600 1814.127430 PHL 608
1655 Vietnam Asia 2007 74.249 85262356 2441.576404 VNM 704
1151 Norway Europe 2007 80.196 4627926 49357.190170 NOR 578

데이터를 Animation으로 보여주기 위해서 데이터프레임의 정보를 확인합니다.

df.info()
<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()
12

데이터프레임의 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_xrange_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_frameanimation_group을 설정하여 시간의 흐름에 따른 인구 변화를 나타낼 수 있습니다. hover_namecountry로 설정해서 animation_groupcountry 정보를 확인할 수 있습니다.

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
Back to BLOG LIST