About the Data: Data Source
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import pandas as pd
import numpy as np
import plotly.express as px
from plotly.figure_factory import create_table # 修正拼字
gapminder = px.data.gapminder()
# 建立前10筆的表格
table = create_table(gapminder.head(10))
# 顯示表格
import plotly.offline as py
py.init_notebook_mode(connected=True)
py.iplot(table)
Note: If you are starting the notebook from this task, you can run cells from all the previous tasks in the kernel by going to the top menu and Kernel > Restart and Run All
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x = 'year', y='pop', height = 400)
fig.show()
fig = px.bar(data_canada, x = 'year', y ='pop',
hover_data = ['lifeExp', 'gdpPercap'], color = 'lifeExp',
labels = {'pop': 'population of Canada'}, height = 400)
fig.show()
Note: If you are starting the notebook from this task, you can run cells from all the previous tasks in the kernel by going to the top menu and Kernel > Restart and Run All
gapminder2007 = gapminder.query("year == 2007")
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp" )
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp", color = "continent")
Note: If you are starting the notebook from this task, you can run cells from all the previous tasks in the kernel by going to the top menu and Kernel > Restart and Run All
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp", color = "continent",
size = "pop", size_max =60)
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp", color = "continent",
size = "pop", size_max =60, hover_name = "country")
Note: If you are starting the notebook from this task, you can run cells from all the previous tasks in the kernel by going to the top menu and Kernel > Restart and Run All
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp", color = "continent",
size = "pop", size_max =60, hover_name = "country", facet_col = "continent", log_x = True)
px.scatter(gapminder, x = "gdpPercap", y = "lifeExp", color = "continent",
size = "pop", size_max =60, hover_name = "country",animation_frame = 'year',
animation_group = "country", log_x = True, range_x = [100,100000], range_y = [25,90],
labels = dict(pop = "Population", gdpPercap = "GDP per Capita", lifeExp = "Life Expenctancy"))
import plotly.express as px
# 建立 highlight 欄位,標示出 Taiwan
gapminder["highlight"] = gapminder["country"].apply(
lambda c: "Taiwan" if c == "Taiwan" else "Other Asia"
)
# 過濾只留下亞洲國家
asia_df = gapminder[gapminder["continent"] == "Asia"]
# 繪製散點動畫圖
fig = px.scatter(
asia_df,
x="gdpPercap",
y="lifeExp",
color="highlight", # 用 highlight 區分 Taiwan 與其他國家
size="pop",
size_max=60,
hover_name="country",
animation_frame="year",
animation_group="country",
log_x=True,
range_x=[100, 100000],
range_y=[25, 90],
labels=dict(
pop="Population",
gdpPercap="GDP per Capita",
lifeExp="Life Expectancy"
),
title="Taiwan vs Other Asian Countries: GDP vs Life Expectancy"
)
fig.show()
Note: If you are starting the notebook from this task, you can run cells from all the previous tasks in the kernel by going to the top menu and Kernel > Restart and Run All
px.choropleth(gapminder, locations = "iso_alpha", color = "lifeExp", hover_name = "country",
animation_frame = 'year', color_continuous_scale = px.colors.sequential.Plasma,
projection = "natural earth")
px.choropleth(gapminder, locations = "iso_alpha", color = "lifeExp", hover_name = "country",
animation_frame = 'year', color_continuous_scale = px.colors.sequential.Plasma,
projection = "orthographic")
whos
Variable Type Data/Info -------------------------------------- asia_df DataFrame country contine<...>n\n[396 rows x 9 columns] create_table function <function create_table at 0x73485377e290> data_canada DataFrame country continent ye<...>.23501 CAN 124 fig Figure Figure({\n 'data': [{'<...> 'Life Expectancy'}}}\n}) gapminder DataFrame country contine<...>\n[1704 rows x 9 columns] gapminder2007 DataFrame country <...>n\n[142 rows x 8 columns] go module <module 'plotly.graph_obj<...>/graph_objs/__init__.py'> np module <module 'numpy' from '/op<...>kages/numpy/__init__.py'> pd module <module 'pandas' from '/o<...>ages/pandas/__init__.py'> px module <module 'plotly.express' <...>tly/express/__init__.py'> py module <module 'plotly.offline' <...>tly/offline/__init__.py'> table Figure Figure({\n 'data': [{'<...> 'zeroline': False}}\n})
fig = px.line_geo(gapminder.query("year==2007"), locations="iso_alpha", color="continent", projection="orthographic")
fig.show()
Note: If you are starting the notebook from this task, you can run cells from all the previous tasks in the kernel by going to the top menu and Kernel > Restart and Run All
fig = px.line(gapminder, x="year", y="lifeExp", color="continent", line_group="country", hover_name="country",
line_shape="spline", render_mode="svg")
fig.show()
fig = px.area(gapminder, x="year", y="pop", color="continent", line_group="country")
fig.show()