首页 > 解决方案 > 在保留原始顺序的numpy数组中获取唯一行

问题描述

我正在尝试将此调色板的颜色读入一个 numpy 数组:

在此处输入图像描述

首先,我沿着 1 像素宽的列获取所有颜色:

  im_in = Image.open(inputfile)
  im_in = im_in.convert('RGBA').crop((50, 88, 50 + 1, 88 + 1049))

然后我将图像转换为 numpy 数组并使用该numpy.unique函数获取唯一列。

  scale = np.array(im_in).squeeze()
  a, indx = np.unique(scale, axis=0, return_index=True)
  colors = scale[indx]

现在,问题是没有保留颜色的顺序:

  cm = LinearSegmentedColormap.from_list('asdf', np.array(colors) / 255, 
                                     N=len(colors))
  fig, ax = plt.subplots(figsize=(6, 1))
  fig.subplots_adjust(bottom=0.5)
  norm = mpl.colors.Normalize(vmin=5, vmax=10)
  fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cm),
               cax=ax, orientation='horizontal', label='Some Units')
  fig.show()

我得到了这个洗牌的数组:

在此处输入图像描述

我在unique功能上做错了什么?

标签: pythonuniquenumpy-ndarraypalette

解决方案


推荐阅读