簡體   English   中英

在多個子圖上自動 plot 相同的 shapefile

[英]Automatically plot same shapefile on multiple subplots

我正在嘗試比較兩組倫敦 Airbnb 數據。 我想要一種優雅的方式來 plot 倫敦 shapefile 在兩個子圖上,然后將不同的數據疊加為每個 map 上的點。 我的 shapefile 來自這里:

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

這是 plot 地圖的代碼:

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

結果:

Airbnb房源地圖

我的代碼工作正常,但似乎不優雅。

  1. 在每個子圖上手動繪制 shapefile 僅適用於兩個子圖,但不適用於大量子圖。 我想有一種更快的方法可以自動完成(例如循環?)
  2. 每個子圖上的一些散點圖特征(如標記形狀/大小)是相同的。 我確信有更好的方法可以為整個圖形設置這些特征,然后分別編輯每個子圖所特有的特征(如顏色)。

我不會把它寫出來,但我可以給你一些提示:

  • 是的,您可以使用循環到 plot 多個子圖,您所要做的就是遍歷要更改的多個變量列表,例如顏色和數據並在循環中使用它們
  • 使用循環時,您可以輕松訪問所需的所有不同變量,包括圖表的所有功能,例如:
c= ["blue","red","yellow"]
for x in range(3):
    plt.plot(...,color=c[x])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM