简体   繁体   English

matplotlib:如何获得显示颜色?

[英]matplotlib: How to get imshow colors?

I've plotted an image using 我已经使用绘制了图像

import matplotlib.pyplot as plt
plt.imshow(image)

image is a NxM numpy array with only 5 different values. image是一个只有5个不同值的NxM numpy数组。 How can I get a list of the RGB values that these 5 values have been mapped to in the image shown with imshow? 如何获得在imshow显示的图像中这5个值已映射到的RGB值的列表?

if you don't know in advance what your values in your image are going to be, you can use np.unique to find all unique values, then use norm and cmap properties of the AxesImage returned by imshow 如果您事先不知道图像中的值是什么,可以使用np.unique查找所有唯一值,然后使用AxesImage返回的imshow normcmap属性

For example: 例如:

import numpy as np
import matplotlib.pyplot as plt

im = plt.imshow(image)
colours = im.cmap(im.norm(np.unique(image))

Well, I found a way do doing it based on this other question. 好吧,我发现基于一种方法做做这个的其他问题。

import matplotlib.pyplot as plt
art = plt.imshow(image)
colours = [art.cmap(art.norm(c)) for c in range(1,6)]

colours contains the colours I needed. 颜色包含我需要的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM