使用Holoviews和Bokeh绘制桑基图

huangapple go评论109阅读模式
英文:

Draw Sankey diagram with holoviews and Bokeh

问题

如何使用这些数据绘制以下桑基图:

目标 数值 类别链接
22 SPHYRNIDAE CE 3 极度濒危
27 TRIAKIDAE CE 5 极度濒危
67 DASYATIDAE CE 2 极度濒危
14 CARCHARHINIDAE CE 4 极度濒危
62 UROTRYGONIDAE CE 2 极度濒危
英文:

How can I draw the following Sankey diagram with this data

Source Target Value Category_links
22 SPHYRNIDAE CE 3 Critically Endangered
27 TRIAKIDAE CE 5 Critically Endangered
67 DASYATIDAE CE 2 Critically Endangered
14 CARCHARHINIDAE CE 4 Critically Endangered
62 UROTRYGONIDAE CE 2 Critically Endangered

使用Holoviews和Bokeh绘制桑基图

答案1

得分: 1

最简单的方法是使用 pandas DataFrame。您的表格看起来非常接近所需的形式。然后使用此 DataFrame 调用 hv.Sankey()

import pandas as pd
import holoviews as hv
hv.extension('bokeh')

df = pd.DataFrame({
    'Source':['SPHYRNIDAE', 'TRIAKIDAE', 'DASYATIDAE','CARCHARHINIDAE', 'UROTRYGONIDAE'],
    'Target':['CE']*5,
    'Value': [3,5,2,4,2]
})

上面代码的输出如下图所示:

使用Holoviews和Bokeh绘制桑基图

要为边缘添加颜色,请在 DataFrame 中添加一个包含颜色信息(颜色字符串或十六进制字符串)的列,并将列名传递给 opts() 调用中的 edge_color 参数。

df = pd.DataFrame({
    'Source':['SPHYRNIDAE', 'TRIAKIDAE', 'DASYATIDAE','CARCHARHINIDAE', 'UROTRYGONIDAE'],
    'Target':['CE']*5,
    'Value': [3,5,2,4,2],
    'EdgeColor': ['blue', 'red', 'black', 'orange', 'magenta']
})

sankey = hv.Sankey(df)
sankey.opts(width=600, height=400, edge_color='EdgeColor')

带有彩色边缘的输出如下图所示:

使用Holoviews和Bokeh绘制桑基图

或者使用节点的默认颜色,并将其传递给边缘。要使用左节点的颜色,请将源的列名传递给 hv.dim().str()。要使用目标节点的颜色,请将目标的列名传递给 hv.dim().str()

sankey = hv.Sankey(df)
sankey.opts(width=600, height=400, edge_color=hv.dim('Source').str())
# sankey.opts(width=600, height=400, edge_color=hv.dim('Target').str())

节点颜色决定边缘颜色的输出如下图所示:

使用Holoviews和Bokeh绘制桑基图

评论

此代码在 bokeh 3.1.1、panel 1.0.2 和 holoviews 1.16.0 下运行。

参考链接

https://holoviews.org/reference/elements/matplotlib/Sankey.html#sankey

英文:

The easiest way is to have a pandas DataFrame. Your table looks very close to the wanted from. Then call hv.Sankey() with this DataFrame.

import pandas as pd
import holoviews as hv
hv.extension('bokeh')

df = pd.DataFrame({
    'Source':['SPHYRNIDAE', 'TRIAKIDAE', 'DASYATIDAE',' CARCHARHINIDAE', 'UROTRYGONIDAE'],
    'Target':['CE']*5,
    'Value': [3,5,2,4,2]
})
>>>         Source Target  Value
0       SPHYRNIDAE     CE      3
1        TRIAKIDAE     CE      5
2       DASYATIDAE     CE      2
3   CARCHARHINIDAE     CE      4
4    UROTRYGONIDAE     CE      2

sankey = hv.Sankey(df)
sankey.opts(width=600, height=400)

The Output of the code above is

使用Holoviews和Bokeh绘制桑基图

To style the edges with a color add a column with color information (color string or hex string) to the DataFrame and pass the column name to the parameter edge_color in the ops() call.

df = pd.DataFrame({
    'Source':['SPHYRNIDAE', 'TRIAKIDAE', 'DASYATIDAE',' CARCHARHINIDAE', 'UROTRYGONIDAE'],
    'Target':['CE']*5,
    'Value': [3,5,2,4,2],
    'EdgeColor': ['blue', 'red', 'black', 'orange', 'magenta']
})
sankey = hv.Sankey(df)
sankey.opts(width=600, height=400, edge_color='EdgeColor')

使用Holoviews和Bokeh绘制桑基图

Or use the default colors from the nodes and pass them to the edges. To use the color from the left node, pass the column name of the source to hv.dim().str(). To use the color of the target node, pass the column name of the traget to hv.dim().str().

sankey = hv.Sankey(df)
sankey.opts(width=600, height=400, edge_color=hv.dim('Source').str())
# sankey.opts(width=600, height=400, edge_color=hv.dim('Target').str())

使用Holoviews和Bokeh绘制桑基图

Comment

This code was run with bokeh 3.1.1, panel 1.0.2 and holoviews 1.16.0

References

https://holoviews.org/reference/elements/matplotlib/Sankey.html#sankey

huangapple
  • 本文由 发表于 2023年6月19日 17:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定