Python Matplotlib imshow:数据旋转?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14320159/
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
Matplotlib imshow: Data rotated?
提问by Tengis
I was trying to plot some data with scatter. My code is
我试图用散点图绘制一些数据。我的代码是
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from scipy.interpolate import griddata
data = np.loadtxt('file1.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]
plt.scatter(x, y, c=z, s=100, cmap=mpl.cm.spectral)
cbar=plt.colorbar()
s=18
plt.ylabel(r"$a_v$", size=s)
plt.xlabel(r"$a_{\rm min}$", size=s)
plt.xlim([x.min(),x.max()])
plt.ylim([y.min(),y.max()])
plt.show()
The result is

结果是

Now I came on the idea to try imshow with the some data, soince I didn't like the circles of scatter. So I tried this
现在我想到了用一些数据尝试 imshow 的想法,因为我不喜欢散布的圆圈。所以我试过这个
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
data = np.loadtxt('file1.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]
N = 30j
extent = (min(x), max(x), min(y), max(y))
xs,ys = np.mgrid[extent[0]:extent[1]:N, extent[2]:extent[3]:N]
resampled = griddata(x, y, z, xs, ys)
plt.imshow(resampled.T, extent=extent)
s=18
plt.ylabel(r"$a_v$", size=s)
plt.xlabel(r"$a_{\rm min}$", size=s)
plt.xlim([x.min(),x.max()])
plt.ylim([y.min(),y.max()])
cbar=plt.colorbar()
plt.show()
With this result:

有了这个结果:

My problem is obviosly why imshow()does invert the data? What happens here exactly?
我的问题很明显为什么imshow()会反转数据?这里到底发生了什么?
PS: Here are the data, in case someone would like to play with them
PS:这是数据,以防有人想和他们一起玩
采纳答案by Thorsten Kranz
Look at the keyword arguments of imshow. There is origin. The default is "upper", but you want "lower".
查看 的关键字参数imshow。有origin。默认为“上”,但您想要“下”。
The default makes sense for plotting images, that usually start at the top-left corner. For most matrix-plotting, you'll want origin="lower"
默认值对于绘制图像很有意义,通常从左上角开始。对于大多数矩阵绘图,你会想要origin="lower"
回答by Bernhard
It's not inverted, just flipped. The origin for imshowdefault to the upper left rather than the lower left. imshowhas a parameter to specify the origin, it's named origin. Alternatively you can set the default in your matplotlib.conf.
不是颠倒,只是翻转。imshow默认的原点在左上角而不是左下角。imshow有一个参数来指定原点,它被命名为原点。或者,您可以在matplotlib.conf.
回答by dhpizza
BTW, you could use marker='s' to draw squares in your scatter plot instead of circles and then just keep your original code.
顺便说一句,您可以使用 marker='s' 在散点图中绘制正方形而不是圆形,然后保留原始代码。
回答by lumbric
Consider to use pcolormeshor contourfif you want to plot data of the form f(X, Y) = Z. imshowsimply plots data Z, scaling and resampling has do be done manually.
考虑使用pcolormeshorcontourf如果你想绘制表格的数据f(X, Y) = Z。imshow简单地绘制数据Z,缩放和重新采样是手动完成的。

