是否可以在python中使用底图来创建不透明(透视)地图?

时间:2016-09-30 05:27:15

标签: python matplotlib maps matplotlib-basemap

是否可以使用Python底图(matplotlib)绘制填充颜色透过的地图(半透明)?我正在郊区构建澳大利亚的热图,我最终打算用谷歌地图图像覆盖它。因此,我希望填充颜色是半透明的。我的代码很长(250行),所以最好不要发布所有代码。将不胜感激任何建议。最有可能的是,它涉及将colormap的alpha值设置为介于0和1之间的值。我正在使用Python的matplotlib底图(如果有帮助的话)。我还使用了本网站的代码(它可以帮助您理解我在做什么):http://brandonrose.org/pythonmap

# Convenience functions for working with color ramps and bars
def colorbar_index(ncolors, cmap, labels=None, **kwargs):
"""
This is a convenience function to stop you making off-by-one errors
Takes a standard colour ramp, and discretizes it,
then draws a colour bar with correctly aligned labels
"""
cmap = cmap_discretize(cmap, ncolors)
mappable = cm.ScalarMappable(cmap=cmap)
mappable.set_array([])
mappable.set_clim(-0.5, ncolors + 0.5)
colorbar = plt.colorbar(mappable, **kwargs)
colorbar.set_ticks(np.linspace(0, ncolors, ncolors))
colorbar.set_ticklabels(range(ncolors))
#colorbar.set_alpha(0.5)
if labels:
    colorbar.set_ticklabels(labels)
return colorbar


def cmap_discretize(cmap, N):
"""
Return a discrete colormap from the continuous colormap cmap.

    cmap: colormap instance, eg. cm.jet.
    N: number of colors.

Example
    x = resize(arange(100), (5,100))
    djet = cmap_discretize(cm.jet, 5)
    imshow(x, cmap=djet)

"""
if type(cmap) == str:
    cmap = get_cmap(cmap)
colors_i = np.concatenate((np.linspace(0, 1., N), (0., 0., 0., 0.)))
colors_rgba = cmap(colors_i)
indices = np.linspace(0, 1., N + 1)
cdict = {}
for ki, key in enumerate(('red', 'green', 'blue')):
    cdict[key] = [(indices[i], colors_rgba[i - 1, ki], colors_rgba[i, ki]) for i in xrange(N + 1)]
return matplotlib.colors.LinearSegmentedColormap(cmap.name + "_%d" % N, cdict, 1024)

0 个答案:

没有答案