Explore the Gapminder Dataset with Plotly Express¶

About the Data: Data Source

Task 1: Loading the Data¶

In [1]:
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
In [2]:
import plotly.express as px
from plotly.figure_factory import create_table   # 修正拼字

gapminder = px.data.gapminder()
In [3]:
# 建立前10筆的表格
table = create_table(gapminder.head(10))

# 顯示表格
import plotly.offline as py
py.init_notebook_mode(connected=True)
py.iplot(table)

Task 2: Quick Visualizations with Custom Bar Charts¶


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


In [4]:
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x = 'year', y='pop', height = 400)
fig.show()
In [5]:
fig = px.bar(data_canada, x = 'year', y ='pop',
            hover_data = ['lifeExp', 'gdpPercap'], color = 'lifeExp',
            labels = {'pop': 'population of Canada'}, height = 400)
fig.show()
In [ ]:
 

Task 3: Plot Life Expectancy vs GDP per Capita¶


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


In [6]:
gapminder2007 = gapminder.query("year == 2007")

px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp" )
In [7]:
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp", color = "continent")
In [ ]:
 

Task 4: Customize Interactive Bubble Charts¶


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


In [8]:
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp", color = "continent",
          size = "pop", size_max =60)
In [9]:
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp", color = "continent",
          size = "pop", size_max =60, hover_name = "country")
In [ ]:
 

Task 5: Create Interactive Animations and Facet Plots¶


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


In [10]:
px.scatter(gapminder2007, x = "gdpPercap", y = "lifeExp", color = "continent",
          size = "pop", size_max =60, hover_name = "country", facet_col = "continent", log_x = True)
In [11]:
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"))
In [12]:
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()

Task 6: Represent Geographic Data as Animated Maps¶


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


In [13]:
px.choropleth(gapminder, locations = "iso_alpha", color = "lifeExp", hover_name = "country",
             animation_frame = 'year', color_continuous_scale = px.colors.sequential.Plasma, 
             projection = "natural earth")
In [14]:
px.choropleth(gapminder, locations = "iso_alpha", color = "lifeExp", hover_name = "country",
             animation_frame = 'year', color_continuous_scale = px.colors.sequential.Plasma, 
             projection = "orthographic")
In [15]:
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})
In [16]:
fig = px.line_geo(gapminder.query("year==2007"), locations="iso_alpha", color="continent", projection="orthographic")
fig.show()

Task 7: Interactive Line Plots and Area Plots¶


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


In [17]:
fig = px.line(gapminder, x="year", y="lifeExp", color="continent", line_group="country", hover_name="country",
        line_shape="spline", render_mode="svg")
fig.show()
In [18]:
fig = px.area(gapminder, x="year", y="pop", color="continent", line_group="country")
fig.show()
In [ ]: