将颜色条图例添加到包含多个形状文件的底图图
Adding Colorbar legend to Basemap plot containing multiple shapefiles
我正在使用 Basemap 将多个 shapefile 绘制到一个 canvas,使用我之前计算的索引进行着色。现在我想在我的图像旁边添加一个图例(颜色条),但我无法让它工作。到目前为止,这是我的代码:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# Floats between 0 and 20
indexList = [*Previously calculated indices*]
minIndex = min(indexList)
maxIndex = max(indexList)
# Figure out colors for each index
norm = matplotlib.colors.Normalize(vmin=minIndex, vmax=maxIndex, clip=True)
mapper=cm.ScalarMappable(norm=norm, cmap='RdYlBu')
for key in indexList
gridIndexDict[key] = mapper.to_rgba(indexList[key]) #
# Map to draw on, minX etc. won't interest here
m = Basemap(projection='merc', lat_0=(minX+maxX)/2, lon_0 = (minY+maxY)/2,
resolution='h', area_thresh=0.1,llcrnrlon=minX, llcrnrlat=minY,
urcrnrlon=maxX, urcrnrlat=maxY)
# Yes, that's 1000 shapefiles
for i in range(0, 40):
for j in range(0, 25):
path = "Some path using i and j"
m.readshapefile(path, "Title", color=gridIndexDict["Key using i and j"])
###
# I think this is the place to add a colorbar legend?
###
plt.show()
基本上我使用这两个循环来读取和绘制具有先前确定颜色的 shapefile。各个 shapefile 都以正确的颜色绘制,但我什至无法显示颜色条。
我花了几个小时试图深入研究文档和示例,但无法使任何工作正常进行。
也许你们中的一些人可以帮我解决这个问题?如果我需要为您提供更多信息,请告诉我。
我代表@tcaswell 代替他写下这个答案:
mapper.set_array(indexList)
plt.colorbar(mapper)
与
一样有效
fig = plt.gcf()
mapper.set_array(indexList)
fig.colorbar(mapper)
我正在使用 Basemap 将多个 shapefile 绘制到一个 canvas,使用我之前计算的索引进行着色。现在我想在我的图像旁边添加一个图例(颜色条),但我无法让它工作。到目前为止,这是我的代码:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# Floats between 0 and 20
indexList = [*Previously calculated indices*]
minIndex = min(indexList)
maxIndex = max(indexList)
# Figure out colors for each index
norm = matplotlib.colors.Normalize(vmin=minIndex, vmax=maxIndex, clip=True)
mapper=cm.ScalarMappable(norm=norm, cmap='RdYlBu')
for key in indexList
gridIndexDict[key] = mapper.to_rgba(indexList[key]) #
# Map to draw on, minX etc. won't interest here
m = Basemap(projection='merc', lat_0=(minX+maxX)/2, lon_0 = (minY+maxY)/2,
resolution='h', area_thresh=0.1,llcrnrlon=minX, llcrnrlat=minY,
urcrnrlon=maxX, urcrnrlat=maxY)
# Yes, that's 1000 shapefiles
for i in range(0, 40):
for j in range(0, 25):
path = "Some path using i and j"
m.readshapefile(path, "Title", color=gridIndexDict["Key using i and j"])
###
# I think this is the place to add a colorbar legend?
###
plt.show()
基本上我使用这两个循环来读取和绘制具有先前确定颜色的 shapefile。各个 shapefile 都以正确的颜色绘制,但我什至无法显示颜色条。
我花了几个小时试图深入研究文档和示例,但无法使任何工作正常进行。 也许你们中的一些人可以帮我解决这个问题?如果我需要为您提供更多信息,请告诉我。
我代表@tcaswell 代替他写下这个答案:
mapper.set_array(indexList)
plt.colorbar(mapper)
与
一样有效fig = plt.gcf()
mapper.set_array(indexList)
fig.colorbar(mapper)