Animated Data Visualization using Plotly Express
Last Updated :
05 Sep, 2024
Data Visualization is a big thing in the data science industry and displaying the proper statistics to a business or governments can help them immeasurably in improving their services. It is very painful to understand data from different times from multiple charts and make any sense of it. That is where the need for animated data visualizations lie. In this, article we are going to use Plotly Express for plotting and animating the data and datasets from Gapminder. We are going to look at different types of animation provided by Plotly Express.
Installation
Make sure your have Python 3 installed in your computer. Install Plotly which is going to be used for animating the data. To install it, type the below command in the terminal.
!pip install plotly
Use Jupyter Notebooks or Google Colab if you don't want to install the module locally on your computer.
Importing modules and dataset
We need to import Plotly Express and world data from Gapminder.
import plotly.express as px
gapminder = px.data.gapminder()
Example: Let's print the top few rows of this database
Python
import plotly.express as px
gapminder = px.data.gapminder()
gapminder.head(15)
Output:
Data from GapminderNow we have obtained data from almost all countries in the world from 1952 to 2007 with fields like life expectancy, GDP per capita and population.
Life Expectancy Choropleth
A choropleth is a map that uses differences in color in defined areas about a common property in order to visualize data as an aggregate summary of a region(in this case, a country). Plotly Express makes it easy to plot choropleths. The below code shows how to take information from gapminder. Set the parameter to which you want to color the choropleth to. Here we wanted the choropleth to shade regions on the basis of life expectancy(lifeExp). hover_name shows the set data when hovered over. animation_frame refers to the parameter on which the animation should be done(mostly, this parameter is the time series data).
Example:
Python
import plotly.express as px
gapminder = px.data.gapminder()
gapminder.head(15)
fig = px.choropleth(gapminder,
locations ="iso_alpha",
color ="lifeExp",
hover_name ="country",
color_continuous_scale = px.colors.sequential.Plasma,
scope ="world",
animation_frame ="year")
fig.show()
Output:
scope refers to the area of scope of the choropleth. For example, if we type scope="asia", the following is displayed:
Life Expectancy in AsiaPopulation Bar Graph
Now let's make an animated bar graph using the same dataset using population as our primary data in this instance. Let the x of the data be the continent and y be population and when hovered over the names of the countries should be shown. So let's set hover to 'country'. It is important to specify range as it helps in understanding the scale of the data we are working on. The parameter on which the animation is done is, of course, the year.
Example:
Python
import plotly.express as px
gapminder = px.data.gapminder()
gapminder.head(15)
fig = px.bar(gapminder,
x ="continent",
y ="pop",
color ='lifeExp',
animation_frame ='year',
hover_name ='country',
range_y =[0, 4000000000])
fig.show()
Output:
GDP Per Capita vs Life Expectancy Density Contour
The relationship between the life expectancy and GDP per capita across time is an important component for governmental or national data statistics. Governments can(and have) used this for establishing correlation between the two and this thus explains why the increase in life expectancy has also increased the GDP per capita. The approach can be visualized using a density contour plot and a histogram.
Example:
Python
import plotly.express as px
gapminder = px.data.gapminder()
gapminder.head(15)
fig = px.density_contour(gapminder,
x ="gdpPercap",
y ="lifeExp",
color ="continent",
marginal_y ="histogram",
animation_frame ='year',
animation_group ='country',
range_y =[25, 100])
fig.show()
Output:
GDP Per Capita vs Life Expectancy Scatter Plot
The same approach can be used for a different kind of plot, that is, the scatter plot. The below code shows how it is done. facet_col is used to split our plot into sub-plots of continent data like shown below.
Python
import plotly.express as px
gapminder = px.data.gapminder()
gapminder.head(15)
fig = px.scatter(
gapminder,
x ="gdpPercap",
y ="lifeExp",
animation_frame ="year",
animation_group ="country",
size ="pop",
color ="continent",
hover_name ="country",
facet_col ="continent",
size_max = 45,
range_y =[25, 90]
)
fig.show()
Output:
Conclusion
Thus, in this article, we have learnt to code the following using Plotly Express and Python.
- Animated Choropleth
- Animated Bar Graph
- Animated Density Contour Plot
- Animated Scatter Plot
The same knowledge can be extended for more complex datasets, for generating animated visualizations and can be used with forecasting models to animate forecasted data.
Get complete notebook link here :
Notebook: click here.
Similar Reads
Covid-19 Analysis and Visualization using Plotly Express
In this article, we will discuss Analyse Covid-19 data and will visualize it using Plotly Express in Python. This article deals with creating dozens of bar charts, line graphs, bubble charts, scatter plots. The graph that will be made in this project will be of excellent quality. Envisioning COVID-1
12 min read
Data Visualization using Plotnine and ggplot2 in Python
Plotnoine is a Python library that implements a grammar of graphics similar to ggplot2 in R. It allows users to build plots by defining data, aesthetics, and geometric objects. This approach provides a flexible and consistent method for creating a wide range of visualizations. It is built on the con
7 min read
Data Visualization using Matplotlib in Python
Matplotlib is a widely-used Python library used for creating static, animated and interactive data visualizations. It is built on the top of NumPy and it can easily handles large datasets for creating various types of plots such as line charts, bar charts, scatter plots, etc. These visualizations he
10 min read
Interactive visualization of data using Bokeh
Bokeh is a Python library for creating interactive data visualizations in a web browser. It offers human-readable and fast presentation of data in an visually pleasing manner. If youâve worked with visualization in Python before, itâs likely that you have used matplotlib. But Bokeh differs from matp
4 min read
Plotly for Data Visualization in Python
Plotly is an open-source Python library for creating interactive visualizations like line charts, scatter plots, bar charts and more. In this article, we will explore plotting in Plotly and covers how to create basic charts and enhance them with interactive features.Introduction to Plotly in PythonP
12 min read
Data Visualization Using Bqplot in Python
Bqplot is a powerful and flexible tool for creating interactive visualizations within Jupyter Notebooks. Its integration with ipywidgets and use of D3.js make it a valuable library for data scientists and developers who need to build interactive data visualization applications. What is BqPlot in Pyt
9 min read
Interactive Data Visualization with Plotly Express in R
Data Visualization in R is the process of representing data so that it is easy to understand and interpret. Various packages are present in the R Programming Language for data visualization. Plotly's R graphing library makes interactive, publication-quality graphs. Plotly can be used to make various
8 min read
Dynamic Visualization using Python
Data visualization in Python refers to the pictorial representation of raw data for better visualization, understanding, and inference. Python provides various libraries containing different features for visualizing data and can support different types of graphs, i.e. Matplotlib, Seaborn, Bokeh, Plo
11 min read
Interactive Data Visualization with Python and Bokeh
In this article, we'll learn how to do Interactive Data Visualization with Bokeh. Bokeh is a Python library that is used for creating interactive visualizations for modern web browsers. It handles custom or specialized use cases very simply. Â It provides high-performance interactive charts and plots
8 min read
Animated Scatter Plots in Plotly for Time-Series Data
Time-series data consists of observations collected at regular intervals over time, often used in fields such as finance, meteorology, and economics. One powerful way to visualize this type of data is through animated scatter plots, which not only display the data points but also reveal changes and
5 min read