Plotly Scattermapbox,无法向标记添加文本

2024-04-22

我正在尝试将文本添加到下面的绘图中,原始代码修改自https://plotly.com/python/lines-on-mapbox/ 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()

enter image description here I have tried to copy the solution provided at Plotly Scattermapbox: Is there a way to include some text above and below the markers? https://stackoverflow.com/questions/62240079/plotly-scattermapbox-is-there-a-way-to-include-some-text-above-and-below-the-ma

请参阅下面我编辑的代码,其中包括用于从幅度列表检索数据的新文本代码和 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()

Result blank enter image description here

我想得到更像这样的结果

任何建议,将不胜感激


我发现该文本仅适用于 Mapbox 提供的矢量背景。光栅背景会导致一些 JavaScript 错误。

See https://plotly.com/javascript/mapbox-layers/#mapbox-access-tokens-and-when-you-need-them https://plotly.com/javascript/mapbox-layers/#mapbox-access-tokens-and-when-you-need-them,“基本地图...”部分第 3 项下列出的样式是有效的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Plotly Scattermapbox,无法向标记添加文本 的相关文章

随机推荐