使用matplotlib绘制颜色条,区别颜色和不连续颜色

问题描述:

下图中的颜色条在matlab中绘制。我想知道是否有可能在python/matplotlib中绘制类似的颜色条?我需要颜色栏中的未平滑颜色变化,以便值0-5将表示为可区分的和不连续的 coulors中的。如果可能,请给我一个例子吗?谢谢〜使用matplotlib绘制颜色条,区别颜色和不连续颜色

enter image description here

使用ListedColormapBoundaryNorm

import numpy as np 
import matplotlib as mpl 
from matplotlib import pyplot 
data = np.floor(np.random.random((10,10)) * 6) 
cmap = mpl.colors.ListedColormap(['w', 'b', 'g', 'y', '#ff8c00', 'r']) 
norm = mpl.colors.BoundaryNorm([0,1,2,3,4,5,6], cmap.N) 
pyplot.imshow(data, cmap=cmap, norm=norm, interpolation='none') 
pyplot.colorbar() 
pyplot.show() 

使用ListedColormap的一些例子:colorbarsmulticolored lines