Python imshow 当您绘制数据而不是图像时。方面和范围之间的关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18784354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:50:14  来源:igfitidea点击:

imshow when you are plotting data, not images. Realtion between aspect and extent?

pythonmatplotlibplot

提问by AlexNtheitroad

I am plotting a 2D data array with imshow in matplotlib. I have a problem trying to scale the resulting plot. The size of the array is 30x1295 points, but the extent in units are: extent = [-130,130,0,77]If I plot the array without the extent, I get the right plot, but if I use extent, I get this plot with the wrong aspect. It is a pretty beginner question, but there is always a first time: How I can control the aspect and the size of the plot at the same time? Thanks,

我正在用 matplotlib 中的 imshow 绘制一个二维数据数组。我在尝试缩放结果图时遇到问题。数组的大小是 30x1295 点,但范围的单位是: extent = [-130,130,0,77]如果我绘制没有范围的数组,我会得到正确的图,但是如果我使用范围,我会得到这个带有错误方面的图。这是一个非常初学者的问题,但总是有第一次:我如何同时控制情节的方面和大小?谢谢,

Alex

亚历克斯

P.D. The code is, for the right case: imshow(np.log10(psirhoz+1e-5),origin='lower')

PD 代码是,对于正确的情况: imshow(np.log10(psirhoz+1e-5),origin='lower')

and for the wrong one: imshow(np.log10(psirhoz+1e-5),origin='lower', extent =[z_ax.min(),z_ax.max(),rho_ax.min(),rho_ax.max()])

对于错误的: imshow(np.log10(psirhoz+1e-5),origin='lower', extent =[z_ax.min(),z_ax.max(),rho_ax.min(),rho_ax.max()])

I hope this clarify a bit things.

我希望这能澄清一些事情。

采纳答案by Joe Kington

I'm guessing that you're wanting "square" pixels in the final plot?

我猜你想要最终图中的“方形”像素?

For example, if we plot random data similar to yours:

例如,如果我们绘制与您相似的随机数据:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((30, 1295))

fig, ax = plt.subplots()
ax.imshow(data, extent=[-130,130,0,77])
plt.show()

We'll get an image with "stretched" pixels:

我们将得到一个带有“拉伸”像素的图像:

enter image description here

在此处输入图片说明

So, first off, "aspect" in matplotlib refers to the aspect in datacoordinates. This means we have to jump through a couple of hoops to get what you want.

因此,首先,matplotlib 中的“方面”是指数据坐标中的方面。这意味着我们必须跳过几个环节才能得到您想要的东西。

import numpy as np
import matplotlib.pyplot as plt

def main():
    shape = (30, 1295)
    extent = [-130,130,0,77]

    data = np.random.random(shape)

    fig, ax = plt.subplots()
    ax.imshow(data, extent=extent, aspect=calculate_aspect(shape, extent))
    plt.show()

def calculate_aspect(shape, extent):
    dx = (extent[1] - extent[0]) / float(shape[1])
    dy = (extent[3] - extent[2]) / float(shape[0])
    return dx / dy

main()

回答by Keith Hughitt

In this case, pyplot.matshow()might also be useful:

在这种情况下,pyplot.matshow()也可能有用:

from matplotlib import pyplot as plt
import numpy as np
dat = np.array(range(9)).reshape(3,3)
plt.matshow(dat)
plt.show()

result:

结果:

enter image description here

在此处输入图片说明