0

I am working with Asheville Airbnb dataset and want to plot the neighborhoods based on their lat and lng, but it won't map together!

shp_path = '../Airbnb_Asheville/Asheville_City_Limits/Asheville_City_Limits.shp'
gdf = gpd.read_file(shp_path)

geom2 = ashvile_list[['latitude', 'longitude','neighbourhood_cleansed','room_type']]
geom2 = geom2.rename(columns={'neighbourhood_cleansed': 'neighbourhood'})
geometry = [Point(xy) for xy in zip (geom2["latitude"], geom2["longitude"])]

crs = {'init': 'epsg:3857'}
geo_df = gpd.GeoDataFrame(geom2, crs=crs, geometry=geometry)

fig, gax = plt.subplots(figsize=(10,10))
gdf.boundary.plot(ax=gax)
geo_df.plot(ax=gax, color='red', alpha = 0.5)

gax.spines['top'].set_visible(False)
gax.spines['right'].set_visible(False)
gax.set_title('Asheville Airbnb')

plt.show()

The result of above code:

enter image description here

here is the geo_df.head():

enter image description here

and gdf.head():

enter image description here

I am using this tutorial to do so: https://datascience.quantecon.org/applications/maps.html

Sepid
  • 107
  • 1
  • 10
  • Your two datasets are in different projections. Read the geopandas docs on managing projections and figure out what the crs of the two data sources are – Michael Delgado Mar 08 '23 at 22:32

1 Answers1

1

Update

Hey, so in general you have multiple problems here...

  1. as initially mentioned your data is given in lon/lat so your crs is most probably epsg=4326 not epsg=3857 (also note its lon/lat not lat/lon !)
  2. Looking at your data (from here), you are missing accompanying files to geolocate the shape (e.g. the spatial-index file <filename>.shx and the projection description <filename>.prj). I downloaded the shapefile incl. all aux. files from here.
  3. since you use normal matplotlib axes, you need to handle reprojections manually! (e.g. you need to bring all your data and the shape in the same crs before you can put them on the same plot... (your data is in epsg=4326 but your shape is in epsg=2264) for GeoDataFrames you can use gdf.to_crs(...))

I strongly recommend that you have a look at cartopy or EOmaps and use projection-aware matplotlib axes to visualize your data!

... finally, here's how I would plot it using EOmaps :-)

from eomaps import Maps
import geopandas as gpd
import pandas as pd

# read the data (no need to manually specify crs if .prj and .shx files are available!)
gdf = gpd.read_file(r"Asheville_City_Limits\Asheville_City_Limits.shp")
data = pd.read_csv(r"Asheville_listings.csv")

m = Maps(crs=Maps.CRS.GOOGLE_MERCATOR, figsize=(6, 8))
m.add_gdf(gdf, fc="none", ec="darkblue", lw=2)

# plot the data
m.set_data(data, "longitude", "latitude", crs=4326, parameter="availability_365")
m.set_shape.scatter_points(size=5)
m.set_classify.EqualInterval(k=5)
m.plot_map(ec="k", lw=0.25)
m.add_colorbar(hist_bins=100, label="Availability 365")

# add a background map
m.add_wms.OpenStreetMap.add_layer.default(alpha=0.5)

enter image description here


Old answer

It's difficult to say without having access to the actual data, but from what I see you simply picked the wrong epsg code for your data!

epsg 3857 is a pseudo-mercator projection (with units of "meters") not lon/lat... you most probably want epsg 4326!

e.g. since geometry is provided in lon/lat, the crs should be:

crs = {'init': 'epsg:4326'}
raphael
  • 2,159
  • 17
  • 20
  • when I use the espg 4326 the map will shrink to the left side! – Sepid Mar 13 '23 at 03:29
  • of course... because that's where you're coordinates really are! You should use a projection-aware axes (e.g. use [cartopy](https://scitools.org.uk/cartopy/docs/latest/index.html#) or [EOmaps](https://github.com/raphaelquast/EOmaps) to properly handle coordinate-projections... at the moment you plot your shapes and points as if they are in the same projection but they are not! if you want to stick with a non-projection aware axes you need to handle reprojections yourself (use `gdf.to_crs(....)` before plotting) – raphael Mar 13 '23 at 08:00
  • Is there any way I share my code with you? I still didn't figure out how to solve this issue! I have tried this one https://stackoverflow.com/questions/38961816/geopandas-set-crs-on-points too but still got the same problem! – Sepid Mar 28 '23 at 16:45
  • hey, its not so much the code... its the data I would need to pinpoint your issue... if you upload it somewhere and share the link I can have a look (a subset is also fine if its a large dataset). – raphael Mar 28 '23 at 21:13
  • I have uploaded them to my github: https://github.com/sepideh68/Airbnb I appreciate if you have time to look at it. Thanks. – Sepid Mar 30 '23 at 16:47
  • hey @Sepid, check my updates for the answer! – raphael Mar 31 '23 at 14:21