首页 > 解决方案 > 如何使用 plotly 在气泡图上添加文本

问题描述

我正在使用 python 绘制气泡图。我想可视化词频。所以我做了两列有 5 个气泡,我想在每个气泡内写下这个词。我的代码是:

trace0 = go.Scatter(
x=[1, 1, 1, 1, 1],
y=[1, 2, 3, 4, 5],
mode='markers',
text=['plant', 'use', 'student', 'show', 'water'],  #words I want to show
textposition='top center',
marker=dict(
    size=norm,
color=['rgb(255, 144, 14)','rgb(255, 144, 14)','rgb(255, 144, 14)', 'rgb(255, 144, 14)',
           'rgb(255, 144, 14)'],

)

)

trace1 = go.Scatter(
x=[2, 2, 2, 2, 2],
y=[1, 2, 3, 4, 5],
mode='markers',
text=['use', 'water', 'diagram', 'show', 'plant'],   #words I want to show
textposition='top center',
marker=dict(
    size=norm,
color=['rgb(93, 164, 214)','rgb(93, 164, 214)','rgb(93, 164, 214)','rgb(93, 164, 214)','rgb(93, 164, 214)'],

)
)

data = [trace0,trace1]
py.iplot(data, filename='bubblechart-size', layout=layout)

问题是这里没有显示任何单词。如何更改代码以可视化它们?

谢谢,

标签: pythontextplotlybubble-chart

解决方案


我从这个网站上的例子Text and Annotations in Plotly

我相信您的“模式”参数不准确。

trace2 = go.Scatter(
                   x=[0, 1, 2],
                   y=[2, 2, 2],
                   mode='markers+text',
                   name='Markers and Text',
                   text=['Text D', 'Text E', 'Text F'],
                   textposition='bottom center'
                   )

所以你应该在模式参数中写:mode='markers+text'


推荐阅读