简体   繁体   English

Python Plotly object 带有不同标记颜色的图例

[英]Python Plotly object with legend for different marker_color

I want to add a legend for each marker_color on my second trace below but I can't figure out how to make it happen through the graph objects.我想在下面的第二条轨迹上为每个 marker_color 添加一个图例,但我不知道如何通过图形对象实现它。 Any idea?任何想法?

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

在此处输入图像描述

Moved around by using a for loop and specifying the type of marker when we are on the outliers value:通过使用 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() 

在此处输入图像描述

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

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