Image Courtesy: Bar Chart Animation with Plotly library
We all love animation! We can update our photos on Instagram, Facebook, or any other social media platform with just a click. We live in a big data era where everything is a click away. We have seen often that in a meeting we present our findings in a presentation form rather than in a tabular format in excel or sometimes we also illustrate the data in a simple dashboard format for easy understanding. There is an abundance of ways to represent the data but the most effective way is to make it look like a movie or a film where you are depicting a story with a higher engagement ratio.
For example, if a movie is good then it is going to make a significant rise in revenue from it and the shows are going to be booked. Take this example, in our life as we easily get affected by a short film rather than reading a story of hundreds of pages of poetry. Why? The main reason is our mind processes the things which are present to us in a pictorial form faster than in reading format.
Here, comes the power of visualization which is a great tool to have in our pocket it works the same as harry potter’s magic wand! One can easily use the magic wand to make eye-catching infographics, and charts that will hook up the audience in the first 90 seconds. Visualization is the main powerhouse of exploratory data analysis from where one can understand what is data trying to convey.
There is no pre-defined format of visualization. Also, not all charts are feasible for every use case. Here, in this particular article, we are going to see one of the highly utilized charts “Animation Charts”.
Plotly Python library has an amazing library for making easy, understandable, and lessen effort charts with extraordinary animation charts.
Let’s see how to make animated charts.
Importing library for visualizing and reading the dataset
import plotly.express as px import pandas as pd
The data looks like this after importing the libraries for animation
Python Code:
import plotly.express as px
import pandas as pd
df = px.data.gapminder()
print(df)
Image Courtesy: The look of the data
Let’s make an animated bar chart having a year as the dependent axis. First, we will see what is the population of each continent with respect to the year.
fig = px.histogram(df, x="continent", y="pop", color="continent", animation_frame="year", animation_group="country", range_y=[0,4000000000]) fig.show()
Image Courtesy: Plotly animation bar chart
Normal chart parameters descriptions
Animated chart parameters descriptions
We can also add other parameters to make it look rich and eye-catching! Let’s see how can we achieve that look.
Personally, I love dark shade colors in which black is my favorite color, So, let’s use it in the chart. Most of the colors which are used for the background are black, white, or creme. There is a direct parameter for using white as the background color in the chart.
First and foremost we will keep the same parameters to make animated chart with plotly we will just add some parameters to enhance the chart.
fig = px.histogram(df, x="continent", y="pop", color="continent", animation_frame="year", animation_group="country", range_y=[0,4000000000], color_discrete_sequence=px.colors.qualitative.T10) fig.update_yaxes(visible=False) fig.update_layout(margin=dict(t=70, b=0, l=50, r=40), width=900, height=450, xaxis_title=' ', plot_bgcolor='#333', paper_bgcolor='#333', title_font=dict(size=25, color='#a5a7ab', family="Lato, sans-serif"), font=dict(color='#8a8d93'), legend=dict(orientation="h", yanchor="bottom", y=1, xanchor="right", x=0.8) ) fig.show()
Image Courtesy: The final look of animated bar chart plotly
The color and background parameters descriptions
Axis + Basic parameters descriptions
The font and title parameters descriptions
Image Courtesy
All the images used while making the animated bar chart with the help of plotly are of the author.
To sum up, we saw how we can make an infographic view animated chart with the help of the plotly library. Also, we saw how we add the parameters to make it more eye-catching charts which we can easily embed in any blog or article to make the analysis pop out for the audience. Some of the key takeaways are:
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.