Matplotlib: how to make imshow read x,y coordinates from other numpy arrays?

Setting the extent

Assuming you have

real_x=np.array([15,16,17])
real_y=np.array([20,21,22,23])

you would set the image extent as

dx = (real_x[1]-real_x[0])/2.
dy = (real_y[1]-real_y[0])/2.
extent = [real_x[0]-dx, real_x[-1]+dx, real_y[0]-dy, real_y[-1]+dy]
plt.imshow(data, extent=extent)

Changing ticklabels

An alternative would be to just change the ticklabels

real_x=np.array([15,16,17])
real_y=np.array([20,21,22,23])
plt.imshow(data)
plt.gca().set_xticks(range(len(real_x)))
plt.gca().set_yticks(range(len(real_x)))
plt.gca().set_xticklabels(real_x)
plt.gca().set_yticklabels(real_y)

Leave a Comment