Density mapbox tooltips

Hi!!

I’ve had a problem with tooltips in the density heatmap:
The documentation specifies that for showing text in tooltips you can pass a string or an array, but, while when I pass a String it works, when an array is given it doesn’t

Can anybody help me?

Thank you so much!


        density['data'] = [
            go.Densitymapbox(
                lat=lat, # Array
                lon=lon, # Array
                radius=20,
                z=values,
                text=texttype, # => This is a string, I need an Array
                name=dtype.upper(),
                hoverinfo='z+text',
                opacity=1
            )
        ]

Captura de pantalla de 2019-11-27 12-20-41

DOCS

text
Parent: data[type=densitymapbox]
Type: string or array of strings
Default: ""

Sets text elements associated with each (lon,lat) pair If a single string, the same string appears over all the data points. If an array of string, the items are mapped in order to the this trace’s (lon,lat) coordinates. If trace hoverinfo contains a “text” flag and “hovertext” is not set, these elements will be seen in the hover labels.

Hi @Sampal, below is a working example passing the array as customdata and then using a hovertemplate (which is the recommended way to tune the tooltip in plotly.py). Please see https://plot.ly/python/hover-text-and-formatting/#customize-tooltip-text-with-a-hovertemplate for more info on the hovertemplate.

import pandas as pd
quakes = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

import plotly.graph_objects as go
fig = go.Figure(go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude, radius=10,
                                customdata=quakes.Date, hovertemplate='z: %{z} <br> %{customdata}',
                                ))
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
1 Like

Also, would you mind modifying the title of the post so that it says “density mapbox” instead of “density heatmap” ? It would make easier for people for are looking for information in the forum.

2 Likes

Thank you very much @Emmanuelle, my problem is solved with this solution.

Title updated :wink:

1 Like