Skip to main content
 首页 » 编程设计

python之在 matplotlib-basemap 中绘制颜色编码标记

2024年08月26日31haluo1

我有以下变量,我想在 map 上绘制: Lons,经度列表,Lats,纬度列表和 Vals,每个位置的值列表。

为了绘制位置,我做了一个简单的

x,y=Mymap(Lons,Lats) 
Mymap.scatter(x,y,marker='o',color='red') 

问:如何最好地将我的 Vals 转换为颜色列表(热图)以传递到 scatter,以便每个x,y 对获取匹配颜色的值?

basemap 文档相当缺乏,而且没有一个示例符合我的目的。

我可能会遍历我的整个 Lats、Lons 和 Vals 但考虑到 basemap 有多慢,我宁愿避免这种情况。我已经需要绘制大约 800 张 map ,将其增加到大约 100 万张可能需要数年时间。

请您参考如下方法:

basemap scatter documentation简单地指代 pyplot.scatter documentation ,您可以在其中找到参数 c(强调我的):

c : color or sequence of color, optional, default

c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however.

因此,如果您只是将 vals 传递给 c,matplotlib 应该将值转换为标准颜色图或 chosen colormap 上的颜色。 .要显示哪种颜色映射到什么值,您可以使用 colorbar .

示例代码:

import matplotlib.pyplot as plt 
 
lons = range(50) 
lats = range(25, 75) 
vals = range(50,100) 
 
plt.scatter(lons, lats, c=vals, cmap='afmhot') 
plt.colorbar() 
plt.show()