可視化神器Plotly繪製矩形樹狀圖

大家好,我是Peter~

今天給大家帶來的是一篇關於Plotly繪圖的文章:如何使用Plotly來繪製矩形樹狀圖

Plotly文章

目前Plotly的文章更新到第17篇,推薦幾篇文章:

閒聊

爲什麼Peter一直堅持寫Plotly的文章?國慶節前有位讀者加了Peter的VX:

1、你的教程關於Plotly的對我幫助很大🦀

2、本科大三就開始捲了😭

3、山大學子,優秀👍

以前還有另一位Plotly的讀者,也是看了Peter的文章:

所以大家一起好好學習,Peter也好好寫文章,說不定哪天你看了就會受益~

什麼是樹圖

樹狀圖(tree diagram)是一種將層次結構式的構造性質,以圖象方式表現出來的方法。主要是通過父子級的關係來表現的,比如:中國—廣東—深圳,就是一個例子。中國和廣東之間,廣東和深圳之間都是這種關係的表現。

下面是網上找到的一份關於樹圖的層級結構的圖形,很經典:

我們再看一幅現代的很有衝擊力的樹圖:

這種圖形叫緩衝墊樹狀結構圖(Cushion Treemap),它使用紋理使每個矩形在中間看起來像墊子一樣”凸起”,並且逐漸變細到邊緣。這種視覺效果利用了人類將這種類型的陰影解釋爲凸起的表面的優勢,從而可以更快地識別出不同的矩形

參考資源:

1、Plotly官網:https://plotly.com/python/treemaps/

2、矩形式樹狀結構圖(Treemaps)-複雜層次結構的數據可視化:https://www.idesigntools.com/treemaps.html

導入庫

本文中介紹的樹圖還是會使用 plotly_express 和 plotly.graph_objects 兩種方法來繪製,下面還是先導入庫:

import pandas as pd
import numpy as np

import plotly_express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots  # 畫子圖

基於plotly_express繪製

2.1 基礎樹狀圖

在繪製樹圖的時候是基於數據的列表形式

name = ["中國","福建", "廣東","廈門","深圳", "珠海", "湖北", "湖南", "長沙", "陝西","衡陽","咸陽","東莞"]
parent = ["", "中國", "中國","福建", "廣東", "廣東", "中國", "中國", "湖南", "中國","湖南","陝西","廣東"]


fig = px.treemap(
    names = name,
    parents = parent)
fig.update_traces(root_color="lightgrey")

fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))

fig.show()

2.2 基於DataFrame的樹圖

上面的數據是我們自定義的列表形式,一般如果在pandas中,數據會是DataFrame的格式,如何繪製樹圖呢?

在這裏我們使用的plotly中自帶的消費數據集:

fig = px.treemap(
    df,  # 傳入數據
    path=[px.Constant("all"),"day","sex","time"],  # 重點:傳遞數據路徑
    values="tip"  # 數值顯示使用哪個字段
)

fig.update_traces(root_color="lightskyblue") 

fig.update_layout(margin=dict(t=30,l=20,r=25,b=30))   

fig.show()

還可以設置顏色參數:

fig = px.treemap(
    df,
    path=[px.Constant("all"),"day","sex","time"],  # 重點:傳遞數據路徑
    values="tip",
    color="time"   # 指定顏色變化的參數
)

fig.update_traces(root_color="lightskyblue")

fig.update_layout(margin=dict(t=30,l=20,r=25,b=30))   

fig.show()

2.3 帶有連續顏色變化的樹圖

在這裏採用的是gdp數據集:

fig = px.treemap(
    df1,
    path=[px.Constant("world"),"continent","country"], # 路徑
    values="pop",  # 值
    color="lifeExp",  # 顏色的取值
    hover_data=["iso_alpha"],  # 懸停數據
    color_continuous_scale="RdBu",  # 顏色變化的設置
    color_continuous_midpoint=np.average(df1["lifeExp"],
                                        weights=df1["pop"])
)

