将alpha添加到现有的matplotlib颜色图 [英] Add alpha to an existing matplotlib colormap

查看:84
本文介绍了将alpha添加到现有的matplotlib颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖几个hexbin图,但是对于内置的色图,只有最后一个可见.我不想从头构造一个颜色图.在不事先知道颜色表的内部结构的情况下,如何将线性alpha添加到颜色表?

I'd like to make an overlay of several hexbin plots, but with builtin colormaps only the last one is visible. I don't want to construct a colormap de novo. How one would add linear alpha to the colormap without knowing the inner structure of the colormap beforehand?

推荐答案

我不太确定这是否符合不知道颜色图的内部结构"的要求,但是类似的方法可能会添加线性alpha到现有的颜色图?

I'm not quite sure if this qualifies within "not knowing the inner structure of the colormap", but perhaps something like this would work to add a linear alpha to an existing colormap?

import numpy as np
import matplotlib.pylab as pl
from matplotlib.colors import ListedColormap

# Random data
data1 = np.random.random((4,4))

# Choose colormap
cmap = pl.cm.RdBu

# Get the colormap colors
my_cmap = cmap(np.arange(cmap.N))

# Set alpha
my_cmap[:,-1] = np.linspace(0, 1, cmap.N)

# Create new colormap
my_cmap = ListedColormap(my_cmap)

pl.figure()
pl.subplot(121)
pl.pcolormesh(data1, cmap=pl.cm.RdBu)
pl.colorbar()

pl.subplot(122)
pl.pcolormesh(data1, cmap=my_cmap)
pl.colorbar()

这篇关于将alpha添加到现有的matplotlib颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