2017-09-13 45 views
8

我跟着這裏的指導: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) 

先前塊後的碼構建弦圖如下:

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') 

這是唯一的hoverinfo這表明,外面的標籤,而不是稍微更多的裏面:

enter image description here

使用我的問題開始時的鏈接示例。他們有兩套標籤。就我的例子而言,相當於'伊莎貝爾評論了索菲亞的32個......'並沒有顯示出來。

enter image description here

+0

從提供的鏈接:「ribbon_info是設置當鼠標懸停在功能區結束時顯示的信息的dicts列表。「僅僅允許hoverinfo用於標記,而不用於連接線 –

+0

因此,當ribbon_info追加textf和texti時,它在功能區結束時顯示標籤,即'Economics'。在我的問題開始時(https://plot.ly/python/filled-chord-diagram/),他們的圖表在帶子上有標籤並且在帶子上有標籤?:/ – ScoutEU

+0

當光標處於光標所在位置時,我的瀏覽器顯示hoverinfo在圈子的外部,但不在裏面,另外還有一些hoverinfo接近外部段,但是再次沒有內部的東西。在你的瀏覽器中刪除'class =「shapelayer」'來擺脫這些功能區,並看到底層標記它會觸發hoverinfo。 –

回答

1

和絃圖生成代碼由兩年前的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

+0

這真的很有幫助。非常感謝你回覆我的文章!特別是考慮到其年齡 – ScoutEU

1

我找到了解決方案。事實證明,ribbon_info不喜歡脫機運行,只要我在網上做了它顯示。即它不會在plotly.offline中運行。你必須在網上做

編輯:不知道爲什麼downvoted,很多人使用陰謀離線。希望這可以幫助!