简体   繁体   中英

Add markers NAME on the map using Plotly Scattermapbox traces

I need support to add markers NAME on the map using Plotly Scattermapbox traces.

The below code shows markers and lines, I want to show markers name on the map:

import plotly.graph_objects as go

fig = go.Figure(go.Scattermapbox(
    mode = "markers+lines",
    lon = [10, 20, 30],
    lat = [10, 20,30],
    marker = {'size': 10}))

fig.add_trace(go.Scattermapbox(
    mode = "markers+lines",
    lon = [-50, -60,40],
    lat = [30, 10, -20],
    marker = {'size': 10}))

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

fig.show()

You can use mode = "markers+text+lines" and also provide a text field

mode – Determines the drawing mode for this scatter trace. If the provided mode includes “text” then the text elements appear at the coordinates. Otherwise, the text elements appear on hover.

fig = go.Figure(go.Scattermapbox(
    mode = "markers+text+lines",
    lon = [10, 20, 30],
    lat = [10, 20,30],
    marker = {'size': 10},
    text = ["name1", "name2", "name3"], 
    textposition = "bottom right")
)

There is a known bug that prevents non mapbox styles from showing the text

https://github.com/plotly/plotly.js/issues/4110

Logging in to mapbox and creating a new custom, solved this issue for some people:

https://community.plotly.com/t/scattermapbox-doesnt-work-with-marker-mode-as-text/35065/4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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