Разгледайте този урок. Бих препоръчал да го прочетете изцяло; част от това да станеш умел програмист е да четеш кода на други хора и да разбираш какво са направили. Но ако се интересувате само от вашия отговор, превъртете надолу, докато видите подзаглавието „Добавяне на цвят“ или щракнете върху връзката, която е в горната част на страницата. Надявам се това да помогне!
Що се отнася до оцветяването на вашите стойности, имам някакво решение за вашия проблем, което намерих тук. Модифицирах rgb функцията, намерена в отговора, за да върна шестнадесетични цветове, HTML формат. След това ще трябва да създадете своя собствена цветна лента въз основа на вашите стойности, които са картографирани към тези цветове. Ще трябва също да използвате нещо като 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' )
person
K. Shores
schedule
18.10.2015