類別資料視覺化

使用 Bokeh 製作互動式資料視覺化

George Boorman

Core Curriculum Manager, DataCamp

類別資料

  • 類別資料是具有固定選項或標籤的資料。

    • 例如性別或出生國家。
  • 在統計中,factors 指的就是類別變數

print(nba[["position", "team", "conference"]].head())
    position    team    conference
0   PG          OKC     West
1   PG          HOU     West
2   PG          BOS     East
3   C           NO      West
4   SG          TOR     East
使用 Bokeh 製作互動式資料視覺化

排序

未排序長條圖

pos = nba.groupby("position")["points"].mean()
pos = pos.sort_values("points", ascending=False)

fig = figure(x_range=pos["position"], x_axis_label="Position", y_axis_label="Points per Game") fig.vbar(x=pos["position"], top=pos["points"]) output_file(filename="sorted_plot.html") show(fig)

已排序長條圖

使用 Bokeh 製作互動式資料視覺化

留白

fig = figure(x_range=nba["position"], 
             x_axis_label="Position", 
             y_axis_label="Points per Game")
fig.vbar(x=nba["position"], top=nba["points"],

width=0.9)
output_file(filename="padded_plot.html") show(fig)

留白後的長條圖

使用 Bokeh 製作互動式資料視覺化

標籤方向

fig = figure(x_range=nba['position'], 
             x_axis_label="Position", 
             y_axis_label="Points per Game")
fig.vbar(x=nba["position"], top=nba["points"], width=0.9)

fig.xaxis.major_label_orientation = 45
output_file(filename="rotated_x_label_plot.html") show(fig)
使用 Bokeh 製作互動式資料視覺化

旋轉 x 軸標籤

旋轉 x 軸標籤的長條圖

使用 Bokeh 製作互動式資料視覺化

巢狀類別

positions = ["Point Guard", "Shooting Guard", 
            "Small Forward", "Power Forward", "Center"]
conferences = ["East", "West"]

factors = [("Point Guard", "East"), ("Point Guard", "West"), ("Shooting Guard", "East"), ("Shooting Guard", "West"), ("Small Forward", "East"), ("Small Forward", "West"), ("Power Forward", "East"), ("Power Forward", "West"), ("Center", "East"), ("Center", "West")]
使用 Bokeh 製作互動式資料視覺化

建立分組長條圖

from bokeh.models import FactorRange

fig = figure(x_range=FactorRange(*factors), y_axis_label="Points per Game")
fig.vbar(x=factors, top=nba["points"], width=0.9)
output_file(filename="grouped_bar_plot.html") show(fig)
使用 Bokeh 製作互動式資料視覺化

分組長條圖

分組長條圖

使用 Bokeh 製作互動式資料視覺化

一起來練習吧!

使用 Bokeh 製作互動式資料視覺化

Preparing Video For Download...