Change range withouth scaling in matplot

柔情痞子 提交于 2019-12-12 03:27:45

问题


I have a question, I am making a program that displays a zoomed area of Peru but the axis shown are in the range of the image (e.g. 7000x7500) but i want to be in UTM range (e.g. x-axis between 500000-600000 and y-axis 9500000-9700000) I have tried using plt.set_xlim and plt.sety_lim but no success, I think I have to use plt.autoscale(False) but it also didn't work or I used it wrong.

I create the figure and axes out of the main program

f = plt.figure(figsize=(5,5))
axe = f.add_axes([0, 0, 1, 1])

this is the function I call everytime I want to plot

def plotear(self, mapa):

    axe.clear()
    axe.imshow(mapa, cmap='gray', interpolation='nearest')
    axe.set_xlim(0,10000) #This is just for testing
    axe.set_ylim(0,10000) #This is just for testing
    plt.autoscale(False)
    self.canvas.draw()


Edit: @ImportanceOfBeingErnest's answer worked as expected! Now I am having another problem, in the canvas I am showing the image, the x-axis and y-axis doesnt visualize correctly, here is an image example how could I fix it? thanks.

回答1:


From the imshow documentation you'd find that there is an argument extent which can be used to scale the image.

extent : scalars (left, right, bottom, top), optional, default: None
The location, in data-coordinates, of the lower-left and upper-right corners. If None, the image is positioned such that the pixel centers fall on zero-based (row, column) indices.

In this case you'd use it like

ax.imshow(mapa, extent=[5e5, 6e5, 9.5e6, 9.7e6])


Answer to the edited question:
In the case of the image being too large, this is probably caused by you setting axe = f.add_axes([0, 0, 1, 1]). You should rather use ax = fig.add_subplot(111) and if the margins are not as you want then, setting plt.subplots_adjust( ... ) with the respective spacings.

来源:https://stackoverflow.com/questions/43283643/change-range-withouth-scaling-in-matplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!