I am trying to plot a collection of points onto a graphic of King County, WA.
I can show the graphic with the following code:
fig, ax = plt.subplots(figsize = (15,15))
kings_county.plot(ax=ax)
This gives me the graphic. I then read in the points from the lat/long of a csv I load.
df = pd.read_csv('kc_house_data_train.csv')
crs = {'init': 'epsg:4326'}
And this puts all of the points in a new dataframe
geometry = [Point(x,y) for x,y in zip(df.lat,df.long)]
geo_df = gpd.GeoDataFrame(df, # specifify your dataframe
crs = crs, # this is your coordinate system
geometry = geometry) # specify the geometry list we created
However, when I do this
fig, ax = plt.subplots(figsize = (40,40))
kings_county.plot(ax=ax, alpha = 0.4, color = 'grey', aspect = 'auto')
geo_df[geo_df.price >= 750000].plot(ax = ax , markersize = 20, color = 'blue',marker = 'o',label = 'pricey')
geo_df[geo_df.price < 750000].plot(ax = ax , markersize = 20, color = 'blue',marker = 'o',label = 'pricey')
plt.legend()
It just returns an error: ValueError: 'box_aspect' and 'fig_aspect' must be positive Not sure why loading the graphic works but the added points makes it break.
Just looking for understanding of this and a fix, if you can. Thanks
I solved this issue simply setting gdf.plot(aspect=1)
. It didn't happen in previous versions of geopandas, but it suddenly started happening and I figured it was simply this parameter, as it can be read in the error you gave.
I had this exact problem and found out the Shapely polygon (x,y) coordinates are (longitude, latitude), so I just had to switch the lat/long order in the zip function and everything worked properly. Thanks to this thread for the solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With