【问题标题】:Matplotlib imshow: cursor coordinates fail when ticks are specifiedMatplotlib imshow:指定刻度时光标坐标失败
【发布时间】:2016-02-08 22:11:36
【问题描述】:

当您使用 imshow 在窗口的右下角绘制图像时,会显示光标的坐标。但是,当我尝试设置轴的刻度和刻度标签时,窗口停止显示坐标,只显示“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=”。

有没有办法让坐标以刻度定义的新单位显示?我认为这与设置轴的变换有关,但我无法弄清楚。

【问题讨论】:

    标签: python python-2.7 matplotlib imshow


    【解决方案1】:

    调用“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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2019-08-25
      • 2012-03-12
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      相关资源
      最近更新 更多