【问题标题】:Visualize extra info on plotly scatterplot when moving the cursor on marker在标记上移动光标时可视化散点图上的额外信息
【发布时间】:2022-11-12 02:49:42
【问题描述】:

给定以下玩具pd.DataFrame

    x   y  info
0  10   5    10
1  20  13    20
2  30  64    30
3  40  56    40
4  50  75    50
5  60  44    60
6  70  31    70

当用光标接近标记时,我想可视化额外的info 列。这是不是与任何额外维度或任何应该绘制的内容相关,或者两者都不必直接出现在情节中。这只是我想可视化的额外信息。现在我刚刚使用了代码

px.scatter(dataframe, 
            x='x,
            y='y'
         )

产生结果:

我只想将'info' 显示在'x''y' 下。

【问题讨论】:

    标签: plotly plotly-dash plotly-python


    【解决方案1】:

    我可以用下面的代码做到这一点:

    ## Import Libraries
    import plotly.graph_objects as go
    import pandas as pd
        
    ## Create the Dataframe
    df = pd.DataFrame({'x': [10, 20, 30, 40, 50, 60, 70],
                       'y': [5, 13,  64, 56, 75, 44, 31],
                       'info': [10, 20, 30, 40, 50, 60, 70]})
    
    ## The Plot 
    layout = dict(plot_bgcolor='white',
                  margin=dict(t=20, l=20, r=20, b=20),
                  xaxis=dict(title='X',
                             range=[0, 100],
                             linecolor='#d9d9d9',
                             showgrid=False,
                             mirror=True),
                  yaxis=dict(title='Y',
                             range=[0, 100],
                             linecolor='#d9d9d9',
                             showgrid=False,
                             mirror=True))
    
    data = go.Scatter(x=df['x'],
                      y=df['y'],
                      text=df['info'],
                      textposition='bottom center',
                      textfont=dict(color='#E58606'),
                      mode='markers+text',
                      marker=dict(color='#5D69B1', size=8),
                      )
    
    fig = go.Figure(data=data, layout=layout)
    
    fig.show()
    

    结果:

    【讨论】:

    • 谢谢,但这并不是我想要的。我不希望 info 列在标记附近显示为文本。这是因为在我正在进行的实际实验中,我有很多标记,并且让这些标签出现会使整个事情变得非常混乱。
    • 哦,我刚刚意识到将mode 参数更改为mode = markers 就可以了!非常感谢!您可以通过添加此评论来更新答案吗?
    猜你喜欢
    • 1970-01-01
    • 2021-09-10
    • 2019-09-30
    • 1970-01-01
    • 2011-01-29
    • 2020-02-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多