Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'turn off' blurry effect of imshow() in matplotlib?

I want to make a color plot of probabilities however imshow generates blurry values for points which have zero probability. How can I get rid of this blurry periphery around real grid points?

Example:

import numpy as np import matplotlib.pyplot as plt  a=np.asarray([[  0.00000000e+00 , 1.05824446e-01 ,  2.05086136e-04,   0.00000000e+00], [  1.05824446e-01 ,  3.15012305e-01  , 1.31255127e-01  , 1.05209188e-01],  [  2.05086136e-04  , 1.31255127e-01 ,  0.00000000e+00 ,  0.00000000e+00],  [  0.00000000e+00   ,1.05209188e-01  , 0.00000000e+00  , 0.00000000e+00]]) im=plt.imshow(a,extent=[0,4,0,4],origin='lower',alpha=1,aspect='auto') plt.show() 

enter image description here

like image 585
Cupitor Avatar asked Nov 15 '13 21:11

Cupitor


People also ask

How do you normalize Imshow?

Just specify vmin=0, vmax=1 . By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).

What is the difference between PLT Imshow and PLT show?

plt. imshow() draws an image on the current figure (creating a figure if there isn't a current figure). Calling plt. show() before you've drawn anything doesn't make any sense.

What is PLT Imshow ()?

The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .


1 Answers

By default (which is changed mpl 2.0), imshow interpolates the data (as you would want to do for an image). All you need to do is tell it to not interpolate:

im = plt.imshow(..., interpolation='none') 

'nearest' will also work for what you want. See smoothing between pixels of imagesc\imshow in matlab like the matplotlib imshow for examples of all of the kinds of interpolation.

doc

like image 129
tacaswell Avatar answered Sep 22 '22 02:09

tacaswell