簡體   English   中英

向包含多個shapefile的底圖圖中添加顏色條圖例

[英]Adding Colorbar legend to Basemap plot containing multiple shapefiles

我正在使用Basemap將多個shapefile繪制到一個畫布上,並使用之前計算出的索引進行着色。 現在,我嘗試在圖像旁邊添加圖例(顏色欄),但無法正常使用。 到目前為止,這是我的代碼:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM