Romain Jouin
Romain Jouin

Reputation: 4838

Bokeh - adding a tooltip to patches

I am new to bokeh, and try to make a simple map.

From this page http://www.abisen.com/blog/bokeh-maps/ it was quite simple to make a map. But personnalizing it needs deeper understanding of bokeh. I would like to display the name of each town in france, which I have from the french state open data (https://www.data.gouv.fr/fr/datasets/fond-de-carte-des-codes-postaux/)

For now I only get the by-default tooltip:

enter image description here

I would like to change it to add the city name :

enter image description here

I added a tooltip :

hover = HoverTool(
        tooltips=[
            ("index", "$index"),
            ("(x,y)", "($x, $y)"),
            ("city", "@city"),
        ]
    )

p = figure(title="France", tools=[hover])

But couldn't find the way to let the cities names accesible to the tooltip (so there are "???" instead of the names). I couldn't figure out how to add the city names to the data_source of the glyphs. So I tried to add a column into the data_source of each glyph by hand :

for city_name in states:
    data   = getDict(city_name, dat)
    # here I tried to add a legend => useless
    gliph_ = p.patches(data[city_name]['lat_list'], data[city_name]['lng_list'],legend=city_name,  line_color="black")

    #here I try to add a column into the glyph's data_source :
    cities = [x for x in data[city_name]['city']][0]
    gliph_.data_source.add(cities, "city")

I see that the gliph has a new column as a data source :

gliph_.data_source.column_names
>['xs', 'ys', 'city']

but it is not recognised by the tooltip :-(

how to add a data_source column into a patche ? How to make it recognised by the tooltip ?

Upvotes: 0

Views: 1071

Answers (1)

Romain Jouin
Romain Jouin

Reputation: 4838

I had a wrong data structure in my "data" dict.

I had to create an array of value for each city in the departement, with the same length, and attach it to the departement :

for new_col in ["surf", "pop", "fam", "villes", "dpt", "cp", "perimetre"]:
            gliph_.data_source.add(data[departement][new_col], new_col)

The actual adding up of the data source to the glyph is working.

Upvotes: 1

Related Questions