1

I'm just testing very basic stuff about image manipulation with numpy ndarray data structure and matplotlib for show the image. I created a 2-dimensional array with np.zeros((n,m)) function where store my pixel, to keep things easy I'm just working in grayscale so I only need one value to represent my pixel. this is my code snippet:

import numpy as np
import matplotlib.pyplot as plt

matrix= np.zeros((4,4))
print(matrix)
plt.matshow(matrix, cmap=plt.cm.gray) #plot matrix 

matrix+=128 #increase each pixel value by 128
print(matrix) #print the content of matrix
plt.matshow(matrix, cmap=plt.cm.gray) #plot matrix 

What I expect from this code is that the first plot will be totally black because each "pixel" has value 0, and there's no problem here. Then I increase the pixel from 0 to 128 so the second plot should be totally grey, but is still black. Is like no operation is executed but matrix effectively has changed(checked from the print function).

So I tried to modify just some pixel "manually":

matrix[0,0]=255
matrix[0,3]=255
matrix[3,0]=255
matrix[3,3]=255

plt.matshow(matrix, cmap=plt.cm.gray) #plot matrix 

and now 4 pixels are white. There are some aspects that I'm totally missing and I think that are related to numpy and some stuff about ndarray and how is managed.

There's anyone can explain me why this happens? thanks

1 Answer 1

3

By default, the colors of the color map are mapped to the minimum and maximum values of the data to be plotted. To override this, use the arguments vmin and vmax:

plt.matshow(matrix, cmap=plt.cm.gray, vmin=0, vmax=255)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.