Reputation: 21
I have a geodataframe of points in epsg:27700 and want to plot these points with geoplot.kdeplot() over a raster layer which is also in epsg:27700. But I'm struggling to make this happen.
Initially I have tried geopandas add background with Contextily. Although it works but the extent of the area is very small and after converting it to epsg=3857 and using zoom level 13 to make the streetmap least visible- the points do not fall where they should.
gdf = geopandas.GeoDataFrame(df1, crs=crs, geometry=geom) #geodataframe from pandas dataframe
gdf.Prob[gdf.Prob < 0.2]= np.nan #set probability values <0.2 to nan for transparency
gdf.plot(column='Prob',figsize=(10, 10), alpha=0.5, legend=True)
rst = rasterio.open('UUS.tif')
red = rst.read(1)
bounds = (rst.bounds.left, rst.bounds.right, \
rst.bounds.bottom, rst.bounds.top)
plt.figure(num=None, figsize=(10, 10), facecolor='w', edgecolor='k')
plt.imshow(red, extent=bounds, cmap = 'gray')
I would like the points to be on the raster (kdeplot preferably). I have done it before in R with minimal effort but here I need to do it in Python, and I'm new to Python. The solution may be very simple here too, but I need a bit of suggestion. Thank you for reading this.
Upvotes: 2
Views: 2206
Reputation: 7804
You have to pass the same matplotlib ax
to both plots. So in your case, following should to the trick.
import matplotlib.pyplot as plt
gdf = geopandas.GeoDataFrame(df1, crs=crs, geometry=geom) #geodataframe from pandas dataframe
gdf.Prob[gdf.Prob < 0.2]= np.nan #set probability values <0.2 to nan for transparency
fig, ax = plt.subplots(figsize=(10, 10))
gdf.plot(column='Prob',ax=ax, alpha=0.5, legend=True)
rst = rasterio.open('UUS.tif')
red = rst.read(1)
rst.plot.show(red, ax=ax)
You might need to plot rst before gdf, not sure about that.
Upvotes: 4