【问题标题】:Automatically plot same shapefile on multiple subplots在多个子图上自动绘制相同的 shapefile
【发布时间】:2022-08-09 21:27:42
【问题描述】:

我正在尝试比较两组伦敦 Airbnb 数据。我想要一种优雅的方式在两个子图上绘制伦敦 shapefile,然后将不同的数据叠加为每个地图上的点。我的 shapefile 来自这里:

londonshp = gpd.read_file(\"statistical-gis-boundaries london\\ESRI\\London_Borough_Excluding_MHW.shp\")
londonshp = londonshp.to_crs(4326)`

这是绘制地图的代码:

fig, axes = plt.subplots(ncols=2, figsize = (12,16))
#entire home/apt on left
axes[0].set_aspect(\'equal\')
londonshp.plot(ax = axes[0],
               color = \'#e0e1dd\', 
               edgecolor = \'#1c1c1c\')
axes[0].scatter(entirehomedf.longitude,
                entirehomedf.latitude,
                s = 1,
                c = \'#2ec4b6\',
                marker = \'.\')
axes[0].set_yticklabels([])
axes[0].set_xticklabels([])
axes[0].set_title(\"Entire Homes/Apts\")
#private room on right
axes[1].set_aspect(\'equal\')
londonshp.plot(ax = axes[1],
               color = \'#e0e1dd\', 
               edgecolor = \'#1c1c1c\')
axes[1].scatter(privateroomdf.longitude,
                privateroomdf.latitude,
                s = 1,
                c = \'#ff9f1c\')
axes[1].set_yticklabels([])
axes[1].set_xticklabels([])
axes[1].set_title(\"Private Rooms\")

结果:

我的代码工作正常,但似乎不优雅。

  1. 在每个子图上手动绘制 shapefile 仅适用于两个子图,但不适用于大量子图。我想有一种更快的方法可以自动完成(例如循环?)
  2. 每个子图上的一些散点图特征(如标记形状/大小)相同。我确信有一种更好的方法可以为整个图形设置这些特征,然后分别编辑每个子图各自的特征(如颜色)。

    标签: python matplotlib plot shapefile subplot


    【解决方案1】:

    我不会把它写出来,但我可以给你一些提示:

    • 是的,您可以使用循环来绘制多个子图,您所要做的就是遍历要更改的多个变量列表,例如颜色和数据并在循环中使用它们
    • 使用循环时,您可以轻松访问所需的所有不同变量,包括图表的所有功能,例如:
    c= ["blue","red","yellow"]
    for x in range(3):
        plt.plot(...,color=c[x])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多