fig.update_layout(margin = dict(t=40, l=15, r=35, b=45))

fig.show()

2.4 基於離散顏色變化的樹狀圖

採用的還是基於消費的數據集:

繪圖代碼如下:

fig = px.treemap(
    df,   # 傳入數據
    path=[px.Constant("all"), 'sex', 'day', 'time'],   # 數據路徑
    values='total_bill',   # 採用的值
    color='time',   # 顏色
    color_discrete_map={'(?)':'lightgrey',   # 離散型顏色設置
                        'Lunch':'gold', 
                        'Dinner':'darkblue'})

fig.update_layout(margin = dict(t=50, l=15, r=25, b=35))

fig.show()

3 基於go.Treemap繪製

3.1 基礎樹狀圖

 name = ["中國","福建", "廣東","廈門","深圳", "珠海", "湖北", "湖南", "長沙", "陝西","衡陽","咸陽","東莞"]
parent = ["", "中國", "中國","福建", "廣東", "廣東", "中國", "中國", "湖南", "中國","湖南","陝西","廣東"]


fig = go.Figure(go.Treemap(  # go方法實現
    labels = name,
    parents = parent,
    root_color = "lightgrey"
))

fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))

fig.show()

3.2 不同顏色的樹圖

多種方式來設置樹狀圖的顏色

1、方式1

name = ["中國","福建", "廣東","廈門","深圳", "珠海", "湖北", "湖南", "長沙", "陝西","衡陽","咸陽","東莞"]
parent = ["", "中國", "中國","福建", "廣東", "廣東", "中國", "中國", "湖南", "中國","湖南","陝西","廣東"]
color = ["pink", "royalblue", "lightgray", "purple", "cyan", "lightgray", "lightblue", "lightgreen"]


fig = go.Figure(go.Treemap(  
    labels = name,
    parents = parent,
    marker_colors = color   # 方式1:marker_colors參數設置
))

fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))

fig.show()

方式2:

name = ["中國","福建", "廣東","廈門","深圳", "珠海", "湖北", "湖南", "長沙", "陝西","衡陽","咸陽","東莞"]
parent = ["", "中國", "中國","福建", "廣東", "廣東", "中國", "中國", "湖南", "中國","湖南","陝西","廣東"]

fig = go.Figure(go.Treemap(  
    labels = name,
    parents = parent,
))

fig.update_layout(
    margin = dict(t=50, l=25, r=25, b=25),
    # 方式2:通過 treemapcolorway 參數設置
    treemapcolorway = ["pink","blue","red","lightblue","purple","royalblue"])

fig.show()

方式3:

name = ["中國","福建", "廣東","廈門","深圳", "珠海", "湖北", "湖南", "長沙", "陝西","衡陽","咸陽","東莞"]
parent = ["", "中國", "中國","福建", "廣東", "廣東", "中國", "中國", "湖南", "中國","湖南","陝西","廣東"]
values = [0,10,20,30,44,55,60,70,88,96,127,150,180]


fig = go.Figure(go.Treemap(  
    labels = name,
    parents = parent,
    values = values,
    marker_colorscale = 'Blues'  # 方式3
))

fig.update_layout(
    margin = dict(t=20, l=25, r=25, b=25))

fig.show()

如果我們想控制所有的標籤內容的大小是相同的,我們可以使用來uniformtext參數來進行控制。

在這裏我們採用的是一份在線的CSV文件:

fig = go.Figure(go.Treemap(
    ids = df2.ids, 
    labels = df2.labels,  # 標籤
    parents = df2.parents,  # 父級路徑
    pathbar_textfont_size = 20,  # 路徑的字體大小
    root_color = "lightblue"  # root下的顏色
))

fig.update_layout(uniformtext=dict(minsize=10,mode="hide"),
                  margin=dict(t=50,l=25,r=25,b=25))

fig.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章