Multiple sources (layers) in a single plot: source and tooltips problem

Hi, all, first time posting here so pls be gentle :slight_smile:

I am trying to plot an additional dataset over the existing (plotted) dataset.

  1. dataset1 is downloaded from GeoPandas and it is a map of the world.
  2. dataset2 is some other dataset for countries (not all countries) that I want to overplot over the existing map of the world.

The problem arises because those two datasets have different sources AND I need different tooltips for each. I cannot really merge the two datasets as they do not have the same dimensions.

So, is there a way to solve this? I basically need something equivalent to two layers but both need to show in the interactive plot. For example, I want to preserve the hover over each country (even for the ones I do not have the dataset2 info on AND I want to have the detailed hover display for the dataset2.

Here is my code for the world map (but without the dataset2. let’s say it has a list of some cities, their lats and longs, and universities in it. I use p.circle to mark it down)

from bokeh.plotting import figure, show, output_notebook
import geopandas as gp

output_notebook()

world = gp.read_file(gp.datasets.get_path('naturalearth_lowres'))
geo_source = GeoJSONDataSource(geojson=world.to_json())

p = figure(title='World', tooltips=[('Country', '@name')],
           x_range=(-180, 180), y_range=(-90, 90), 
           x_axis_location=None, y_axis_location=None,
           plot_width=1000, plot_height=500
          )
p.patches('xs', 'ys', fill_alpha=0.4, fill_color='grey', 
          line_color='black', line_width=0.5, source=geo_source
         )
show(p)

If you can help me with this, I am giving away virtual chocolate and my eternal gratitude! Thanks in advance!

1 Like

Instead of using the convenience tooltips parameter you should create and configure separate HoverTool instances. You can set the renderers property on each to restrict it to just one of the glyphs.

r1 = p.circle(...)
h1 = HoverTool(..., renderers=[r1]) # only hovers the circle
r2 = p.patches(...)
h2 = HoverTool(..., renderers=[r2]) # only hovers the patches
p.add_tools(h1, h2)

If you are saying you only want one hover tooltip, then you will have to process your data to add columns for Country and Institute the larger city dataset (and also then restrict the hover to only city circles)

1 Like

Hi, @Bryan

thanks for such a quick response!
I think I solved a similar issue 2 years ago but cannot find my old notebook anywhere. I will try that now and report back (if I do not report back soon that means I fell asleep as it is after 1 am here in the UK).

Thanks again, much appreciated! Stay safe! :slight_smile:

Hi, again, @Bryan

that solved it!!! Thank you so much! :handing over the virtual chocolate:

1 Like