根据给定值的彩色地图国家

问题描述 投票:0回答:1

我正在和那些了解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 days 之前我的约会D,然后出现在丹麦2.8 days 之前 D,然后出现在瑞典2.7 days ] 之前 D,然后在德国,3.3 days 之后 D,依此类推,去了白俄罗斯,出现了52.1 days 之后 D52.1

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

我的问题是:知道我做了一个合适的程序来绘制欧洲地图,为了根据变量Time0为国家/地区着色,我应该向程序中添加哪种代码,例如,从red(对于意大利)到violet(对于白俄罗斯),遵循可见光谱的颜色,其中red = 800 nmviolet = 400 nm

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

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

我真的不知道是否可以完成,因为它似乎很复杂(可能不必要地复杂)。因此,我愿意接受任何其他想法。目的是区分我在此文件44中拥有的dft0个国家,并按顺序排列的颜色显示出有规律的减少(或有规律的增长...)

感谢您的关注。

enter image description hereenter image description here

已添加:我使用的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个回答
0
投票

此解决方案基于您发布的代码示例,并在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,或搜索有关感知均匀的颜色表的信息。您作品的观众(尤其是有色觉障碍的观众)会谢谢您。

© www.soinside.com 2019 - 2024. All rights reserved.