【问题标题】:Overlay scatter plot on map (img)地图上的叠加散点图 (img)
【发布时间】:2017-08-29 20:39:31
【问题描述】:

为了自己的学习目的,我正在使用住房数据集,我希望能够将我的地块叠加在地图上,以便更好地了解“热点”。

我的代码如下:

housing = pd.read_csv('https://raw.githubusercontent.com/ageron/handson-ml/master/datasets/housing/housing.csv')

plt.figure()
housing.plot(x='longitude', y='latitude', kind='scatter', alpha=0.4, 
             s= housing['population']/100, label='population', figsize=(10,7),
             c= 'median_house_value', cmap=plt.get_cmap('jet'), colorbar=True, zorder=5)
plt.legend()
plt.show()

我保存为'California.png'的图片

这是我尝试过的:

img=imread('California.png')

plt.figure()
plt.imshow(img,zorder=0)
housing.plot(x='longitude', y='latitude', kind='scatter', alpha=0.4, 
             s= housing['population']/100, label='population', figsize=(10,7),
             c= 'median_house_value', cmap=plt.get_cmap('jet'), colorbar=True, zorder=5)
plt.legend()
plt.show()

但这只是给了我两个情节。我试过切换索引无济于事。

有没有一种简单的方法可以做到这一点?谢谢。

编辑:使用@nbeuchat 下面的代码:

plt.figure(figsize=(10,7))
img=imread('California.png')

plt.imshow(img,zorder=0)
ax = plt.gca()
housing.plot(x='longitude', y='latitude', kind='scatter', alpha=0.4, 
         s= housing['population']/100, label='population', ax=ax,
         c= 'median_house_value', cmap=plt.get_cmap('jet'), colorbar=True, 
         zorder=5)
plt.legend()
plt.show()

我得到以下情节:

【问题讨论】:

    标签: python image plot


    【解决方案1】:

    好的,这个问题很老了,但我有一个不同的答案,可能有人会感兴趣......

    我一直在处理完全相同的问题。 GitHub (https://github.com/ageron/handson-ml.git) 上提供的代码可以满足您的需求(请参阅 02_end_to_end_machine_learning_project.ipynb)。

    但是,该代码使用加利福尼亚地图作为图像,并仅在其顶部绘制点。一种替代方法是构建真实地图,并在其上绘制点,而无需读取 ma 图像。为此,我使用了下面的代码。您需要install cartopy,如果您还需要县线,您必须使用here 中的说明绘制它们。

    最后生成的图片是这样的:

    这是我使用的代码:

    # Trying to use a real map
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    
    plt.figure(figsize=(10,7))
    
    # Creates the map
    ca_map = plt.axes(projection=ccrs.PlateCarree())
    
    ca_map.add_feature(cfeature.LAND)
    ca_map.add_feature(cfeature.OCEAN)
    ca_map.add_feature(cfeature.COASTLINE)
    ca_map.add_feature(cfeature.BORDERS, linestyle=':')
    ca_map.add_feature(cfeature.LAKES, alpha=0.5)
    ca_map.add_feature(cfeature.RIVERS)
    ca_map.add_feature(cfeature.STATES.with_scale('10m'))
    
    # To add county lines
    import cartopy.io.shapereader as shpreader
    
    reader = shpreader.Reader('datasets/housing/countyl010g.shp')
    counties = list(reader.geometries())
    COUNTIES = cfeature.ShapelyFeature(counties, ccrs.PlateCarree())
    ca_map.add_feature(COUNTIES, facecolor='none', edgecolor='gray')
    
    ca_map.xaxis.set_visible(True)
    ca_map.yaxis.set_visible(True)
    
    # Plots the data onto map
    plt.scatter(housing['longitude'], housing['latitude'], alpha=0.4, 
                s=housing["population"]/100, label="population",
                c=housing['median_house_value'], 
                cmap=plt.get_cmap("jet"), 
                transform=ccrs.PlateCarree())
    
    # Colorbar
    prices = housing["median_house_value"]
    tick_values = np.linspace(prices.min(), prices.max(), 11)
    cbar = plt.colorbar()
    cbar.ax.set_yticklabels(["$%dk"%(round(v/1000)) for v in tick_values], fontsize=14)
    cbar.set_label('Median House Value', fontsize=16)
    
    # Plot labels
    plt.ylabel("Latitude", fontsize=14)
    plt.xlabel("Longitude", fontsize=14)
    plt.legend()
    
    save_fig("housing_prices_scatterplot_cartopy")
    

    这里的优点是使用真实地图,现在可以轻松更改此代码以用于您想要使用的世界任何部分。玩得开心!

    【讨论】:

      【解决方案2】:

      您正在使用数据框绘图功能创建一个新图形。您应该传递要在其上绘制第二个图的轴。一种方法是使用gca 获取当前坐标轴。

      以下应该可以工作(虽然未经测试):

      plt.figure(figsize=(10,7))
      img=imread('California.png')
      
      plt.imshow(img,zorder=0,extent=[housing['longitude'].min(),housing['longitude'].max(),housing['latitude'].min(),housing['latitude'].max()])
      ax = plt.gca()
      housing.plot(x='longitude', y='latitude', kind='scatter', alpha=0.4, 
               s= housing['population']/100, label='population', ax=ax,
               c= 'median_house_value', cmap=plt.get_cmap('jet'), colorbar=True, 
               zorder=5)
      plt.legend()
      plt.show()
      

      编辑:使用imshowextent 参数以及经度和纬度数据的最小值和最大值将正确缩放图像。

      【讨论】:

      • 绘图看起来很奇怪,散点图很小而且在角落里。
      • imshow 不知道 1px 在纬度/经度方面的含义。您需要明确设置它。您可以使用imshowextent 参数。几周前我就这样做了,让我检查一下我的笔记本
      • 这样做还具有在您的绘图上具有正确 x 和 y 刻度标签的优势
      • 天才!非常感谢,让我省了很多麻烦。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2012-06-26
      • 2014-03-31
      • 1970-01-01
      • 2018-03-17
      • 2014-07-14
      • 1970-01-01
      相关资源
      最近更新 更多