1
我借鉴cartopy
国家,并从彩色图添加颜色如下:Matplotlib:添加颜色条到cartopy图像
cmap = plt.get_cmap('viridis')
norm = matplotlib.colors.Normalize(vmin=dfSingle.min(), vmax=dfSingle.max())
dfSingle[:] = norm(dfSingle).data
kw = dict(resolution='110m', category='cultural',
name='admin_0_countries')
states_shp = shapereader.natural_earth(**kw)
shp = shapereader.Reader(states_shp)
ax = plt.axes(projection=ccrs.PlateCarree())
for record, state in zip(shp.records(), shp.geometries()):
try:
colorNormalized = dfSingle[int(record.attributes['iso_n3'])]
ax.add_geometries([state], ccrs.PlateCarree(),
facecolor=cmap(colorNormalized), edgecolor='black')
except KeyError:
ax.add_geometries([state], ccrs.PlateCarree(),
facecolor='grey', edgecolor='black')
,我的数据是这样的:
In [246]: dfSingle.head()
Out[246]:
V2
12 0.179909
31 0.332297
32 0.642179
36 0.815429
48 0.215383
现在我想添加对应于规范化值的颜色条和cmap
。不过,我不断收到错误:
ax.get_figure().colorbar()
AttributeError: 'GeoAxesSubplot' object has no attribute 'colorbar'
cmap.colorbar
AttributeError: 'ListedColormap' object has no attribute 'colorbar'
foo = matplotlib.cm.ScalarMappable(cmap)
ax.get_figure().colorbar(foo)
TypeError: You must first set_array for mappable
foo.set_array(dfSingle.values)
ax.get_figure().colorbar(foo)
AttributeError: 'ListedColormap' object has no attribute 'autoscale_None'
这是我的阴谋的外观现在:
我如何可以添加颜色条?
这不就是一个matplotlib问题吗? http://stackoverflow.com/a/11558629/741316 – pelson
@pelson第一部分是一个'cartopy'问题:当axis是一个'GeoAxesSubplot'时,'ax.get_figure()。colorbar()'不起作用。 cartopy。 'ax.colorbar()'也没有。这将是伟大的,如果它的工作和[颜色轴轴得到正确的高度](http://stackoverflow.com/questions/30030328/correct-placement-of-colorbar-relative-to-geo-axes-cartopy)。但是,只要不改变,'plt.colorbar(sm,cax = ax)'应该可以工作。 – j08lue