简体   繁体   中英

Osmnx: how to make plot_shape() work in pyplot subplots?

I've been using OSMNX to extract shapes of parks from Open Street Maps. I am trying to display them as a standard pyplot subplots but I can't quite make it work directly.

Say this is my array of places:

places = 
     {'Hyde Park'       : 'Hyde Park, London, UK',
      'Kensington Gardens'        : 'Kensington Gardens, London, UK',
      'Regents Park'       : 'Regents Park, London, UK',
      'Hampstead Heath' : 'Hampstead Heath, London, UK',
      'Alexandra Park' : 'Alexandra Park, London, UK',
      'Clissold Park' : 'Clissold Park, London, UK',
      'Finsbury Park' : 'Finsbury Park, N4 2NQ, London, UK',
      'Russell Square' : 'Russell Square, London, UK'
     }

The following, correctly, displays the shapes stacked:

for place in sorted(places.keys()):
    query = places[place]
    print(query)
    G = ox.gdf_from_place(query)
    fig, ax = ox.plot_shape(G)

I'm not a massive expert of pyplot/OSMNX, but what I understand is that in order to pass the shape graph to a subplot, I need to somehow "extract the axes".

However, I know how to take the shape, convert it to a shapefile, and display it in a subplot:

import shapefile
n = len(places)
ncols = int(np.ceil(np.sqrt(n)))
nrows = int(np.ceil(n / ncols))
figsize = (ncols * 3, nrows * 3)

fig, axes = plt.subplots(nrows, ncols, figsize=figsize, subplot_kw, {'projection':None})
axes = [item for sublist in axes for item in sublist]

for ax, place in zip(axes, sorted(places.keys())):
    query = places[place]
    G = ox.gdf_from_place(query)
    ox.save_gdf_shapefile(G, folder='shp', filename=place)

    shp = shapefile.Reader("shp/"+place+"/"+place+".shp")

    for shape in shp.shapeRecords():
        x = [i[0] for i in shape.shape.points[:]]
        y = [i[1] for i in shape.shape.points[:]]
        ax.plot(x,y)

Is it possible to generate the same chart, using plot_shape() directly?

A couple of options, depending on what your end goal is:

import osmnx as ox
import matplotlib.pyplot as plt
ox.config(use_cache=True, log_console=True)

# first get a GeoDataFrame of neighborhood geometries
places = ['Hyde Park, London, UK',
          'Kensington Gardens, London, UK',
          'Regents Park, London, UK',
          'Hampstead Heath, London, UK',
          'Alexandra Park, London, UK',
          'Clissold Park, London, UK',
          'Finsbury Park, N4 2NQ, London, UK',
          'Russell Square, London, UK']
gdf = ox.geocode_to_gdf(places)

# OPTION 1: display all neighborhoods in a single ax
ax = gdf.plot()
ax.axis('off')

# OPTION 2: display each neighborhood in its own ax in a single fig
fig, axes = plt.subplots(nrows=2, ncols=4)
for i, ax in zip(gdf.index, axes.flat):
    gdf.loc[i:i].plot(ax=ax)
    ax.axis('off')
plt.show()

Note that as of OSMnx v0.15.0, the gdf_from_place and gdf_from_places functions have been deprecated and replaced by the geocode_to_gdf function. See the docs for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM