简体   繁体   中英

How to add customdata to a plotly Scattermapbox plot

I am displaying different climatic measuring stations in a map with go.Scattermapbox . Now I would like to include multiple different values in the hover by using customdata in the hovertemplate command. This works fine for values that are part of the graph (like lat, lon) but I can not figure out how to make the customdata command work with external data (eg adding values for altitude in the hover box).

plot_data = [
    go.Scattermapbox(
        lon=data_slctd["longitude"], 
        lat=data_slctd["latitude"], 
        mode="markers", 
        text = data_slctd.site_name.tolist(), 
        hovertemplate =  "<b>%{text}</b><br><br>" + "longitude: %{lon:.2f}<br>" + "latitude: %{lat:.2f}<br>" + "altitude: %{customdata[0]:.0f}<br>"+ "ppm: %{marker.color:.2f}<extra></extra>",
    )
]

As explained in the tutorial https://plotly.com/python-api-reference/generated/plotly.graph_objects.Scattermapbox.html (under "Adding other data to the hover with customdata and a hovertemplate") I have tried to include the customdata == np.dstack((hover_1, hover_2)) command within the go.Scattermapbox but trying to access the first values for hover_1 in the hovertemplate with %customda[0}:.0f did not work.

hover_1 and hover_2 hold the values for altitude respective measuring methods for all stations displayed (ie each has the same length as the number of stations).

That's what the hover looks like:

Screenshot of exemplary hover

Maybe the customdata was not displayed because the length of the customdata array did not match with the length of the lat and lon variables. When you do customdata[0] , you are trying to get the first element of the sublist of the customdata array. For example, if you have

x = [1,2,3],
y = [3,4,5]

in a plotly figure, then customdata should be something like:

customdata = [
  ["a", "b"],
  ["c", "d"],
  ["e", "f"],
]

That way when you do customdata[0] and hover on (1,3) you will get a . If you hover on 2,4 and do customdata[1] you will get d .

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