Python 基本映射Matplotlib颜色栏

Python 基本映射Matplotlib颜色栏,python,matplotlib,matplotlib-basemap,Python,Matplotlib,Matplotlib Basemap,我正在使用matplotlib和basemap生成一张美国地图和按人口划分的各州地图。我可以让地图按人口显示和状态着色(使用“热”颜色比例),但在尝试同时显示颜色栏时遇到问题。以下是我如何显示地图和着色状态: m = Basemap(llcrnrlon=-121,llcrnrlat=20,urcrnrlon=-62,urcrnrlat=51,projection='lcc',lat_1=32,lat_2=45,lon_0=-95) shp_info = m.readshapefile('st9

我正在使用matplotlib和basemap生成一张美国地图和按人口划分的各州地图。我可以让地图按人口显示和状态着色(使用“热”颜色比例),但在尝试同时显示颜色栏时遇到问题。以下是我如何显示地图和着色状态:

m = Basemap(llcrnrlon=-121,llcrnrlat=20,urcrnrlon=-62,urcrnrlat=51,projection='lcc',lat_1=32,lat_2=45,lon_0=-95)

shp_info = m.readshapefile('st99_d00','states',drawbounds=True)

colors={}
statenames=[]
cmap = plt.cm.hot
vmin = 0
vmax = 100000000 
for shapedict in m.states_info:
    statename = shapedict['NAME']
    # skip DC and Puerto Rico.
    if statename not in ['District of Columbia','Puerto Rico']:
        stateabbrev = conversiondict2[statename]
        rate = statedict[stateabbrev]
        colors[statename] = cmap(1.-np.sqrt((rate-vmin)/(vmax-vmin)))[:3]
    statenames.append(statename)

ax = plt.gca()

for nshape,seg in enumerate(m.states):
    # skip DC and Puerto Rico.
    if statenames[nshape] not in ['Puerto Rico', 'District of Columbia']:
        color = rgb2hex(colors[statenames[nshape]])
    # Offset Alaska and Hawaii to the lower left corner. 
        if statenames[nshape] == 'Alaska': 
            seg = list(map(lambda (x,y): (0.35*x + 1100000, 0.35*y-1300000), seg))
        elif statenames[nshape] == 'Hawaii':
            seg = list(map(lambda (x,y): (x + 5100000, y-1400000), seg))
        poly = Polygon(seg,facecolor=color,edgecolor="black",zorder=0)
        ax.add_patch(poly)

m.colorbar(cmap, ax=ax)
plt.show()
注:为了简洁起见,我省略了我的州和缩写转换词典。在没有m.colorbar(cmap,ax=ax)行的情况下,这显示得很好,但当我尝试添加colorbar时,出现以下错误:

AttributeError: 'LinearSegmentedColormap' object has no attribute 'autoscale_None'

也许我不完全理解Basemap colorbar函数是如何工作的,但我认为在我的例子中,cmap将是可映射的参数(颜色渐变),ax将是轴限制?任何帮助都将不胜感激。

您退房了吗?我没有。我要试一试。谢谢