python - Plotly python 气泡图 - 添加文本

标签 python pandas plotly

我需要使用 python 将文本添加到绘图气泡图中。我只能让文本属性为每个数据点取 1 个值。但是,对于每个数据点,我需要在工具提示上显示两个值:其大小和另一个值。我正在使用一个列表列表,大致基于示例 here

这是我的代码:

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

最佳答案

实际上,您可以为工具提示的文本连接一个新的字符串列,如下所示:

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

关于python - Plotly python 气泡图 - 添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43837393/

相关文章:

python - 在模板文件中运行函数(Python/Bottle)

python - 在 python 中创建短而窄的表,并在列中列出列表

hover - Plotly,更改悬停文本

python - 按行中的值选择 Pandas 数据框中的列

python - 使用 python/pandas 组合值

r - 自定义绘图热图的悬停文本

r - 可以在 plotly 中使用文本标签和悬停文本吗?

python - 基于偏移月份序列(任意日期范围)进行合并、重新索引或以其他方式分箱

python - 检查 Sympy Expression 是否为 Nan?

python - Pandas :按列分组,将列表行合并为组的单个列?