import plotly.graph_objects as go
fig = go.Figure()
x = np.array([0,1,2,3])
x_s = np.array([0,1,2,3])
y = np.array([1,1,2,3])
y_s = np.array([0,0,0,0])
fig.add_trace(go.Scatter(
  x=x_s,
  y=y_s,
  name='Test',
  marker=dict(
    size=10,
    cmax=1,
    cmin=0,
    color='black',
    colorscale="Rainbow_r",
    symbol='circle'
    # line=dict(color=line_colors, width=3)
  ),
  showlegend=True,
  hoverinfo='text',
  hoverlabel=dict(bgcolor="white", font_color="black"),
  mode='markers',
  textposition="top center"))
fig.add_annotation(
  x=0,
  y=1,
  ax=0,
  ay=0,
  xref='x',
  yref='y',
  axref='x',
  ayref='y',
  text='xxxx',
  showarrow=True,
  arrowhead=3,
  arrowsize=1,
  arrowwidth=1,
  arrowcolor='black'
)

此代码生成以下图表:

chart

有没有可能这样做,当你点击标记的图例时,箭头就会随之消失?

阅读https://plotly.com/python/text-and-annotations/https://plotly.com/python/legend/.

推荐答案

这可以通过在同一图例组中设置箭头和文本批注来实现.组名将散布、箭头和注释放在相同的组名中.

import plotly.graph_objects as go
import numpy as np

fig = go.Figure()

x = np.array([0,1,2,3])
x_s = np.array([0,1,2,3])
y = np.array([1,1,2,3])
y_s = np.array([0,0,0,0])

fig.add_trace(go.Scatter(
    x=x_s,
    y=y_s,
    name='Test',
    marker=dict(
        size=10,
        cmax=1,
        cmin=0,
        color='black',
        colorscale="Rainbow_r",
        symbol='circle'
        # line=dict(color=line_colors, width=3)
    ),
    showlegend=True,
    hoverinfo='text',
    hoverlabel=dict(bgcolor="white", font_color="black"),
    mode='markers',
    textposition="top center",
    legendgroup='001'
))

fig.add_trace(go.Scatter(x=[0,0],
                         y=[0,1],
                         #text='xxxx',
                         marker=dict(
                             size=10,
                             symbol='arrow-bar-up',
                             angleref='previous'
                         ),
                         #textposition='bottom center',
                         line_color='black',
                         legendgroup='001',
                         showlegend=False
                        ))

fig.add_trace(go.Scatter(mode='text',
                         x=[0,0],
                         y=[0,0],
                         text='xxxx',
                         textposition='bottom center',
                         legendgroup='001',
                         showlegend=False
                        ))

fig.show()

enter image description here

enter image description here

Python相关问答推荐

从多行文本中提取事件对

使用Python C API重新启动Python解释器

pandas DataFrame中类型转换混乱

按照行主要蛇扫描顺序对点列表进行排序

在应用循环中间保存pandas DataFrame

Pandas 第二小值有条件

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

删除任何仅包含字符(或不包含其他数字值的邮政编码)的观察

非常奇怪:tzLocal.get_Localzone()基于python3别名的不同输出?

在Python Attrs包中,如何在field_Transformer函数中添加字段?

Excel图表-使用openpyxl更改水平轴与Y轴相交的位置(Python)

Pandas:将多级列名改为一级

driver. find_element无法通过class_name找到元素'""

将scipy. sparse矩阵直接保存为常规txt文件

pandas:对多级列框架的列进行排序/重新排序

python panda ExcelWriter切换动态公式到数组公式

下三角形掩码与seaborn clustermap bug

如何杀死一个进程,我的Python可执行文件以sudo启动?

如何在Python请求中组合多个适配器?

我对这个简单的异步者的例子有什么错误的理解吗?