【问题标题】:Python Plotly object with legend for different marker_color具有不同标记颜色图例的 Python Plotly 对象
【发布时间】:2022-01-09 19:21:34
【问题描述】:

我想在下面的第二条轨迹上为每个 marker_color 添加一个图例,但我不知道如何通过图形对象实现它。 有什么想法吗?

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(
    go.Scattergeo(
        lat = df_outliers['Lat'], 
        lon = df_outliers['Long'], 
        marker = {'color' : 'grey',
                   'size': 5, 
                  'symbol': 'diamond'},
        name = "Stations outside clusters"
                )
            )

fig.add_trace(
    go.Scattergeo(
        lat = df_clusters['Lat'], 
        lon = df_clusters['Long'],
        mode = 'markers',
        marker_color = df_clusters['Clus_Db']
                )
            )

fig.update_layout(width=800, height=500,
                  margin=dict(r=0, b=20, l=0, t=50),
                  title = {
                     'text': "Weather Station in Canada",
                     'y':0.95,
                     'x':0.5,
                     'xanchor': 'center',
                     'yanchor': 'top' },
                 geo_scope='north america'
                 )
fig.show()

【问题讨论】:

    标签: python object plotly legend trace


    【解决方案1】:

    当我们处于离群值时,使用 for 循环并指定标记的类型来移动:

    import plotly.graph_objects as go
    import pandas as pd
    
    fig = go.Figure()
    
    for C in list(df.Clus_Db.unique()):
        # Adding trace for outlier
        if C==-1:
            fig.add_trace(go.Scattergeo(
                            lat = df[df.Clus_Db == C]['Lat'],
                            lon = df[df.Clus_Db == C]['Long'],
                            name = 'Stations outside clusters',
                            marker = {'color' : 'grey','size': 5,'symbol': 'diamond'}
                                        )
                            )
            
        # Adding trace for clusters
        else:
            fig.add_trace(go.Scattergeo(
                            lat = df[df.Clus_Db == C]['Lat'],
                            lon = df[df.Clus_Db == C]['Long'],
                            name = 'Cluster ' + str(C))
                         )
        
    fig.update_layout(width=800, height=500,
                      margin=dict(r=0, b=20, l=0, t=50),
                      title = {
                         'text': "Weather Station in Canada",
                         'y':0.95,
                         'x':0.5,
                         'xanchor': 'center',
                         'yanchor': 'top' },
                     geo_scope='north america'
                     )
    
    fig.show() 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-16
      • 2022-11-26
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 2017-05-25
      相关资源
      最近更新 更多