2017-05-07 63 views
1

我需要用python將文字添加到氣泡圖。我只能得到text屬性,每個數據點取1個值。但是,對於每個數據點,我需要在工具提示中顯示兩個值:其大小和另一個值。我使用一個列表清單,鬆散地基於示例herePlotly python氣泡圖 - 添加文字

這是我的代碼:

bubbletext = dfenv[['Max_FZ','Argmax_FZ']] # dataframe with the 2 cols I need for text 
bubbletext = bubbletext.values.tolist() # puts the df into list of lists 

trace1 = Scatter(
     x=dfenv['X (ft)'], 
     y=dfenv['Y (ft)'], 
     text=bubbletext, # here's the problem 
     mode='markers', 
     marker=Marker(
      size=dfenv['Max_FZ'], 
      sizeref= dfenv['Max_FZ'].max()/1e2**2, 
      sizemode='area' 
      ) 
     ) 
data = Data([trace1]) 
layout = Layout(showlegend=False) 
fig = Figure(data=data)#, layout=layout) 
plot_url = py.plot(fig, filename='Envelope_MaxBubblechart') 

回答

1

其實,你可以連接一個新字符串列工具提示的文字如下:

dfenv['text'] = dfenv['Max_FZ'].round(1).astype(str) + 'units' + '<br>at: ' + dfenv['Argmax_FZ'] 

trace1 = Scatter(
    x=dfenv['X (ft)'], 
    y=dfenv['Y (ft)'], 
    text=dfenv['text'], #string df column here with line breaks if needed 
    mode='markers', 
    marker=Marker(
     size=dfenv['Max_FZ'], 
     sizeref= dfenv['Max_FZ'].max()/1e2**2, 
     sizemode='area' 
     ) 
    ) 
data = Data([trace1]) 
fig = Figure(data=data) 
plot_url = py.plot(fig, filename='Envelope_MaxBubblechart')