Fork me on GitHub

可视化神器plotly玩转树状图

可视化神器Plotly绘制矩形树图

今天分享的是一篇关于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 两种方法来绘制,下面还是先导入库:

1
2
3
4
5
6
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 基础树状图

在绘制树图的时候是基于数据的列表形式

1
2
3
4
5
6
7
8
9
10
11
12
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中自带的消费数据集:

1
2
3
4
5
6
7
8
9
10
11
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()

还可以设置颜色参数:

1
2
3
4
5
6
7
8
9
10
11
12
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数据集:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 基于离散颜色变化的树状图

采用的还是基于消费的数据集:

绘图代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
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 基础树状图

1
2
3
4
5
6
7
8
9
10
11
12
13
 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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文件:

1
2
3
4
5
6
7
8
9
10
11
12
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()

本文标题:可视化神器plotly玩转树状图

发布时间:2021年10月13日 - 21:10

原始链接:http://www.renpeter.cn/2021/10/13/%E5%8F%AF%E8%A7%86%E5%8C%96%E7%A5%9E%E5%99%A8plotly%E7%8E%A9%E8%BD%AC%E6%A0%91%E7%8A%B6%E5%9B%BE.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea