Matplotlib 显示唯一值的唯一颜色

Matplotlib 显示唯一值的唯一颜色,matplotlib,matplotlib-basemap,Matplotlib,Matplotlib Basemap,我有一个2d numpy数组,通过读取图像获得。数组的唯一值为0、1和2。我想打印图像,分别显示值为0、1和2的独特颜色红色、绿色和蓝色 plt.imshow(data, cmap=colors.ListedColormap(['red']) 你会怎么做?n种颜色需要n+1值,例如0到1将被指定为红色,2到3将被指定为蓝色,它们是边界间隔n种颜色需要n+1值,例如0到1将被指定为红色,2到3将被指定为蓝色,它们是边界间隔n种颜色需要n+1值,例如,0到1将被指定为红色,2到3将被指定为蓝色,它

我有一个2d numpy数组,通过读取图像获得。数组的唯一值为0、1和2。我想打印图像,分别显示值为0、1和2的独特颜色红色、绿色和蓝色

plt.imshow(data, cmap=colors.ListedColormap(['red'])

你会怎么做?

n种颜色需要n+1值,例如0到1将被指定为红色,2到3将被指定为蓝色,它们是边界间隔n种颜色需要n+1值,例如0到1将被指定为红色,2到3将被指定为蓝色,它们是边界间隔n种颜色需要n+1值,例如,0到1将被指定为红色,2到3将被指定为蓝色,它们是边界间隔。n种颜色需要n+1个值,例如,0到1将被指定为红色,2到3将被指定为蓝色,它们是边界间隔
from matplotlib.colors import from_levels_and_colors
cmap, norm = from_levels_and_colors([0,1,2,3],['red','green','blue'])
plt.imshow(data, cmap=cmap, norm=norm)