python - basemap 中的色标

标签 python maps matplotlib-basemap

    map = Basemap(resolution ='c')
    map.drawcoastlines()
    map.drawcountries()
    map.drawmeridians(np.arange(0, 360, 30))
    map.drawparallels(np.arange(-90, 90, 30))
    map.bluemarble()
    lat_list=worksheet.col_values(16)
    long_list=worksheet.col_values(17)
    lat_list.remove('lat')
    long_list.remove('lon')
    for index in range(0,len(lat_list)):
        x, y = map(long_list[index],lat_list[index])
        map.plot(x, y, 'bo', markersize=5)

上面在 map 上绘制了点。我有一个大小与 lat_list 和 long_list 相同的列表。我想让 map 上的点根据该点着色,并在 map 上显示带有相应标签的色标。

最佳答案

看看this tutorial 。我建议您完整地阅读它;成为熟练程序员的一部分是阅读其他人的代码并理解他们做了什么。但是,如果您只对答案感兴趣,请向下滚动直到看到副标题“添加颜色”,或单击页面顶部的链接。希望这有帮助!

由于它与给你的值着色有关,我有一些解决你的问题的方法,我发现here 。我修改了答案中的 rgb 函数以返回十六进制颜色、HTML 格式。那么你需要create your own color bar根据您映射到这些颜色的值。您还需要使用 gridspec 之类的工具来适当调整图形大小。

import matplotlib as mpl
import matplotlib.pyplot as plot
from mpl_toolkits.basemap import Basemap
import matplotlib.gridspec as gridspec

def rgb(mini,maxi,value):
    mini, maxi, value = float(mini), float(maxi), float(value)
    ratio = 2 * (value - mini) / (maxi-mini)
    b = int(max(0,255*(1-ratio)))
    r = int(max(0,255*(ratio -1)))
    g = 255 - b - r
    b = hex(b)
    r = hex(r)
    g = hex(g)
    if len(b) == 3:
        b = b[0:2] + '0' + b[-1]
    if len(r) == 3:
        r = r[0:2] + '0' + r[-1]
    if len(g) == 3:
        g = g[0:2] + '0' + g[-1]
    color = '#'+r[2:]+g[2:]+b[2:]
    return color


#gridspec will ensure that we get good size ratios when we display the figure
gs = gridspec.GridSpec(1,2, width_ratios = [20,1], height_ratios = [10,1])

#generate the default map
fig = plot.figure(figsize=(17,10))
ax1 = plot.subplot(gs[:, :-1])
map1 = Basemap(ax=ax1)
#code to add more things to the map


#coloring values

correspondance = {}
minimum = min(list_of_values)
maximum = max(list_of_values)
for lon,lat,val in zip(list_of_longitude, list_of_latitude,list_of_values):
   #get the color for this value and add it on to the end of our list
   color = rgb(minimum,maximum,value)
   #make a dictionary that has each value corresponding to its color
   #will be used later to make the color bar
   correspondance[val] = color
   map1.plot(lon,lat, marker = 'o', color = color)



ax2 = plot.subplot(gs[:-1, 1])

#making a color bar requires the values to be in an ordered list
#as well as the colors to be in an ordered list corresponding to the values
bounds = sorted(set(list_of_values))    #get a list of unique sorted values
colors = [correspondance[value] for value in bounds]   #a list of colors corresponding to its value in bounds

cmap = mpl.colorbar.Colorbase(colors, 'indexed')

#bounds needs to of size length of colors + 1
bounds += [max(bounds) + .001]

norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

cb = mpl.colorbar.ColorbarBase(ax2, cmap = cmap, norm = norm, \
        boundaries = [float(min(list_of_values)-.1)] + bounds + [float(max(list_of_values)+.1)],\
        ticks = bounds, spacing = 'proportional', orientation = 'vertical'  )

关于python - basemap 中的色标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32514143/

相关文章:

Python、Postgres 和具有空值的整数?

python - 如何在 Airflow 中将参数传递给 PythonOperator

python - 如何使用 matplotlib 删除/省略较小的轮廓线

python-3.x - 我的 matplot 的网格偏离了 -0.5

python - 如何在 matplotlib 的 map 中插入比例尺

python - 从 numpy timedelta64 获取秒数

python - 如何在python列表中的值之间进行组合?

css - 可以通过谷歌地图自定义输入字段吗?

r - 格子 levelplot 或 ggplot2 map R 的自定义图例

java - 访问 Map 中的最后一个条目