Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphing points on a map but the error code is "ValueError: 'box_aspect' and 'fig_aspect' must be positive"

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

like image 258
Paul Avatar asked Jul 25 '20 15:07

Paul


2 Answers

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.

like image 82
Feliu Serra Avatar answered Oct 21 '22 11:10

Feliu Serra


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.

like image 3
Tyler P. Avatar answered Oct 21 '22 11:10

Tyler P.