0% found this document useful (0 votes)
3 views

Interactive Visualization - Jupyter Notebook

The document details the process of creating an interactive visualization using Plotly in a Jupyter Notebook. It includes steps for installing necessary libraries, importing data, cleaning the dataset, and generating a scatter plot animated by year. The visualization is designed to display the relationship between GDP per capita and life expectancy across different countries over time.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Interactive Visualization - Jupyter Notebook

The document details the process of creating an interactive visualization using Plotly in a Jupyter Notebook. It includes steps for installing necessary libraries, importing data, cleaning the dataset, and generating a scatter plot animated by year. The visualization is designed to display the relationship between GDP per capita and life expectancy across different countries over time.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11/2/22, 10:15 PM Interactive Visualization - Jupyter Notebook

In [41]: 1 pip install plotly

Defaulting to user installation because normal site-packages is not writeable


Requirement already satisfied: plotly in /data/urk20ai1038/.local/lib/python3.6/site-packages (5.11.0)
Requirement already satisfied: tenacity>=6.2.0 in /data/urk20ai1038/.local/lib/python3.6/site-packages (from p
lotly) (8.1.0)
Note: you may need to restart the kernel to use updated packages.

In [1]: 1 import numpy as np


2 import pandas as pd
3 import plotly.express as px

In [2]: 1 df = px.data.gapminder()
2 df.head()

Out[2]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num

0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4

1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4

2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4

3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4

4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4

In [3]: 1 df.dropna()
2 df.isna().sum()

Out[3]: country 0
continent 0
year 0
lifeExp 0
pop 0
gdpPercap 0
iso_alpha 0
iso_num 0
dtype: int64

https://datalab.karunya.edu/user/urk20ai1038/notebooks/Data Visualization/Interactive Visualization.ipynb 1/4


11/2/22, 10:15 PM Interactive Visualization - Jupyter Notebook

In [4]: 1 df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1704 entries, 0 to 1703
Data columns (total 8 columns):
country 1704 non-null object
continent 1704 non-null object
year 1704 non-null int64
lifeExp 1704 non-null float64
pop 1704 non-null int64
gdpPercap 1704 non-null float64
iso_alpha 1704 non-null object
iso_num 1704 non-null int64
dtypes: float64(2), int64(3), object(3)
memory usage: 106.6+ KB

In [5]: 1 df = df.iloc[:,:-2]
2 df.head(2)

Out[5]:
country continent year lifeExp pop gdpPercap

0 Afghanistan Asia 1952 28.801 8425333 779.445314

1 Afghanistan Asia 1957 30.332 9240934 820.853030

https://datalab.karunya.edu/user/urk20ai1038/notebooks/Data Visualization/Interactive Visualization.ipynb 2/4


11/2/22, 10:15 PM Interactive Visualization - Jupyter Notebook

In [6]: 1 x_title = "gdpPercap"


2 y_title = "lifeExp"
3 animation_frame = 'year'
4 animation_group = 'country'
5 size_of_plot = 'pop'
6 color_category = 'continent'
7 xmin = 100
8 xmax = 100000
9 ymin = 25
10 ymax = 90
11 maximum_size_plot = 55
12 px.scatter(df,
13 x=x_title,y=y_title,
14 animation_frame=animation_frame,
15 animation_group=animation_group,
16 size=size_of_plot,
17 color=color_category,
18 hover_name="country",
19 log_x=True,
20 size_max=maximum_size_plot,
21 range_x=[xmin, xmax],
22 range_y=[ymin, ymax]
23 )

90

80

70
lifeExp

60

50

40
https://datalab.karunya.edu/user/urk20ai1038/notebooks/Data Visualization/Interactive Visualization.ipynb 3/4
11/2/22, 10:15 PM Interactive Visualization - Jupyter Notebook
40

30

2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9
100 1000 10k 10

gdpPercap

year=2007
▶ ◼

In [ ]: 1 ​

https://datalab.karunya.edu/user/urk20ai1038/notebooks/Data Visualization/Interactive Visualization.ipynb 4/4

You might also like