在Plotly气泡图中标记特定气泡(Label specific bubbles in Plotly bubble chart)

编程入门 行业动态 更新时间:2024-10-28 14:32:51
在Plotly气泡图中标记特定气泡(Label specific bubbles in Plotly bubble chart)

我无法弄清楚如何在plot.ly气泡图中标记特定的气泡。 我希望某些“异常”气泡能够在气泡中写入文本,而不是通过悬停文本。

假设我有这些数据:

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

我想只在与(1,10)和(4,13)对应的气泡上添加文字标记。 此外,是否可以控制文本标记的位置?

I am having trouble figuring out how to label specific bubbles in a plot.ly bubble chart. 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). Furthermore, is it possible to control the location of text markers?

最满意答案

您可以使用注释来实现此目的。

这允许您在图表上写下您想要的任何文本并将其引用到您的数据中。 您还可以使用位置锚点或通过在x和y数据之上应用其他计算来控制文本的显示位置。 例如:

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

编辑

如果使用袖扣,那么上面的内容可以略微适应:

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

由于需要条件参数,因此仍需要定义注释。 然后通过annotations将其直接传递给袖扣。

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. 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.

更多推荐

本文发布于:2023-07-31 12:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1343786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:气泡   图中   标记   Plotly   Label

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!