【问题标题】:Color Cartopy map countries according to given values根据给定值的颜色 Cartopy 地图国家
【发布时间】:2020-08-11 02:55:59
【问题描述】:

我正在与熟悉 Cartopy 的人交谈......因为我使用 Cartopy 制作地图,但我不太了解它是如何工作的。

首先,我绘制了一张欧洲地图(广义上,从大西洋到乌拉尔),如图所示。

然后,我有一个单独的文件,比如dft0,为每个欧洲国家指明某种现象出现的时间(Time0),以相对于任意日期D 的天数计算,以及从minmax 排序;以第一行为例:

    Country     Time0
20  Italy    -16.063702
10  Denmark   -2.798684
39  Sweden    -2.711578
15  Germany    3.259436

所以,所谓的现象首先出现在意大利,16.1 daysbefore我的约会D,然后在丹麦,2.8 daysbeforeD ,然后在瑞典,2.7 daysbeforeD,然后在德国,3.3 daysafterD等,去白俄罗斯,在那里出现@ 987654339@ 之后 D52.1.

文件dft0中有44个这样的值(从负到正),从-16.152.1

我的问题是:知道我做了一个合适的程序来绘制欧洲地图,我应该在程序中添加什么样的代码才能根据变量为国家着色Time0,例如从red(意大利)到violet(白俄罗斯),按照可见光谱的颜色,其中red = 800 nmviolet = 400 nm?

更准确地说,如果Time0 = x,我想用对应于(大约)y = -5.9 x + 705.6 nm 的颜色为相应的国家着色。

为了更容易理解,我插入了一个图表,显示如何计算颜色y(在nm);这是一个基本的线性插值。

我真的不知道它是否可以完成,因为它似乎很复杂(可能是不必要的复杂)。所以,我对任何其他想法持开放态度。目的是区分我在此文件dft0 中的44 国家/地区,使用有序的调色板,显示定期减少(或定期增长......)

感谢您的关心。

添加:我使用的Cartopy程序:

import matplotlib.pyplot as plt
import cartopy
import cartopy.io.shapereader as shpreader

plt.figure(figsize=(4, 4))

central_lon, central_lat = 0, 45
extent = [-10, 45, 35, 70]

ax = plt.axes(projection=cartopy.crs.Orthographic(central_lon, central_lat))
ax.set_extent(extent)
ax.gridlines()
ax.add_feature(cartopy.feature.BORDERS, linestyle=':', alpha=1)
ax.add_feature(cartopy.feature.OCEAN,facecolor=("lightblue"))
ax.add_feature(cartopy.feature.LAND)
ax.coastlines(resolution='10m')

plt.show()

【问题讨论】:

    标签: python cartopy


    【解决方案1】:

    此解决方案基于您发布的代码示例,并大量使用 this answer

    import matplotlib.pyplot as plt
    import matplotlib
    import cartopy
    from cartopy.io import shapereader
    import cartopy.crs as ccrs
    import geopandas
    import numpy as np
    
    # get natural earth data (http://www.naturalearthdata.com/)
    
    # get country borders
    resolution = '10m'
    category = 'cultural'
    name = 'admin_0_countries'
    shpfilename = shapereader.natural_earth(resolution, category, name)
    
    # read the shapefile using geopandas
    df = geopandas.read_file(shpfilename)
    
    
    # Set up the canvas
    fig = plt.figure(figsize=(8, 8))
    central_lon, central_lat = 0, 45
    extent = [-10, 45, 35, 70]
    ax = plt.axes(projection=cartopy.crs.Orthographic(central_lon, central_lat))
    ax.set_extent(extent)
    ax.gridlines()
    
    # Add natural earth features and borders
    ax.add_feature(cartopy.feature.BORDERS, linestyle=':', alpha=1)
    ax.add_feature(cartopy.feature.OCEAN, facecolor=("lightblue"))
    ax.add_feature(cartopy.feature.LAND)
    ax.coastlines(resolution='10m')
    
    # Insert your lists of countries and lag times here
    countries = ['Germany', 'France', 'Italy', 'Spain', 'Ukraine']
    lags = [-20,-5, 15, 0, 2]
    
    # Normalise the lag times to between 0 and 1 to extract the colour
    lags_norm = (lags-np.nanmin(lags))/(np.nanmax(lags) - np.nanmin(lags))
    
    # Choose your colourmap here
    cmap = matplotlib.cm.get_cmap('viridis')
    
    
    for country, lag_norm in zip(countries, lags_norm):
        # read the borders of the country in this loop
        poly = df.loc[df['ADMIN'] == country]['geometry'].values[0]
        # get the color for this country
        rgba = cmap(lag_norm)
        # plot the country on a map
        ax.add_geometries(poly, crs=ccrs.PlateCarree(), facecolor=rgba, edgecolor='none', zorder=1)
    
    # Add a scatter plot of the original data so the colorbar has the correct numbers. Hacky but it works
    dummy_scat = ax.scatter(lags, lags, c=lags, cmap=cmap, zorder=0)
    fig.colorbar(mappable=dummy_scat, label='Time lag of phenomenon', orientation='horizontal', shrink=0.8)
    

    结果:

    Map of Europe with France, Germany and Italy coloured

    至于可见光谱的着色,除非你有充分的理由这样做,否则我强烈劝阻你不要这样做。相反,我使用了 matplotlib 的inbuilt perceptually uniform colourmaps 之一。如果 viridis 不适合您的需求,您可以使用许多其他颜色图。这些感知上一致的颜色图更可取,因为它们不会扭曲您的数据。如需更多信息,请查看this pagethis more in depth discussion,或搜索关于感知统一颜色图的信息。观看您作品的人(尤其是那些有色觉缺陷的人)会感谢您。

    【讨论】:

    • 免责声明我不是 Fabio Crameri。我只是最了解他在感知统一颜色图方面的工作。其他作者可用!
    • 天哪! @bystander,这是对我问题的绝妙答案!我意识到我在 Python 中还有很多东西要学……我什至不知道存在诸如 viridis 之类的颜色图引用。我一定会听从您的建议,不要使用可见光谱,并将我的工作基于您的编码。非常感谢您的帮助!
    猜你喜欢
    • 2014-10-15
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    相关资源
    最近更新 更多