'legendonly' doesn't work anymore in Scattermapbox

2023-02-17T23:00:00Z

I recently installed a new Anaconda/Spyder environment with:
python version: 3.19.13
plotly version : 5.9.0
dash version: 2.8.0

Sinds then I have a problem with the funcionality of visible = ‘legendonly’ in Scattermapbox.
When I add a trace with ‘visible=True’ set, everything works fine. But when I add a trace with visible=‘legendonly’ the legenditem is added to the legend but it doesn’t show up the trace when the legenditem is activated.

-- coding: utf-8 --

“”"
Created on Sat Feb 18 09:23:57 2023

@author: herma
“”"

from plotly import graph_objs as go
import dash
from dash import dcc
from dash import html

lat_center = 52.1009166
lon_center = 5.6462914
zoom = 8

lat1_coord = [52.0, 52.2]
lon1_coord = [5.6, 5.7]

lat2_coord = [51.9, 52.0]
lon2_coord = [5.6, 5.7]

fig1 = go.Figure()
fig1.update_layout(
mapbox = dict(
style = “open-street-map”,
zoom = zoom,
center = dict(
lat = lat_center,
lon = lon_center)),
showlegend = True)

fig1.add_traces(go.Scattermapbox(
lat = lat1_coord,
lon = lon1_coord,
mode=‘lines’,
line = dict(
color = ‘red’,
width = 2),
visible =‘legendonly’
)
)

fig1.add_traces(go.Scattermapbox(
lat = lat2_coord,
lon = lon2_coord,
mode =’ lines’,
line = dict(
color = ‘blue’,
width = 2),
visible = True
)
)

app = dash.Dash(name)

app.layout = html.Div(
dcc.Graph(
id = ‘map_tiles’,
figure = fig1
)
)

host = “127.0.0.1”
port = “8050”
DEBUG = True

if name == “main”:
app.run_server(host=host, port=port, debug = DEBUG,use_reloader=False)

Hi @meijerher welcome to the forums. showlegend works as expected, your code has some other things going on, though. Here the working version.

I changed:

  • imports
  • add_trace instead of add_traces
  • dash.Dash(name) to dash.Dash(__name__)
  • if name == 'main' to if __name__ == '__main__'
import plotly.graph_objs as go
import dash
from dash import dcc, html

lat_center = 52.1009166
lon_center = 5.6462914
zoom = 8

lat1_coord = [52.0, 52.2]
lon1_coord = [5.6, 5.7]

lat2_coord = [51.9, 52.0]
lon2_coord = [5.6, 5.7]

fig1 = go.Figure()
fig1.update_layout(
    mapbox=dict(
        style="open-street-map",
        zoom=zoom,
        center=dict(
            lat=lat_center,
            lon=lon_center
        )
    ),
    showlegend=True)

fig1.add_trace(
    go.Scattermapbox(
        lat=lat1_coord,
        lon=lon1_coord,
        mode='lines',
        line=dict(
            color='red',
            width=2
        ),
        visible='legendonly'
    )
)

fig1.add_trace(
    go.Scattermapbox(
        lat=lat2_coord,
        lon=lon2_coord,
        mode=' lines',
        line=dict(
            color='blue',
            width=2
        ),
        visible=True
    )
)

app = dash.Dash(__name__)

app.layout = html.Div(
    dcc.Graph(
        id='map_tiles',
        figure=fig1
    )
)

if __name__ == "__main__":
    app.run(debug=True)

Hi AIMPED,
Thank you for the adjustments. I implemented them. However, I keep having the same problem in my environment. A non-working ’ visible=“legendonly”’ '. The problem started when I created a new environment with the latest plotly, dash and python versions.
These are:

dash                                        2.8.0              pyhd8ed1ab_0    conda-forge
dash-bootstrap-components 1.3.1              pyhd8ed1ab_0    conda-forge
plotly                                      5.9.0              py39haa95532_0
python                                    3.9.13            h6244533_1

Do you use the same versions ?

There is a small mistake in the code. The second mode=’ lines’ contains a space. It should be: mode=‘lines’. It doesn’t affect the legendonly behaviour.

Sorry for the late reply. The code from my post above works with the following versions:

Python 3.10.6
dash 2.6.2
plotly 5.10.0

I have to check again tomorrow, but I think the code did not work on an other machine with a different envoirnment. The legendonly worked but I could not show the tarce by clicking the legend.

Oke, thanks, see also mij comment about the code. There is a mistake in the second ‘ mode=‘ statement. It makes the coordinates visible as ‘ markers’ instead of ‘lines’.

I checked again, by clicking into the legend the trace is NOT shown (correcting the mentioned mode line)

plotly 5.11.0
dash 2.7.0
Python 3.10.4

Thanks for checking again. This seems to be a bug. Have you any idea what I have to do next?

Hi. You could create a new environment with the versions which worked for me, at least for testing. Is this works on your machine, you could consider using this versions.

You could also create a access token for mapbox and try this. I have the feeling, that not using a token sometimes leads to weird behavior of the plots. But that’s just my personal feeling.

You could also create a new environment with the latest versions of dash and plotly, hoping that the chart works as expected.

Hi AIMPED,
just to be clear. The correct operation of ‘legendonly’ should be that initially the trace should be hidden. However, when clicking on the trace in the legend, the trace should become visible. Then ‘legendonly’ doesn’t work for you either, does it?

Correct @meijerher. As I said above, it works in the first environment, in the second it does not.

Hi AIMPED,

From Github I got the following response:

Thanks @Hermeij - this is a problem in plotly.js, I will move the issue over there. Both parts of the problem you identified are visible in this codepen: https://codepen.io/alexcjohnson/pen/BaORKpj?editors=0010: whenever the map tried to render with the blue line displayed it throws the error: Uncaught (in promise) Error: There is already a source with this ID

1 Like

Thanks for the information.