通过使用BoundaryNorm作为散点图的标准化程序,您可以非常容易地创建自定义离散色条。这个古怪的位(在我的方法中)使0显示为灰色。
对于图像我经常使用cmap.set_bad()并将我的数据转换为numpy蒙版数组。这会让0灰变得容易得多,但是我不能让它与分散或自定义的cmap一起工作。
作为一种替代方案,您可以从头开始创建自己的cmap,或者读出现有的cmap并覆盖一些特定的条目。
# setup the plot
fig, ax = plt.subplots(1,1, figsize=(6,6))
# define the data
x = np.random.rand(20)
y = np.random.rand(20)
tag = np.random.randint(0,20,20)
tag[10:12] = 0 # make sure there are some 0 values to showup as grey
# define the colormap
cmap = plt.cm.jet
# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# force the first color entry to be grey
cmaplist[0] = (.5,.5,.5,1.0)
# create the new map
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)
# define the bins and normalize
bounds = np.linspace(0,20,21)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
# make the scatter
scat = ax.scatter(x,y,c=tag,s=np.random.randint(100,500,20),cmap=cmap, norm=norm)
# create a second axes for the colorbar
ax2 = fig.add_axes([0.95, 0.1, 0.03, 0.8])
cb = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm, spacing='proportional', ticks=bounds, boundaries=bounds, format='%1i')
ax.set_title('Well defined discrete colors')
ax2.set_ylabel('Very custom cbar [-]', size=12)
我个人认为有20点不同的颜色它有点难以阅读的具体价值,但多数民众赞成由你,当然。
确实[此](http://matplotlib.org/examples/api/colorbar_only.html)或[此] (http://www.scipy.org/Cookbook/Matplotlib/ColormapTransformations)的帮助? – 2013-02-08 16:40:35
感谢您的链接,但第二个例子是我的意思是极其复杂的手段来执行一个(看似)琐碎的任务 - 第一个链接是有用的 – bph 2013-02-08 17:13:16