简体   繁体   中英

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. How can I get a list of the RGB values that these 5 values have been mapped to in the image shown with imshow?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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