Python 根据给定值对国家/地区进行彩色卡通贴图

Python 根据给定值对国家/地区进行彩色卡通贴图,python,cartopy,Python,Cartopy,我在和那些很了解卡托皮的人说话。。。因为我使用Cartopy制作地图,但我不太清楚它是如何工作的 首先,我创建了一张欧洲地图(从广义上讲,从大西洋到乌拉尔),如附图所示 然后,我有一个单独的文件,比如说dft0,为每个欧洲国家指明某种现象出现的时间(Time0),以任意日期D的天数计算,并从min排序到max;作为第一行的示例: Country Time0 20 Italy -16.063702 10 Denmark -2.798684 39 Sweden

我在和那些很了解卡托皮的人说话。。。因为我使用Cartopy制作地图,但我不太清楚它是如何工作的

首先,我创建了一张欧洲地图(从广义上讲,从大西洋到乌拉尔),如附图所示

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

    Country     Time0
20  Italy    -16.063702
10  Denmark   -2.798684
39  Sweden    -2.711578
15  Germany    3.259436
因此,这种所谓的现象首先出现在意大利,在我的约会日期之前16.1天,然后在丹麦,在我的约会日期之前2.8天,然后在瑞典,在我的约会日期之前2.7天,然后在德国,
3.3天
之后
D
等,前往白俄罗斯,在那里52.1天之后
D
52.1

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

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

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

为了更容易理解,我插入了一个显示如何计算颜色
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()

此解决方案基于您发布的代码示例,并充分利用了

结果:


至于可见光谱的着色,我强烈劝阻你不要这样做,除非你有很好的理由这样做。相反,我使用了matplotlib的一个。如果viridis不适合您的需要,您可以使用许多其他颜色贴图。这些感知上一致的颜色贴图更可取,因为它们不会扭曲您的数据。有关更多信息,请查看或搜索感知均匀彩色地图的信息。观看您作品的观众(尤其是那些有色觉缺陷的观众)会感谢您。

免责声明我不是法比奥·克拉梅里。我最清楚的是他在感知一致的彩色地图上的工作。其他作者也可以!天哪@旁观者,这是对我问题的绝妙回答!我意识到我还有很多东西要学Python。。。我甚至不知道像viridis这样的彩色地图参考存在。我一定会听从你的建议,不要使用可见光谱,我的工作将以你的编码为基础。非常感谢你的帮助!
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)