MITB Banner

Hands-On Guide To Animating Visualizations With Plotly

In this article, we will learn about the different animated graphs through plotly and implement each of them.

Share

Visualization of data is a means of effectively communicating ideas and important information about the dataset. It is an important skill for a data scientist to have good data visualizations in the project. One of the reasons why visualizations are important is because they can be used to convey a lot about the data in a very easy manner since they are graphical in nature. To make these graphs more interesting and suitable for all, plotly allows us to animate these graphs. 

In this article, we will learn about the different animated graphs through plotly and implement each of them. 

Let us get started with plotly animations. The advantages of using this library are that it is available both in R and Python and they are easily integrated into Colab, R Studio and Jupyter notebook. Before we move on to the implementation, make sure you have installed the plotly library. You can install this with the pip command

pip install plotly

Animated Bar Charts

Animations in plotly are a great way of analysing how data changes with time and can help you understand the distribution of data over a particular time period. Bar charts can be animated as follows. 

The dataset

I will select a dataset with a time series to make the visualization more impactful. Here I have chosen the car dataset from Kaggle. You can download the dataset by clicking on this link

Let us load the dataset and import the required libraries

import pandas as pd
import numpy as np
import plotly.express as px
data=pd.read_csv('USA_cars_datasets.csv')
data.head()
visualization

As you see there are different car models and brands and the prices along with the year they were sold. Let us now plot an animated bar chart that represents the models according to the prices and the time. 

animated = px.bar(data,
             y="model",
             x="price",
             animation_frame="year",
             orientation='h',
             color="color")
animated.update_layout(width=800,
                  height=800,
                  paper_bgcolor='rgba(0,0,0,0)',
                  plot_bgcolor='rgba(0,0,0,0)',
                  title_text='Cars',
                  showlegend=False)
animated.update_xaxes(title_text='car sales')
animated.show()

Along the x-axis, we have the price of the car and the y-axis is the model of the car. 

As you can see above, the animations are happening according to the year. The data for each year is shown as and when the time elapses. You can stop whenever needed or manually move the timeline if you wish. 

visualization

If you hover over the image in any part of the bar, you can see the year, the price, the colour and the model of the car that was sold

Animated Scatter plots

Now that we saw how the bar charts are animated, in a very similar manner we can animate our scatter plots as well. Since they are quite similar I will use the same dataset as used above even for this implementation. But here for the x-axis I will choose mileage and y-axis will denote the price of the car. 

animated = px.scatter(
    data,
    x="mileage",
    y="price",
    animation_frame="year",
    color="color",
  )
animated.update_layout(width=800,
                  height=800,
                  paper_bgcolor='rgba(0,0,0,0)',
                  plot_bgcolor='rgba(0,0,0,0)',
                  title_text='Cars',
                  showlegend=False)
animated.update_xaxes(title_text='car sales')
animated.show()

As you can see I have stopped the animations and hovering over a data point will give information about that data. 

scatter

Parallel plots

Parallel plots are a great way to understand the relationship between different categorical values. Since they are best suited for categorical data we will use another dataset here. I will use the waiter tips dataset that can be downloaded directly from the plotly library. 

import plotly.express as px
tips = px.data.tips()
tips

Now, we can go ahead and build a parallel plot as follows

parallel = px.parallel_categories(tips, dimensions=['sex', 'smoker', 'day'],
                color="size", color_continuous_scale=px.colors.sequential.Inferno,
                labels={'sex':'gender', 'smoker':'Smokers', 'day':'day'})
parallel.show()
visualization

This graph allows you to drag and drop, explore values and even highlight the different values. It is also a good technique to identify outliers in the dataset. 

Sunburst plot

This is a plot that allows you to plot pie charts in an interactive way. You can use group by statements well with these plots. These plots visualize data that moves from root outwards to the leaf. 

tips = px.data.tips()
sunburst = go.Figure(go.Sunburst(
    labels=["Female", "Male", "Dinner", "Lunch","Sun","Sat","Mon","Thur","Fri","Tue","Wed"],
    parents=["", "", "Female", "Female", 'Male', 'Male',"Female","Male","Female","Male","Female"],
    values=np.append(
        tips.groupby('sex').tip.mean().values,
        tips.groupby(['sex', 'time','day']).tip.mean().values),
    marker=dict(colors=px.colors.sequential.Inferno)),
sunburst.show()
sunburst

You can iteratively see the information in the pie chart and these can be great visualization tools for presentations and for analysing the categorical data. 

As you can see the tips on Friday for a female can be seen by hovering over the labels. 

Conclusion

In this article, we saw how to implement different methods for animating visualisations to give a better understanding of the project. With such visualization and charts, it makes a better impact on communicating the information hidden in the data and are easy to implement as well. 

Share
Picture of Bhoomika Madhukar

Bhoomika Madhukar

I am an aspiring data scientist with a passion for teaching. I am a computer science graduate from Dayananda Sagar Institute. I have experience in building models in deep learning and reinforcement learning. My goal is to use AI in the field of education to make learning meaningful for everyone.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.