Black Lives Matter. Please consider donating to Black Girls Code today.

Dimension of hexbin grid

What is the dimension of the hexagonal grids created with plotly.figure_factory? Following code produces a map with hexagonal binning of data. How do we get to know the dimensions of each hexagon created over?

fig = ff.create_hexbin_mapbox(
data_frame=df, lat=“centroid_lat”, lon=“centroid_lon”,
nx_hexagon=10, opacity=0.5, labels={“color”: “Point Count”},

You cannot at the moment set the dimension of the hexagon but you can increase or decrease the number of hexagons with the nx_hexagon parameter (it sets the number of hexagons horizontally).

Is there any way by which we can know the dimensions of the hexagon grids created in our Python?

Give that these hexbins are made to look like regular hexagons on a merkator projection, there is no easy answer to this question.

The only real metric you can get is the longitude extend of one hexagon: it is equal to the longitude extent of your points divided by the number of horizontal hexagons: (lon_max - lon_min) / nx_hexagon. This gives you the horizontal size of a hexagon in degrees.

Then to convert it to meters, you would need to use the Earth radius and the local latitude. This can be approximated if your data does not span on too large a scale in terms of latitude. If you are in conditions where the latitude extent is small (~1 degree max, especially further away from the equator), you could get the horizontal size of the hexagon with the following formula:

earth_radius = 6.371e6  # meters
# Horizontal size in meters
size_x = (lon_max - lon_min) / nx_hexagon * np.pi / 180 * earth_radius  * np.cos(lat_mean)