【问题标题】:Matplotlib imshow offset to match axis?Matplotlib imshow 偏移以匹配轴?
【发布时间】:2012-09-01 16:41:45
【问题描述】:

我正在使用 matplotlib.pyplot.scatter 绘制一堆 UTM 坐标。我还有一张背景空气照片,我知道它与图形的范围完全匹配。当我绘制数据并设置轴时,我可以正确显示散点图。如果我使用 imshow 绘制空中照片,它将使用像素数作为轴位置。我需要将图像(numpy 数组)移动到正确的 UTM 位置。有任何想法吗?我对 matplotlib 和 numpy 还很陌生。

例如:我知道图像的左上角(imshow 坐标:0,0)有 UTM 坐标(269658.4、538318.2)。我如何告诉 imshow 相同的东西?

我还应该说我研究了 Basemap,但它似乎还没有完全支持 UTM。我的学习区很小。

【问题讨论】:

    标签: python numpy matplotlib gis coordinate-systems


    【解决方案1】:

    imshow 需要使用extent 关键字参数。

    举个简单的例子:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Random points between 50000 and 51000
    x, y = 1000 * np.random.random((2, 10)) + 50000
    
    # A 10x10 "image"...
    image = np.arange(100).reshape((10,10))
    
    # In a lot of cases, image data will be "flipped" vertically, so you may need 
    # use the `origin` kwarg, as well (or just swap the ymin and ymax ordering).
    plt.imshow(image, extent=[x.min(), x.max(), y.min(), y.max()])
    plt.plot(x, y, 'ro')
    
    plt.show()
    

    【讨论】:

    • 谢谢!这正是我想要的!我不确定为什么在 API 中找不到该关键字。
    • 很容易迷失在各种 matplotlib 函数的所有关键字参数中。不过,我保证,它在文档中! :) 无论如何,很高兴能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多