Search code examples
pythoncoordinatesbokehcoordinate-transformationhexagonal-tiles

Bokeh hexbin - find original indexes of each hexagon


I am using Bokeh 0.12.15 version, which generates a great hexbin plot. I wonder how can I easily find the indexes of the values of each hexagon?

for example for the code below (https://docs.bokeh.org/en/latest/docs/gallery/hexbin.html):

import numpy as np

from bokeh.io import output_file, show
from bokeh.models import HoverTool
from bokeh.plotting import figure

n = 500
x = 2 + 2*np.random.standard_normal(n)
y = 2 + 2*np.random.standard_normal(n)

p = figure(title="Hexbin for 500 points", match_aspect=True,
           tools="wheel_zoom,reset", background_fill_color='#440154')
p.grid.visible = False

r, bins = p.hexbin(x, y, size=0.5, hover_color="pink", hover_alpha=0.8)

p.circle(x, y, color="white", size=1)

hover = HoverTool(tooltips=[("count", "@c"), ("(q,r)", "(@q, @r)")],
                  mode="mouse", point_policy="follow_mouse", renderers=[r])

p.add_tools(hover)

output_file("hexbin.html")

show(p)

I would like to find for each hexbin the indexes of the tuple (x,y) which are inside it

Thanks


Solution

  • EDIT: OK I just realized (I think) that you are asking a different question. To find out the indices of the original points in each tile, you will have to basically recreate what hexbin does itself:

    In [8]: from bokeh.util.hex import cartesian_to_axial
    
    In [8]: import pandas as pd
    
    In [9]: q, r = cartesian_to_axial(x, y, 0.5, "pointytop")
    
    In [10]: df = pd.DataFrame(dict(r=r, q=q))
    
    In [11]: groups = df.groupby(['q', 'r'])
    
    In [12]: groups.groups
    Out[12]:
    {(-4, -3): Int64Index([272], dtype='int64'),
     (-4, 0): Int64Index([115], dtype='int64'),
     (-4, 3): Int64Index([358], dtype='int64'),
     (-4, 4): Int64Index([480], dtype='int64'),
     (-3, -1): Int64Index([323], dtype='int64'),
     (-3, 2): Int64Index([19, 229, 297], dtype='int64'),
     ...
     (11, -5): Int64Index([339], dtype='int64'),
     (12, -7): Int64Index([211], dtype='int64')}
    

    Here each key for each entry in the groups dict is the (q,r) axial hex coordinate of the tile, and the value is a list of the indices of the points that were in that tile.


    Old Answer

    That information is in the bins DataFrame that is returned:

    In [3]: bins.head()
    Out[3]:
       q  r  counts
    0 -4 -3       1
    1 -4  0       1
    2 -4  3       1
    3 -4  4       1
    4 -3 -1       1
    

    Here q and r are Axial Hex Grid Coordinates. If you want the Cartesian x and y coordinates of the hex tile centers, you can use the axial_to_cartesian function.