python - 在保留原始顺序的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
功能上做错了什么?
解决方案
推荐阅读
- javascript - Angular找到一个类型的所有服务
- sql - 内部连接中的案例条件
- android - 如何从 Flutter 访问设备应用程序
- python - Python Pandas 数据框 - 组合/合并具有空值的行
- c - Ghidra 使用像“._0_4_”这样的奇怪字段名称来表示 C 反编译中的非默认访问大小
- mysql - 仅选择作者的最后一篇文章
- python - 将字符串日期转换为可用的熊猫格式
- python - 在不使用主分区键的情况下查询 DynamoDB 中的所有数据
- python - 在 Python 中组织类模块代码的更好方法
- excel - 使用 Vlookup 或 sumif 的数据透视表