trvrrp February 2016

Matplotlib imshow: cursor coordinates fail when ticks are specified

When you use imshow to plot an image in the bottom right corner of the window the coordinates of the cursor show up. However when I try to set the ticks and tick labels of the axes the window stops displaying coordinates and only displays "x= y="

Minimal example:

import matplotlib.pyplot as plt
import numpy as np

d = np.random.rand(100, 100)

fig = plt.figure()
ax = plt.gca()
plt.imshow(d)

# If I comment this I get coordinates in the bottom right,
# but after setting the ticks I only get "x= y="
ax.set_xticks([0, 25, 50, 75, 100])
ax.set_xticklabels([0, 0.25, 0.5, 0.75, 1])
ax.set_yticks([0, 25, 50, 75, 100])
ax.set_yticklabels([0, 0.25, 0.5, 0.75, 1])

fig.show()
raw_input()

This displays a random map of data and sets the tick labels to be from 0 to 1, but the cursor coordinates in the bottom right only shows "x= y=".

Is there any way to get the coordinates to show up in the new units defined by the ticks? I think it has something to do with setting the transform for the axes but I can't figure it out.

Answers


Francesc Torradeflot February 2016

I think you should use the "extent" argument when calling "imshow" as follows:

import matplotlib.pyplot as plt
import numpy as np

d = np.random.rand(100, 100)

fig = plt.figure()
ax = plt.gca()
plt.imshow(d, extent=(0,1,1,0))

ax.set_xticks([0, 0.25, 0.50, 0.75, 1])
ax.set_yticks([0, 0.25, 0.50, 0.75, 1])

fig.show()
raw_input()

If you think my answer is useful, please accept or up-vote it. Thanks

Post Status

Asked in February 2016
Viewed 1,981 times
Voted 8
Answered 1 times

Search




Leave an answer