Search code examples
pythonmatplotlibimshow

How to change specific position's color with matplotlib.pyplot.imshow?


I have a numpy array, and I use following codes to draw a simple picture.

import numpy as np
from matplotlib import pyplot as plt
plt.show(image, cmap='gray')

I also have a list containing the several positions of the image, and I'd like to change the colors of these positions on the same picture. For example, I have another list like this:

pos = [(0,1),(3,6)...]

I'd like to change the pixel's color according to this. For other pixels they remain the same. How can I do that?


Solution

  • If you don't mind copying or modifying the image you can acess the pixel value in the array :

    import numpy as np
    
    image = np.eye(10)
    
    pos = [(0,1),(3,6)]
    values_to_set  = [125,255]
    for p, val in zip(pos, values_to_set):
        image[p] = val