I'm plotting a separate hexbin figure and json boundary file. The hexbin grid overlaps the boundary file though. I'm interested in displaying the African continent only. I'm aiming to cut-off or subset the hexbin grid within the African continent. So no grid square should be visualised outside the boundary file. Is there a way to achieve this using Plotly?
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
import plotly.figure_factory as ff
import geopandas as gpd
import json
data = pd.DataFrame({
'LAT': [1,5,6,7,5,6,7,5,6,7,5,6,7,12,-40,50],
'LON': [10,10,11,12,10,11,12,10,11,12,10,11,12,-20,40,50],
})
gdf_poly = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
gdf_poly = gdf_poly.drop('name', axis = 1)
Afr_gdf_area = gdf_poly[gdf_poly['continent'] == 'Africa'].reset_index(drop = True)
fig = ff.create_hexbin_mapbox(data_frame=data,
lat="LAT",
lon="LON",
nx_hexagon=25,
opacity=0.4,
labels={"color": "Point Count"},
mapbox_style='carto-positron',
zoom = 1
)
fig.update_layout(mapbox={
"layers": [
{"source": json.loads(Afr_gdf_area.geometry.to_json()),
"below": "traces",
"type": "fill",
"color": "orange",
"opacity" : 0.1,
"line": {"width": 1}
},
],
})
fig.show()
Intended output is to cut-off or clip squares outside the African contentment, which is in orange.
from Plotly hexbin cutoff within specified json boundary

No comments:
Post a Comment