In most of my previous articles, I’ve often been stretching the importance of visualizing the results obtained by a technical analysis. Ideally, your charts should be able to summarize in a glimpse what you have been working on for days. Plus, those charts have to do so in a way which is clear and comprehensible to your audience.

Hence, it is important to link your findings to a way to present them efficiently and easily. In this article I’m going to show some interactive tools on Plotly which are very powerful yet easy to implement. For this purpose, I’m going to use the embedded dataset ‘gapminder’ and show you some features you can collect through a visual interpretation.

So let’s first download our dataset and necessary libraries:

import plotly.express as px
gapminder = px.data.gapminder()
gapminder.head()

This data is derived from gapminder.org and you can directly import them via Plotly libraries.

So let’s start building our animated charts.

Since we are provided with panel (or longitudinal) data, we are able to observe the variables of each unit (country) across time. Hence, a natural purpose for an animation would be using time-progressing sliders and buttons. This approach will be achieved using the arguments ‘animation_frame’ and ‘animation_group’ within an ordinary plotly chart.

The first plot I want to build displays the evolution across time of life expectancy in different countries, with the support of a map:

fig = px.choropleth(gapminder, locations="iso_alpha",
                    color="lifeExp",
                    hover_name="country", 
                    color_continuous_scale=px.colors.sequential.Plasma,
                    animation_frame="year")
fig.show()
Animated GIF

As you can see, there is a clear upwards pattern in all over the world, even though certain countries (like those in Africa) are systematically exhibiting a lower life expectancy than others (like those in the USA).

Another interesting information to be retrieved is the relationship between gdp and life expectancy, especially the evolution of that relationship across time. A nice way to visualize it is through a scatter plot. Plus, we can also add a further dimension of our analysis, which is the size of the population, so that the larger the bubble, the larger the population.

import plotly.express as px
gapminder = px.data.gapminder()
px.scatter(gapminder, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])
Animated GIF

Nice, now we can add a further argument in our charts, which is the ‘facet_color’. It basically split our chart into sub-charts according to the variable inserted (in our case, ‘continent’). So, if we want to visualize the above scatter plot for each continent, instead of having them all in one chart (which might be a bit messy), we can proceed as follows:

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()
Animated GIF

With the same approach, we can check the joint probability distribution of GDP and life Expectancy using a contour plot, alongside a histogram, across time:

import plotly.express as px

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()
Animated GIF

Similar statistical information can be collected with a heatmap:

fig = px.density_heatmap(gapminder, x="gdpPercap", y="lifeExp", marginal_y="histogram",
                        animation_frame='year', animation_group='country', range_y=[25,100])
fig.show()
Animated GIF

Finally, we can build a bar chart showing the evolution of population within each continent (with the labels of countries) across time:

fig = px.bar(gapminder, x="continent", y="pop",color='lifeExp',
            animation_frame='year', hover_name='country', range_y=[0,4000000000])
fig.show()
Animated GIF

Those are just a few of the potentialities of plotly animations, yet they are extremely powerful to deliver relevant information. If you want to learn more about animations, I recommend the reading of the official documentation here.

Plus, I’m attaching some of my previous articles on Plotly and data visualization in general.

Hoping you enjoyed the reading!

Further readings:

Published by valentinaalto

I'm a 22-years-old student based in Milan, passionate about everything related to Statistics, Data Science and Machine Learning. I'm eager to learn new concepts and techniques as well as share them with whoever is interested in the topic.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: