【问题标题】:How do I make a heatmap in Cartopy如何在 Cartopy 中制作热图
【发布时间】:2017-07-06 22:38:53
【问题描述】:

我正在映射纬度、经度,然后在 cartopy 上绘制一个单独的值。

如何根据名为 klist 的列表使点像热图一样着色?我找不到任何可以与 cartopy 一起使用的 sn-ps 代码

该列表具有我想要根据值的大小着色的值范围。

# Define a Cartopy 'ordinary' lat-lon coordinate reference system.
crs_latlon = ccrs.PlateCarree()


def make_plot(projection_name, projection_crs):

    ax = plt.axes(projection=projection_crs)

# Set display limits to include a set region of latitude * longitude.
# (Note: Cartopy-specific).
    ax.set_extent((-65.0, -62, 44, 45.5), crs=crs_latlon)

# Add coastlines and meridians/parallels (Cartopy-specific).
    ax.coastlines(linewidth=0.2, color='black')
    ax.gridlines(crs=crs_latlon, linestyle='-')



# Mark some particular places with a small circle and a name label...
# Define some test points with latitude and longitude coordinates.
    #city_data = [('Halifax, NS', 44.67, -63.61)]

    plt.plot(lon,lat,marker='x', markersize=1.0, markeredgewidth=2.5,
             markerfacecolor='black',
             transform=crs_latlon)
# Add a title, and display.
    iplt.show("Mission #1: Attenuation Coeffiecient")

def main():
# Demonstrate with two different display projections.
    make_plot('Equidistant Cylindrical', ccrs.PlateCarree())

if __name__ == '__main__':
    main()

【问题讨论】:

  • 你应该让你的代码成为 MWE。就目前而言,这些模块不是imported。此外,klist 不会出现在您的代码中。它真的是一个列表,一个np.array...?无论如何,最好有一个带有随机/垃圾数据的klist 用于测试目的。

标签: python python-3.x matplotlib scientific-computing cartopy


【解决方案1】:

据我所知,您可以像在普通 matplotlib 中生成热图一样生成热图。只需使用pcolormesh(或pcolor 或其他)并使用正确定义的网格。在这里,我修改了@berna1111 的答案以生成彩色地图,而不是在地图上绘制圆圈。

为避免在海岸线之外绘制,您可以使用masked arraytransparency,尽管前者可能是最好的。

在下面的示例中,我提供了一个 heat_data,它是一个 numpy 数组,其中包含将进行颜色编码的数据。为了方便起见,我假设这些数据是在整个地图范围内定义的。您的数据可能会有所不同。 因为我没有实际数据,所以我根据heat_data 的范围和大小创建latlon 数组。在main() 中,我生成了一些噪声来填充heat_data 并创建绘图。

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np

def make_plot(projection_name, projection_crs, extent, heat_data):
    """
    ?
    """
    fig = plt.figure()
    rect = 0.1, 0.1, 0.8, 0.8
    ax = fig.add_axes(rect, projection=projection_crs)

    # Set display limits to include a set region of latitude * longitude.
    # (Note: Cartopy-specific).
    ax.set_extent(extent, crs=projection_crs)

    # Add coastlines and meridians/parallels (Cartopy-specific).
    ax.coastlines(linewidth=0.2, color='black')
    ax.gridlines(crs=projection_crs, linestyle='-')

    lat = np.linspace(extent[0],extent[1],heat_data.shape[0])
    lon = np.linspace(extent[2],extent[3],heat_data.shape[1])
    Lat,Lon = np.meshgrid(lat,lon)
    ax.pcolormesh(Lat,Lon,np.transpose(heat_data))
    plt.savefig("Test_fig.pdf", bbox_inches='tight')


def main():
    #extent = (-65.0, -62, 44, 45.5)
    extent = (-90, -40, 30, 60)
    # Define some test points with latitude and longitude coordinates.
    #city_data = [('Halifax, NS', 44.67, -63.61, 'black'),
    #             ('Neighbour', 45, -63, 'blue'),
    #             ('Other_Place', 44.1, -64, 'red')]
    heat_data = np.random.normal(0.0,0.2,size=(100,150))

    # Demonstrate with two different display projections.
    # Define a Cartopy 'ordinary' lat-lon coordinate reference system.
    crs_latlon = ccrs.PlateCarree()
    make_plot('Equidistant Cylindrical', crs_latlon, extent, heat_data)
    #crs_ae = ccrs.LambertCylindrical()
    #make_plot('Lambert Cylindrical', crs_ae, extent, heat_data)

if __name__ == '__main__':
    main()

【讨论】:

    【解决方案2】:

    如果您想要不同的颜色点,这可能会对您有所帮助(根据您的代码):

    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    
    
    def make_plot(projection_name, projection_crs, extent, city_data):
        """
        ?
        """
        fig = plt.figure()
        rect = 0.1, 0.1, 0.8, 0.8
        ax = fig.add_axes(rect, projection=projection_crs)
    
        # Set display limits to include a set region of latitude * longitude.
        # (Note: Cartopy-specific).
        ax.set_extent(extent, crs=projection_crs)
    
        # Add coastlines and meridians/parallels (Cartopy-specific).
        ax.coastlines(linewidth=0.2, color='black')
        ax.gridlines(crs=projection_crs, linestyle='-')
    
        # Mark some particular places with a small circle and a name label...
        for city in city_data:
            ax.plot(city[2], city[1], marker='o',
                    markersize=2.0, markeredgewidth=1.0,
                    markeredgecolor=city[3], markerfacecolor=city[3],
                    linestyle='None', label=city[0], transform=projection_crs)
        # Add a title, legend, and display.
        ax.set_title(''.join(("Mission #1: Attenuation Coeffiecient - ",
                              projection_name)))
        ax.legend()
        fig.show()
    
    
    def main():
        #extent = (-65.0, -62, 44, 45.5)
        extent = (-90, -40, 30, 60)
        # Define some test points with latitude and longitude coordinates.
        city_data = [('Halifax, NS', 44.67, -63.61, 'black'),
                     ('Neighbour', 45, -63, 'blue'),
                     ('Other_Place', 44.1, -64, 'red')]
    
        # Demonstrate with two different display projections.
        # Define a Cartopy 'ordinary' lat-lon coordinate reference system.
        crs_latlon = ccrs.PlateCarree()
        make_plot('Equidistant Cylindrical', crs_latlon, extent, city_data)
        crs_ae = ccrs.LambertCylindrical()
        make_plot('Lambert Cylindrical', crs_ae, extent, city_data)
    
    if __name__ == '__main__':
        main()
    

    我对制图学的了解不够,无法理解为什么两个投影中的点位于不同的位置,但也许您知道这意味着什么以及如何纠正它。

    【讨论】:

    • 您好,感谢您的回复,我实际上是想根据地图上的点制作带有彩条的热图。所以每个纬度和经度点都有一个附加值,它决定了它的颜色。抱歉,如果我对此不够清楚。
    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 2017-05-19
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多