python - Plotly:如何绘制具有跨不同列的匹配行的 Sankey 图?

标签 python pandas plotly sankey-diagram plotly-python

我正在通过 plotly 绘制桑基图来比较不同的观察分类。但是,我对两个以上的分类有一些问题,其中每个分类中的观察顺序在每个节点的输入和输出之间发生变化。

我使用的代码如下:

def pl_sankey(df, label_color, categories, value, title='Sankey Diagram', fname=None, width=3000, height=1600, scale=2):
    from IPython.display import Image
    import plotly.graph_objects as go
    import pandas as pd
    df = df.copy()
    labels = []
    colors = []
    # associate labels to colors
    for k, v in label_color.items():
        labels += [k]
        colors += [v]
    # transform df into a source-target pair
    st_df = None
    for i in range(len(categories)-1):
        _st_df = df[[categories[i],categories[i+1],value]]
        _st_df.columns = ['source', 'target', 'count']
        st_df = pd.concat([st_df, _st_df])
        st_df = st_df.groupby(['source', 'target']).agg({'count': 'sum'}).reset_index()
    # add index for source-target pair
    st_df['sourceID'] = st_df['source'].apply(lambda x: labels.index(str(x)))
    st_df['targetID'] = st_df['target'].apply(lambda x: labels.index(str(x)))
    # creating the sankey diagram
    data = dict(
        type='sankey', node=dict(
            pad=15, thickness=20, line = dict(color='black', width=0.5), label=labels, color=colors,
        ),
        link=dict(source=st_df['sourceID'], target=st_df['targetID'], value=st_df['count']),
    )
    layout = dict(title=title, font=dict(size=16, family='Arial'))  
    # creating figure
    fig = go.Figure(dict(data=[data], layout=layout))
    if fname:
        fig.write_image(f'{fname}.pdf', format='pdf', width=width, height=height, scale=scale)
    return Image(fig.to_image(format='png', width=width, height=height, scale=scale))

输入参数为:

  • 一个 pandas DataFrame df,每组行都有分组,例如:
# g1_l1 means group1, label1

       g1      g2      g3   counts
0   g1_l1   g2_l1   g3_l1   10
1   g1_l3   g2_l2   g3_l1   1
2   g1_l1   g2_l2   g3_l2   1
3   g1_l2   g2_l2   g3_l1   40
4   g1_l2   g2_l3   g3_l2   20
5   g1_l3   g2_l1   g3_l2   10
  • label_color 是一个字典,其中键是标签,值是颜色
  • categories 是分组的列名,在本例中为 ['grouping1', 'grouping2', 'grouping3']
  • values 是计数的列名,在本例中为 'counts'

一个执行示例如下:

df = pd.DataFrame([
    ['g1_l1', 'g2_l1', 'g3_l1', 10],
    ['g1_l3', 'g2_l2', 'g3_l1', 1],
    ['g1_l1', 'g2_l2', 'g3_l2', 1],
    ['g1_l2', 'g2_l2', 'g3_l1', 40],
    ['g1_l2', 'g2_l3', 'g3_l2', 20],
    ['g1_l3', 'g2_l1', 'g3_l2', 10],
], columns=['g1', 'g2', 'g3', 'counts'])

label_color = {
    'g1_l1': '#1f77b4', 'g1_l2': '#ff7f0e', 'g1_l3': '#279e68',
    'g2_l1': '#1f77b4', 'g2_l2': '#ff7f0e', 'g2_l3': '#279e68',
    'g3_l1': '#1f77b4', 'g3_l2': '#ff7f0e',
}

pl_sankey(df, label_color, categories=df.columns[:-1], value='counts', title='', fname=None)

sankey example

但是,此代码保证仅在两个相邻列之间进行行匹配。例如,考虑第 1 行:

       g1      g2      g3   counts
1   g1_l3   g2_l2   g3_l1   1

这样的行应该从第一列的绿色簇 (g1_l3) 开始,落在第二列的橙色簇 (g2_l2) 并继续到蓝色簇 ( g3_l1) 第三列。然而,这在之前的图中并没有得到尊重,其中第二列的输入与匹配输出的排序不同。

附上注释图以显示第二列观察的跳跃(这种观察在输入中倒数第二,但在第二列输出中倒数第二):

observation jumps

我想沿着从第一列到最后一列的行路径。这可能吗?如何使用 Sankey 图实现?

最佳答案

我可能完全误解了这里的某些内容,但我希望能以正确的方式指导您。因此,如果我错了,请原谅我,但您似乎误解了 plotly sankey 图的一些内部工作原理。别担心,你是not alone .

你是说:

Such row should start from green cluster (g1_l3) on first column, land in orange cluster (g2_l2) in second column and continue to blue cluster (g3_l1) on third column

因此,如果我没理解错的话,您希望这种特殊关系被说明为:

enter image description here

但这并不是一个 plotly sankey 图的工作方式。相反,从 g1_l3g2_l2 的数量与进入 g2_l2 的其他数量组合在一起,然后作为聚合值“发送”到 g3_l1。你有这条线的原因:

enter image description here

... 是因为您还有关系 g2_l2 , g3_l1, 1:

enter image description here

如果您以某种方式成功地说明了数据框中的关系,完全您在桑基图中的描述方式,它就不再是桑基图了。

很抱歉,这就是我目前能为您做的所有事情。

关于python - Plotly:如何绘制具有跨不同列的匹配行的 Sankey 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62902499/

相关文章:

R图表转换为html格式,无需其他文件

r - plotly 线性趋势线不更新 Shiny

python - mypy - 项目 "None"的 "Optional[CustomAttrsModel]"没有属性 "country"

python - 填充多个组的缺失日期和值 - Pandas

python - Pandas:将分组中的唯一项放入单独的行而不是数组中

python - pandas.DataFrame 可以有列表类型的列吗?

python - 将 Queryset 从装饰器传递给函数

python - 当轮廓部分超出可见区域时删除重复的 matplotlib 轮廓标签

python - 如何将增量添加到 python datetime.time?

r - 使用 R 中的plot_ly 单独更改图例