by Dhafer Malouche
I have recently created a data about Research and Development from the WDI data (https://data.worldbank.org/). This data is now available in my website (https://malouche.github.io/data_in_class/RD_data.html) and it can be downloaded in different kind of formats csv, xls... and so on.
The aim of this tutorial is to show how you draw a gapminder animation using this data. I will show different examples of scatter plots.
Let's first import the needed python librairies
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set_style("white")
import pandas as pd
my_dpi=96
I will now get the data after omitting all the missing values in the data
data = pd.read_csv("data_RD.csv")
Preview of the data
data.head()
I will be using now the plotly_express that helps to make a Gapminder Animation in 2 lines.
I first import plotly_express
library
import plotly_express as px
I will start with a scatter plot Expenditure in RD by Researcher x Cost of one article. Both indicators are expressed in current USD. Let's first check some statistics about our variables.
data['ExpOneRD'].min()
data['ExpOneRD'].max()
data['ExpOneArt'].min()
data['ExpOneArt'].max()
fig=px.scatter(data, x="ExpOneRD", y="ExpOneArt", animation_frame="year", animation_group="country",height=600,width=1000,
size="per_g", color="income", hover_name="country",size_max=50,log_x=True,log_y=True,text="iso2c",
range_x=[2000,450000], range_y=[35000,5500000],
labels=dict(ExpOneRD="Expenditure by one researcher (Current USD)",
ExpOneArt="Cost of one article (Current USD)",
per_g="Research and development expenditure (% of GDP)"))
We will now open the graph in a separate file that can be used to be inserted in your website.
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
plot(fig)