我正在和那些非常了解Cartopy的人交谈……因为我使用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 days
之前的D
之前,然后出现在丹麦2.8 days
之前的之前/ strong> D
,然后在瑞典2.7 days
之前 D
,然后在德国,3.3 days
之后 { {1}}等,然后去了白俄罗斯,它出现在{strong}之后D
52.1 days
。
D
。
文件52.1
中有44个这样的值(从负到正),从dft0
到-16.1
。
我的问题是:知道我做了一个合适的程序来绘制欧洲地图,因此我应该在程序中添加哪种代码才能根据变量为国家/地区着色52.1
,例如从Time0
(对于意大利)到red
(对于白俄罗斯),遵循可见光谱的颜色,其中violet
和red = 800 nm
?
更准确地说,如果是violet = 400 nm
,我想用(大约)Time0 = x
对应的颜色为相应的国家/地区着色。
为了更容易理解,我插入了一个图,显示了如何计算颜色y = -5.9 x + 705.6 nm
(在y
中);这是基本的线性插值。
我真的不知道是否可以完成,因为它似乎很复杂(可能不必要地复杂)。因此,我愿意接受任何其他想法。目的是区分文件nm
中我所拥有的44
个国家,并按顺序排列的颜色显示出有规律的减少(或有规律的增长...)
感谢您的关注。
已添加:我使用的 Cartopy 程序:
dft0
答案 0 :(得分: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 page或this more in depth discussion,或搜索有关可感知的统一颜色图的信息。您作品的观看者(尤其是有色觉障碍的观看者)会感谢您。