Python 和弦图 (Plotly) - 交互式工具提示

标签 python plotly

我按照这里的指南:

https://plot.ly/python/filled-chord-diagram/

我制作了这个:

enter image description here

在指南中,我按照 ribbon_info 代码将 hoverinfo 添加到连接的功能区,但没有任何显示。我可以让 hoverinfo 只显示功能区末端。谁能看出我哪里出错了?

ribbon_info=[]
for k in range(L):

    sigma=idx_sort[k]
    sigma_inv=invPerm(sigma)
    for j in range(k, L):
        if matrix[k][j]==0 and matrix[j][k]==0: continue
        eta=idx_sort[j]
        eta_inv=invPerm(eta)
        l=ribbon_ends[k][sigma_inv[j]]  

        if j==k:
            layout['shapes'].append(make_self_rel(l, 'rgb(175,175,175)' ,
                                    ideo_colors[k], radius=radii_sribb[k]))
            z=0.9*np.exp(1j*(l[0]+l[1])/2)
            #the text below will be displayed when hovering the mouse over the ribbon
            text=labels[k]+' appears on'+ '{:d}'.format(matrix[k][k])+' of the same grants as  '+ '',
            ribbon_info.append(Scatter(x=z.real,
                                       y=z.imag,
                                       mode='markers',
                                       marker=Marker(size=5, color=ideo_colors[k]),
                                       text=text,
                                       hoverinfo='text'
                                       )
                              )
        else:
            r=ribbon_ends[j][eta_inv[k]]
            zi=0.9*np.exp(1j*(l[0]+l[1])/2)
            zf=0.9*np.exp(1j*(r[0]+r[1])/2)
            #texti and textf are the strings that will be displayed when hovering the mouse 
            #over the two ribbon ends
            texti=labels[k]+' appears on '+ '{:d}'.format(matrix[k][j])+' of the same grants as '+\
                  labels[j]+ '',

            textf=labels[j]+' appears on '+ '{:d}'.format(matrix[j][k])+' of the same grants as '+\
                  labels[k]+ '',
            ribbon_info.append(Scatter(x=zi.real,
                                       y=zi.imag,
                                       mode='markers',
                                       marker=Marker(size=0.5, color=ribbon_color[k][j]),
                                       text=texti,
                                       hoverinfo='text'
                                       )
                              ),
            ribbon_info.append(Scatter(x=zf.real,
                                       y=zf.imag,
                                       mode='markers',
                                       marker=Marker(size=0.5, color=ribbon_color[k][j]),
                                       text=textf,
                                       hoverinfo='text'
                                       )
                              )
            r=(r[1], r[0])#IMPORTANT!!!  Reverse these arc ends because otherwise you get
                          # a twisted ribbon
            #append the ribbon shape
            layout['shapes'].append(make_ribbon(l, r , 'rgb(255,175,175)', ribbon_color[k][j]))

变量的输出如下:

texti = (u'Sociology appears on 79 of the same grants as Tools, technologies & methods',)

textf = (u'Tools, technologies & methods appears on 79 of the same grants as Sociology',)

ribbon_info = [{'hoverinfo': 'text',
  'marker': {'color': 'rgba(214, 248, 149, 0.65)', 'size': 0.5},
  'mode': 'markers',
  'text': (u'Demography appears on 51 of the same grants as Social policy',),
  'type': 'scatter',
  'x': 0.89904409911342476,
  'y': 0.04146936036799545},
 {'hoverinfo': 'text',
  'marker': {'color': 'rgba(214, 248, 149, 0.65)', 'size': 0.5},
  'mode': 'markers',
  'text': (u'Social policy appears on 51 of the same grants as Demography',),
  'type': 'scatter',
  'x': -0.65713108202353809,
  'y': -0.61496238993825791},..................**etc**

sigma = array([ 0, 14, 12, 10,  9,  7,  8,  5,  4,  3,  2,  1,  6, 16, 13, 11, 15], dtype=int64)

构建和弦图的前一个 block 之后的代码如下:

ideograms=[]
for k in range(len(ideo_ends)):
    z= make_ideogram_arc(1.1, ideo_ends[k])
    zi=make_ideogram_arc(1.0, ideo_ends[k])
    m=len(z)
    n=len(zi)
    ideograms.append(Scatter(x=z.real,
                             y=z.imag,
                             mode='lines',
                             line=Line(color=ideo_colors[k], shape='spline', width=0),
                             text=labels[k]+'<br>'+'{:d}'.format(row_sum[k]), 
                             hoverinfo='text'
                             )
                     )


    path='M '
    for s in range(m):
        path+=str(z.real[s])+', '+str(z.imag[s])+' L '

    Zi=np.array(zi.tolist()[::-1]) 

    for s in range(m):
        path+=str(Zi.real[s])+', '+str(Zi.imag[s])+' L '
    path+=str(z.real[0])+' ,'+str(z.imag[0]) 

    layout['shapes'].append(make_ideo_shape(path,'rgb(150,150,150)' , ideo_colors[k]))

data = Data(ideograms+ribbon_info)
fig=Figure(data=data, layout=layout) 

plotly.offline.iplot(fig, filename='chord-diagram-Fb') 

这是唯一显示的悬停信息,外部标签,而不是内部标签:

enter image description here

使用问题开头链接中的示例。他们有两组标签。在我的例子中,“Isabelle 评论了 32 of Sophia....”的等价内容没有显示。

enter image description here

最佳答案

生成和弦图的代码是两年前Plotly发的。同时,对 Plotly 形状的定义进行了一些更改。要使工具提示在离线模式下工作,您应该:

1) 插入行

layer='below'

在函数 make_ideo_shapemake_ribbonmake_self_rel 返回的字典中;

2) 在包含列表 ribbon_info 定义的单元格中,更改包含 ribbon_info.append、x 和 y 赋值的三行中的每一行, 到包含这些值的列表:

x=[z.real],
y=[z.imag],

x=[zi.real],
y=[zi.imag],

分别,

x=[zf.real],
y=[zf.imag],

对应笔记本https://plot.ly/python/filled-chord-diagram/现在更新了。 感谢 Python StackOverflow @PythonStack 指出这个错误,在这里:https://twitter.com/PythonStack/status/914924595953721344

关于Python 和弦图 (Plotly) - 交互式工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46191793/

相关文章:

python - 计算python中多维数组中数组的出现次数

python - Plotly:如何制作 3D 堆叠直方图?

python - ESP8266 Micropython - 连接到大学 Wi-fi (WPA2 Enterprise PEAP)

python - 如何创建每个点使用一种颜色的散点图(绘图)

r - 在 R 中直接绘制数学函数

javascript - Plotly.js 添加标记将填充添加到 x 轴

python - 更改 Plotly 饼图中值的格式(在 Python 中)

python - python多久刷新一次文件?

python - 如何使用与 get_height() 中的值不同的值注释条形图

python - 如何为输出文本文件添加时间戳