Matplotlib的imshow偏移量以匹配轴? [英] Matplotlib imshow offset to match axis?
问题描述
我正在使用matplotlib.pyplot.scatter绘制一堆UTM坐标.我还有一张背景空气照片,据我所知与背景图的范围完全吻合.当我绘制数据并设置轴时,可以正确显示散点图.如果我使用imshow绘制空中照片,它将使用像素数作为轴位置.我需要将图像(numpy数组)移动到正确的UTM位置.有任何想法吗?我对matplotlib和numpy相当陌生.
I'm plotting a bunch of UTM coordinates using a matplotlib.pyplot.scatter. I also have a background air photo that I know matches the extent of the figure exactly. When I plot my data and set the axis I can display the scatter correctly. If I plot the air photo using imshow it uses the pixel number as the axis location. I need to shift the image (numpy array) to it's correct UTM position. Any ideas? I'm fairly new to matplotlib and numpy.
例如:我知道图像的左上角(imshow坐标:0,0)具有UTM坐标(269658.4、538318.2).我该如何告诉imshow同一件事?
For example: I know that the top left corner of the image (imshow coordinate: 0,0) has the UTM coordinate (269658.4, 538318.2). How do I tell imshow the same thing?
我还应该说我调查了底图,但它似乎尚未完全支持UTM.我的学习区域很小.
I should also say that I investigated Basemap but it doesn't appear to fully support UTM yet. My study area is very small.
推荐答案
您需要使用extent
关键字参数来显示.
You need to use the extent
keyword argument to imshow.
作为一个简单的例子:
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()
这篇关于Matplotlib的imshow偏移量以匹配轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!