如何使用底图向国家/地区添加颜色

时间:2014-05-11 10:36:26

标签: python-2.7 colors matplotlib-basemap

我在使用python中的底图向我的世界地图添加颜色时遇到了麻烦。 我有一个国家名称和频率列表 例如:

Usa 304
France 120
Italy 23
Spain 152

我创建了一个有三种不同颜色(红色,黄色,橙色)的颜色条。黄色表示300-400的面积,所以当美国的频率为304时,我希望美国为黄色。

from mpl_toolkits.basemap import Basemap 
import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib import colors

m = Basemap(projection='mill',llcrnrlat=-90,urcrnrlat=90,\
    llcrnrlon=-160,urcrnrlon=160,resolution='c')

m.drawcoastlines()
m.drawcountries(color='black')
m.drawmapboundary(fill_color='aqua')
m.fillcontinents(color='white',lake_color='aqua')

#Colorbar

zvals = np.random.rand(100, 100) * 10

# make a color map of fixed colors
cmap = colors.ListedColormap(['white','yellow','orange','red','purple'])
bounds=[0,20,30,50,200, 400]
norm = colors.BoundaryNorm(bounds, cmap.N)

img = plt.imshow(zvals, interpolation='nearest', origin='lower',
                cmap=cmap, norm=norm)


plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[0 ,20, 30, 50, 200, 400])


plt.title('Imdb Top250 feq countries')
plt.show()

所以我的问题是,如何在频率之后组合添加颜色到国家/地区。 是经纬度的吗?

1 个答案:

答案 0 :(得分:0)

您需要的是实际国家/地区边界的shapefile,然后您可以通过调用readshapefile方法将其添加到Basemap实例:

m.readshapefile("/path/to/your/shapefile", "countries")

如果你没有这个,你可以从例如GADM

接下来,您需要一种方法将shapefile中的信息与您拥有的频率上的数据集相关联。在上面的示例中执行的对m.readshapefile()的调用将返回两个对象,然后可以从Basemap实例调用:m.countries将是国家边界的实际坐标列表,而m.countries_info将包含一个字典列表,每个国家/地区都有一个字典,其中包含该国家/地区的名称。

因此,您必须弄清楚m.countries_info的哪一部分可用于将m.countries中的形状映射到基础数据集。这将如何完全取决于数据集中的确切信息和来自shapefile的*_info词典中的键。