Search code examples
pythonimagematplotlibscaleaxis

Changing extent of image and matching plot with that extent rather than pixels


I'm creating a figure in matplotlib with two subplots, the first subplot shows an image with a line plotted over it, the second subplot shows a plot of the pixel values across that line. I have added an extent to the image so that the axes show the scale of the image rather than a distance in pixels, and I would like the x-axis of the second subplot to show the same scaled distance, however I can't figure out how to change that axis since I am pulling the data from the array representing the image.

I think the solution to the problem would be to add in scale factor of my image (1.93 um/pixel), but I'm not sure where to include this factor so that is also doesn't scale the intensity of the pixels.

umPerPx = 1.93
linexPx = 225
liney1Px, liney2Px = 50, 100
linexUm = 225 * umPerPx
liney1Um, liney2Um = 50 * umPerPx, 100 * umPerPx

img = cv2.imread(test.tif, -1)
img_h_px, img_w_px = img.shape
img_h_um, img_w_um = img_h_px * umPerPx, img_w_px * umPerPx
fig = plt.figure(figsize=(12, 4))

ax_img = fig.add_subplot(1, 2, 1)
ax_img.set_title(imgDirList[i][:-4])
ax_img.imshow(img, cmap='gray', extent=[0, img_w_um, img_h_um, 0])
ax_img.plot([linexUm, linexUm], [liney1Um, liney2Um], 'r', linewidth=1)
ax_img.set_ylabel('Distance (microns)')

ax_prof = fig.add_subplot(1, 2, 2)
ax_prof.set_title('Intensity Profile')
ax_prof.plot(img[:, linexPx], 'r')
ax_prof.set_ylim(min(img[liney1Px:liney2Px, linexPx]), max(img[liney1Px:liney2Px, linexPx]))
ax_prof.set_xlim([liney1Px, liney2Px])
ax_prof.set_xlabel('Distance (pixels)')
ax_prof.set_ylabel('Intensity')

plt.show()

Solution

  • It's hard to be sure without the actual data to try out, but it seems to me what you need to scale is the x-value of your second plot. As it stands, you are not supplying an x array, so it is automatically calculated from the y array you provide. But something like this might work better:

    ax_prof.plot(np.arange(len(img[:, linexPx]))*umPerPx, img[:, linexPx], 'r')