0

I have a pandas Dataframe with 3D coordinates (longitude, latitude, altitude) and used hexbin(longitude, latitude, 0.0001) to create the source for hex_tile(). I looked at the tutorial where the fill_color is calculated as linear_cmap('counts', 'Viridis256', 0, max(bins.counts)). Instead of the counts, how can I color the hexes by the mean of the altitude of the points in the bins?

G Knucklez
  • 11
  • 2
  • You would have to compute that mean altitude for every hex region, and add a column to the `ColumnDataSource` with that data, so that `linear_cmap` can refer to it. I think your actual question is how to compute those means. For that you will need to study the (very short) `hexbin` implementation: https://github.com/bokeh/bokeh/blob/branch-3.0/src/bokeh/util/hex.py `hexbin` uses a Pandas groupby on the axial coordinates to extract a count, but you could do a different aggregation (e.g max or mean) instead. – bigreddot Oct 10 '22 at 22:22

1 Answers1

0

Thanks to the tips of bigreddot I was able to do it:

from bokeh.util.hex import cartesian_to_axial  

q, r = cartesian_to_axial(df["longitude"].values, df["latitude"].values, 0.0001, "pointytop")  
df2 = pandas.DataFrame(dict(r=r, q=q, a=df["altitude"]))  
gb = df2.groupby(['q', 'r']).mean().reset_index()

Then use gb as source for hex_tile() and "a" as value for linear_cmap()

mosc9575
  • 5,618
  • 2
  • 9
  • 32
G Knucklez
  • 11
  • 2