繁体   English   中英

Osmnx:如何使 plot_shape() 在 pyplot 子图中起作用?

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

我一直在使用 OSMNX 从开放街道地图中提取公园的形状。 我试图将它们显示为标准的 pyplot 子图,但我不能让它直接工作。

说这是我的地方数组:

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'
     }

以下正确显示堆叠的形状:

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

我不是 pyplot/OSMNX 的专家,但我理解的是,为了将形状图传递给子图,我需要以某种方式“提取轴”。

但是,我知道如何获取形状,将其转换为 shapefile,并将其显示在子图中:

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)

是否可以直接使用 plot_shape() 生成相同的图表?

有几个选项,具体取决于您的最终目标是什么:

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()

请注意,从 OSMnx v0.15.0 开始, gdf_from_placegdf_from_places函数已被弃用并替换为geocode_to_gdf函数。 有关详细信息,请参阅文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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