How To Create An Animated Bar Chart (Bar Race) In Python Using Plotly & Excel
In this tutorial I will show you, how to create an animated Bar Chart, also known as a Barrace, in Python & Excel by using the library called Plotly.
The data is coming directly from an excel file.
You can do all the changes in the excel file and after running the script again you will have your updated Bar Chart Animation.
The Barrace is also interactive. If you hover over the bars, you will see additional information. Lastly, the animation will be also saved as an HTML file. Hence, you could also send the animation to your friends & colleagues, who do not even need to have Python installed on their machines.
This tutorial is focusing on beginners in Python. Even you have never written a single line of code in Python, you will be able to have your free animated bar chart by the end of this tutorial.
import plotly.express as px import pandas as pd import plotly import sys import os os.chdir(sys.path) # Read Data From Excel and store in a variable df [dataframe] df = pd.read_excel('data.xlsx') # Store each column in a seperate varibale. country = df['Country'] sales = df['Sales'] date = df['Date'].dt.strftime('%Y-%m-%d') # Create Animated Bar Chart and store figure as fig fig = px.bar( df, x=country, y=sales, color=country, animation_frame=date, animation_group=country, range_y=[0,1200] ) # Save Chart and export to HTML plotly.offline.plot(fig,filename='Barrace.html')