Prevent anti-aliasing for imshow in matplotlib

Solution 1:

There is an interpolation option for imshow which controls how and if interpolation will be applied to the rendering of the matrix. If you try

imshow(array, interpolation="nearest") 

you might get something more like you want. As an example

A=10*np.eye(10) + np.random.rand(100).reshape(10,10)
imshow(A)

imshow(A)

compared with

A=10*np.eye(10) + np.random.rand(100).reshape(10,10)
imshow(A, interpolation="nearest")

enter image description here

Solution 2:

you can also try the function

matshow 

which name indicated that it does exactly what you asked - represent matrices. It is quite handy when you do not need to customise the figure too much.

BTW, one of the best resources for matplotlib is their Gallery