This chart was generated in Google Colaboratory. Raw Data from IMF open source.
Install Package:
! pip install bar_chart_race
Import Libraries:
import pandas as pd
import numpy as np
import bar_chart_race as bcr
Read Dataset:
df = pd.read_excel("gdp.xlsx")
df
![](https://static.wixstatic.com/media/c12fb6_235dba28c3b24c5d9bf6cf56d989add2~mv2.png/v1/fill/w_980,h_286,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/c12fb6_235dba28c3b24c5d9bf6cf56d989add2~mv2.png)
Shape Dataset (Rows to Columns, Columns to Rows):
df.set_index("Country", inplace=True)
df = df.T
print(f"Dataframe has {df.shape[0]} rows and {df.shape[1]} columns")
df.head()
![](https://static.wixstatic.com/media/c12fb6_5f7c398f05b5404bab5d373f6739c5f3~mv2.png/v1/fill/w_980,h_131,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/c12fb6_5f7c398f05b5404bab5d373f6739c5f3~mv2.png)
Change data type 'object' to 'numeric':
df = df.apply(lambda x: pd.to_numeric(x, errors='coerce') if x.dtype == "object" else x)
Animated Bar Chart Generation (10 bars):
bcr.bar_chart_race(df, n_bars = 10)
There are still plenty of adjustments can be made for the visualization. It's just an example of how easy to create animated chart.
Yorumlar