Lecture 29 – Fun with Plotly

Data 94, Spring 2021

In [1]:
from datascience import *
import numpy as np
Table.interactive_plots()
import plotly.express as px
import seaborn as sns

Animated scatter plots

In [2]:
world = Table.from_df(px.data.gapminder())
world
Out[2]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
Afghanistan Asia 1952 28.801 8425333 779.445 AFG 4
Afghanistan Asia 1957 30.332 9240934 820.853 AFG 4
Afghanistan Asia 1962 31.997 10267083 853.101 AFG 4
Afghanistan Asia 1967 34.02 11537966 836.197 AFG 4
Afghanistan Asia 1972 36.088 13079460 739.981 AFG 4
Afghanistan Asia 1977 38.438 14880372 786.113 AFG 4
Afghanistan Asia 1982 39.854 12881816 978.011 AFG 4
Afghanistan Asia 1987 40.822 13867957 852.396 AFG 4
Afghanistan Asia 1992 41.674 16317921 649.341 AFG 4
Afghanistan Asia 1997 41.763 22227415 635.341 AFG 4

... (1694 rows omitted)

In [3]:
px.scatter(world.to_df(),
           x = 'gdpPercap',
           y = 'lifeExp', 
           hover_name = 'country',
           color = 'continent',
           size = 'pop',
           size_max = 60,
           log_x = True,
           range_y = [30, 90],
           animation_frame = 'year',
           title = 'Life Expectancy, GDP Per Capita, and Population over Time'
          )