martinako
martinako

Reputation: 2764

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?

Upvotes: 0

Views: 3779

Answers (2)

tmdavison
tmdavison

Reputation: 69213

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))

Upvotes: 4

martinako
martinako

Reputation: 2764

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.

Upvotes: 1

Related Questions