简体   繁体   English

在Plotly气泡图中标记特定气泡

[英]Label specific bubbles in Plotly bubble chart

I am having trouble figuring out how to label specific bubbles in a plot.ly bubble chart. 我在弄清楚如何在plot.ly气泡图中标记特定气泡时遇到了麻烦。 I want certain "outlier" bubbles to have text written inside the bubble instead of via hover text. 我希望某些“离群”气泡将文本写在气泡内,而不是通过悬停文本。

Let's say I have this data: 假设我有以下数据:

import plotly.plotly as py
import plotly.graph_objs as go

trace0 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[10, 11, 12, 13],
    mode='markers',
    marker=dict(
        size=[40, 60, 80, 100],
    )
)

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

I'd like to only add text markers on bubbles that correspond to (1,10) and (4,13). 我只想在对应(1,10)和(4,13)的气泡上添加文本标记。 Furthermore, is it possible to control the location of text markers? 此外,是否可以控制文本标记的位置?

You can achieve this with annotations . 您可以通过批注实现此目的。

This allows you to write any text you want on the chart and reference it to your data. 这样,您就可以在图表上写下所需的任何文本,并将其引用到数据中。 You can also control where the text appears using position anchors or by applying an additional calculation on top of the x and y data. 您还可以使用位置锚点或通过在x和y数据之上应用其他计算来控制文本的显示位置。 For example: 例如:

x_data = [1, 2, 3, 4]
y_data = [10, 11, 12, 13]
z_data = [40, 60, 80, 100]

annotations = [
    dict(
        x=x, 
        y=y,
        text='' if 4 > x > 1 else z, # Some conditional to define outliers
        showarrow=False,
        xanchor='center',  # Position of text relative to x axis (left/right/center)
        yanchor='middle',  # Position of text relative to y axis (top/bottom/middle)
    ) for x, y, z in zip(x_data, y_data, z_data)
]

trace0 = go.Scatter(
    x=x_data,
    y=y_data,
    mode='markers',
    marker=dict(
        size=z_data,
    )
)

data = [trace0]
layout = go.Layout(annotations=annotations)

py.iplot(go.Figure(data=data, layout=layout), filename='bubblechart-size')

Edit 编辑

If using cufflinks, then the above can be adapted slightly to: 如果使用袖扣,则以上内容可以略作调整以:

bubbles_to_annotate = df[(df['avg_pos'] < 2) | (df['avg_pos'] > 3)]  # Some conditional to define outliers
annotations = [
    dict(
        x=row['avg_pos'], 
        y=row['avg_neg'],
        text=row['subreddit'],
        showarrow=False,
        xanchor='center',  # Position of text relative to x axis (left/right/center)
        yanchor='middle',  # Position of text relative to y axis (top/bottom/middle)
    ) for _, row in bubbles_to_annotate.iterrows()
]

df.iplot(kind='bubble', x='avg_pos', y='avg_neg', size='counts', 
       text='subreddit', xTitle='Average Negative Sentiment', 
       yTitle='Average Positive Sentiment', annotations=annotations,
       filename='simple-bubble-chart')

You will still need to define the annotations since you need a conditional argument. 由于需要条件参数,因此您仍然需要定义注释。 Then pass this directly to cufflinks via annotations . 然后通过annotations将其直接传递给袖扣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM