Matplotlib imshow:指定刻度时光标坐标失败
Matplotlib imshow: cursor coordinates fail when ticks are specified
当您使用 imshow 在 window 的右下角绘制图像时,会显示光标的坐标。但是,当我尝试设置轴的刻度和刻度标签时,window 停止显示坐标,仅显示 "x= y="
最小示例:
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()
这会显示一个随机数据图,并将刻度标签设置为从0到1,但右下角的光标坐标仅显示"x= y="。
有没有办法让坐标以刻度定义的新单位显示?我认为这与设置轴的变换有关,但我无法弄清楚。
调用 "imshow" 时应使用 "extent" 参数:
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()
当您使用 imshow 在 window 的右下角绘制图像时,会显示光标的坐标。但是,当我尝试设置轴的刻度和刻度标签时,window 停止显示坐标,仅显示 "x= y="
最小示例:
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()
这会显示一个随机数据图,并将刻度标签设置为从0到1,但右下角的光标坐标仅显示"x= y="。
有没有办法让坐标以刻度定义的新单位显示?我认为这与设置轴的变换有关,但我无法弄清楚。
调用 "imshow" 时应使用 "extent" 参数:
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()