matplotlib: scatter() on imshow() show points on shrunk coordinates

4.6k views Asked by At

I have a 2D array img_pseudo_df that I plot by imshow() and represents some pseudo fluorescence data where the center of each florescence spot represents a cell:

img = plt.imshow(img_pseudo_df.T, cmap=plt.cm.gray, interpolation='none', extent=(0.0,N_pixels,0.0,N_pixels))

In the above I am fixing a priori the size of the figure to N_pixels and I am transposing the image for my convenience. The resulting figure looks like for example:

enter image description here

On top of this figure, I want to overlay a scatter() plot where each point represents a cell. I am passing to scatter x and y coordinates in pixel values (each within the interval [0,N_pixels]):

plt.scatter(x,y)

However for some reason these coordinates seem shrunk with respect to the coordinates of the fluorescence in the figure:

enter image description here

I cannot figure out what I am doing wrong and how to get matching of imshow coordinates with those of my points. This is apparently a common problem with imshow() but I could not find an answer that works for me so far.

In particular, x and y contain the index values for the row columns of img_pseudo_df array. This array is created originally by

img_pseudo_df = zeros((N_pixels,N_pixels))

Then pixels (x,y) in img_pseudo_df are assigned a certain signal value and convolved by a 2D gaussian filter (to create the pseudo fluorescence appeareance):

for cell,pixel in enumerate(zip(x,y)):
    img_pseudo_df[pixel] = signals[cell] # signals contains N cells values and there are N cells (x,y) pairs
img_pseudo_df = convolve2d(space, gaussian_kern(20), mode='valid') 

As the filter is centered around zero and is built by:

def gauss_kern(size, sizey=None):
""" Returns a normalized 2D gauss kernel array for convolutions """
size = int(size)
if not sizey:
    sizey = size
else:
    sizey = int(sizey)
x, y = mgrid[-size:size+1, -sizey:sizey+1]
g = exp(-(x**2/float(size)+y**2/float(sizey)))
return g / g.sum()

I would expect the fluorescence point to be center at the cell locations (x,y).

Thanks in advance for your help.

Cheers,

M

1

There are 1 answers

0
maurizio On BEST ANSWER

Ok, there was indeed an error in the convolution. The right option was mode='same' so as to make the resulting figure match the originally empty `img_pseudo_df'.

Cheers,

M