from datascience import *
import numpy as np
Table.interactive_plots()
import plotly.express as px
import seaborn as sns
world = Table.from_df(px.data.gapminder())
world
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'
)
px.histogram(world.to_df(),
x = 'lifeExp',
animation_frame = 'year',
range_x = [20, 90],
range_y = [0, 50],
title = 'Distribution of Life Expectancy over Time')
world_latest = world.where('year', 2007)
world_latest
px.box(world_latest.to_df(),
y = 'lifeExp',
x = 'continent',
color = 'continent',
hover_name = 'country',
title = 'Distribution of Life Expectancy in 2007 by Continent'
)
world_latest.where('continent', 'Americas')
px.pie(world_latest.where('continent', 'Americas').to_df(),
values = 'pop',
names = 'country',
title = 'Population of the Americas'
)
world_for_pie = world_latest \
.group('continent', sum) \
.select('continent', 'pop sum')
world_for_pie
px.pie(world_for_pie.to_df(),
values = 'pop sum',
names = 'continent',
title = 'World Population by Continent')
phases = [
['Newborn', '1998-11-26', '1999-11-26', 'Canada'],
['Toddler, Preschooler', '1999-11-26', '2005-09-03', 'US'],
['Elementary School Student', '2005-09-03', '2009-06-30', 'Canada'],
['Middle School Student', '2009-09-15', '2012-06-15', 'Canada'],
['High School Student', '2012-09-05', '2016-05-30', 'Canada'],
['Undergrad @ UC Berkeley', '2016-08-22','2020-05-15', 'US'],
['Masters @ UC Berkeley', '2020-08-25', '2021-05-14', 'Canada'],
['Teaching Data 94', '2021-01-20', '2021-05-14', 'Canada']]
phases_table = Table(labels = ['Phase', 'Start', 'End', 'Location']).with_rows(phases)
phases_table
px.timeline(phases_table.to_df(),
x_start = 'Start',
x_end = 'End',
y = 'Phase',
text = 'Location',
title = 'My Life Trajectory') \
.update_yaxes(autorange='reversed')
world_latest
px.choropleth(world_latest.to_df(),
locations = 'iso_alpha',
color = 'lifeExp',
hover_name = 'country',
title = 'Life Expectancy Per Country',
color_continuous_scale = px.colors.sequential.tempo
)
wm = Table.read_table('data/walmart.csv')
wm
# Number of Walmarts per state
wm_per_state = wm.group('STRSTATE')
wm_per_state
px.choropleth(wm_per_state.to_df(),
locations = 'STRSTATE',
color = 'count',
locationmode = 'USA-states',
scope = 'usa',
title = 'Number of Walmarts Per State')
penguins = Table.from_df(sns.load_dataset('penguins'))
penguins
px.scatter_3d(penguins.to_df(),
x = 'bill_length_mm',
y = 'bill_depth_mm',
z = 'flipper_length_mm',
color = 'species',
hover_name = 'island',
title = 'Flipper Length vs. Bill Depth vs. Bill Length')