類別配色

用 Python 改善你的資料視覺化

Nick Strayer

Instructor

三個面板,分別以國家、城市與鳥類示範類別型資料

用 Python 改善你的資料視覺化

知覺上的限制

  • 類別盡量控制在 10 種以內
  • 考量色盲可讀性
sns.palplot(sns.color_palette('Set2', 11))

顏色太多而難以分辨的一組色塊

用 Python 改善你的資料視覺化
# Assign a new column to dataframe the desired combos
pollution['interesting cities'] = [x if x in ['Long Beach', 'Cincinnati'] 
                                   else 'other' for x in pollution['city'] ]

sns.scatterplot(x="NO2", y="SO2", hue = 'interesting cities', palette='Set2',
                data=pollution.query('year == 2014 & month == 12'))

散佈圖中 Long Beach 與 Cincinnati 以不同點色標示,其他城市合併為「other」顏色

用 Python 改善你的資料視覺化
colorbrewer_palettes = ['Set1',   'Set2',    'Set3',    'Accent', 
                        'Paired', 'Pastel1', 'Pastel2', 'Dark2']

for pal in colorbrewer_palettes: 
    sns.palplot(pal=sns.color_palette(pal))
    plt.title(pal, loc = 'left')

多種可用的類別配色方案

用 Python 改善你的資料視覺化

序位資料(a)

  • 類別之間有順序

  • 有固定且離散的類別數

顯示四分位數的示意圖

用 Python 改善你的資料視覺化

序位資料(b)

  • 類別之間有順序

  • 有固定且離散的類別數

顯示一週七天的示意圖

用 Python 改善你的資料視覺化

序位資料(c)

  • 類別之間有順序

  • 有固定且離散的類別數

用表情符號由開心到難過的量表示意圖

用 Python 改善你的資料視覺化
colorbrewer_palettes = ['Reds', 'Blues', 'YlOrBr', 'PuBuGn', 'GnBu', 'Greys']

for i, pal in enumerate(colorbrewer_palettes): 
    sns.palplot(pal=sns.color_palette(pal, n_colors=i+4))

多種可用的序位配色方案

用 Python 改善你的資料視覺化
# Make a tertials column using qcut()
pollution['NO2 Tertial'] = pd.qcut(pollution['NO2'], 3, labels = False)

# Plot colored by the computer tertials 
sns.scatterplot(x="CO", y="SO2", hue='NO2 Tertial', palette="OrRd",
                data=pollution.query("city == 'Long Beach' & year == 2014"))

以紅色序位色階編碼 NO2 三分位,顯示 CO 與 SO2 的散佈圖

用 Python 改善你的資料視覺化

來為類別上色吧

用 Python 改善你的資料視覺化

Preparing Video For Download...