【问题标题】:Plotly Scattermapbox, unable to add text to markersPlotly Scattermapbox,无法向标记添加文本
【发布时间】:2021-06-25 00:53:55
【问题描述】:

我正在尝试将文本添加到下面的 plotly 地图中,原始代码修改自 https://plotly.com/python/lines-on-mapbox/

import plotly.graph_objects as go

fig = go.Figure(go.Scattermapbox(
    mode = "markers",

    marker = {'size': 10}))

fig.add_trace(go.Scattermapbox(
    mode = "markers",
    lon = longlist,
    lat = latlist,
    marker = {'size': 10}))



fig.update_layout(
    margin ={'l':0,'t':0,'b':0,'r':0},
    mapbox = {
        
        'style': "stamen-terrain",
        'center': {'lon': -20, 'lat': -20},
        'zoom': 1})

fig.show()

我试图复制Plotly Scattermapbox: Is there a way to include some text above and below the markers?提供的解决方案

在下面查看我编辑的代码,其中包括从幅度列表中检索数据的新文本代码和 mapbox_access_token 的新代码

import plotly.graph_objects as go

mapbox_access_token = 'mytoken'


fig = go.Figure(go.Scattermapbox(
    mode = "markers",

    marker = {'size': 10}))

data = fig.add_trace(go.Scattermapbox(
    mode = "markers",
    lon = longlist,
    lat = latlist,
    marker = {'size': 10},
    textposition='top right',
    textfont=dict(size=16, color='black'),
    text = magnitudelist
))


layout = dict(margin=dict(l=0, t=0, r=0, b=0, pad=0),
              mapbox=dict(accesstoken=mapbox_access_token,
                          center=dict(lat=-20, lon=-20),
                          style='stamen-terrain',
                          zoom=1))


fig = go.Figure(data=data, layout=layout)
fig.show()

结果空白

我正在尝试获得更像这样的结果

任何建议将不胜感激

【问题讨论】:

    标签: plotly mapbox mapbox-marker


    【解决方案1】:

    我遇到了同样的问题,请尝试以下方法:

    1. 在 Scattermapbox 模式中包含“文本”并指定要显示的字符串或字符串数​​组:
    fig.add_trace(go.Scattermapbox(
          mode = "markers+text", # include text mode
          lon = longlist,
          lat = latlist,
          marker = {'size': 10},
          text = textlist)) # include text list
    
    1. 尝试其他 mapbox 样式,例如“light”:
    layout = dict(margin=dict(l=0, t=0, r=0, b=0, pad=0),
                  mapbox=dict(accesstoken=mapbox_access_token,
                              center=dict(lat=-20, lon=-20),
                              style='light', # change mapbox style
                              zoom=1))
    

    【讨论】:

    • 感谢您的建议。我还没开始尝试这个,但它看起来是个好主意。
    【解决方案2】:

    这是一个在卫星地图上的标记内绘制文本的工作示例

    import plotly.graph_objects as go
    
    # create our figure blank which we will build onto. the biggest thing i saw
    # was that you have to specify the mode that will show markers as well as text
    
    fig = go.Figure(
          go.Scattermapbox(
          mode = "lines+markers+text",
        )
    )
    
    
    # Make sure to use your access token and draw the polygon/lines/features you want
    
    fig.update_layout(
        mapbox = {
            'style': "satellite",
            'accesstoken': "pk.your_access_token",
            'center': { 'lon': -73.605, 'lat': 45.51},
            'zoom': 15,       
            'layers': [{
                'source': {
                    'type': "FeatureCollection",
                    'features': [
                        {
                        
                            'type': "Feature",
                            'geometry': {
                            'type': "MultiPolygon",
                            'coordinates': [[[
                                [-73.606352888, 45.507489991], [-73.606133883, 45.50687600],
                                [-73.605905904, 45.506773980], [-73.603533905, 45.505698946],
                                [-73.602475870, 45.506856969], [-73.600031904, 45.505696003],
                                [-73.599379992, 45.505389066], [-73.599119902, 45.505632008],
                                [-73.598896977, 45.505514039], [-73.598783894, 45.505617001],
                                [-73.591308727, 45.516246185], [-73.591380782, 45.516280145],
                                [-73.596778656, 45.518690062], [-73.602796770, 45.521348046],
                                [-73.612239983, 45.525564037], [-73.612422919, 45.525642061],
                                [-73.617229085, 45.527751983], [-73.617279234, 45.527774160],
                                [-73.617304713, 45.527741334], [-73.617492052, 45.527498362],
                                [-73.617533258, 45.527512253], [-73.618074188, 45.526759105],
                                [-73.618271651, 45.526500673], [-73.618446320, 45.526287943],
                                [-73.618968507, 45.525698560], [-73.619388002, 45.525216750],
                                [-73.619532966, 45.525064183], [-73.619686662, 45.524889290],
                                [-73.619787038, 45.524770086], [-73.619925742, 45.524584939],
                                [-73.619954486, 45.524557690], [-73.620122362, 45.524377961],
                                [-73.620201713, 45.524298907], [-73.620775593, 45.523650879]
                            ]]]
                        }
                    }]
                },
                'type': "line", 'below': "traces", 'color': "red"}]},
        margin = {'l':0, 'r':0, 'b':0, 't':0}
    )
    
    
    # finally add a trace of the markers you want as a list
    
    fig.add_trace( go.Scattermapbox(
        lat=[45.51, 45.50687600],
        lon=[-73.605, -73.606133883,],
        marker={
            "color": "cyan",
            "size": 30,
    
        },
        mode="markers+text",
        text=["21:09", "06:11", ],
        textfont = {'family': "Times", 'size': 12, 'color': "MediumPurple"},
        
        textposition="middle center"
    ))
    
    
    

    最终结果如下所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      相关资源
      最近更新 更多