pylab散点图出现转置

时间:2015-06-26 13:17:57

标签: python matplotlib scatter

我一直在玩各种插值技术 - 尤其喜欢youtube视频中显示的变种 https://www.youtube.com/watch?v=_cJLVhdj0j4

但是,散点模块会将点绘制在错误的位置。我已将它们转换到下面(示例5)以使其工作,但如果感兴趣的区域不以原点(Test_Rbf)为中心,则此方法无效。

我是否误解了一些基本的东西,或者这是pylab分散模块中的一个问题?

# Example 5 
#
# https://www.youtube.com/watch?v=_cJLVhdj0j4

import numpy as np
from scipy import interpolate
import pylab as py

def func(x,y):
return (x+y)*np.cos(-5.0*x + 4.0*y)

x = np.random.uniform(-1.0, 1.0,size=50)
y = np.random.uniform(-1.0, 1.0,size=50)
fvals = func(x,y)

newfunc = interpolate.Rbf(x, y, fvals, function='multiquadric')
xnew, ynew = np.mgrid[-1:1:100j, -1:1:100j]

fnew = newfunc(xnew, ynew)
true = func(xnew, ynew)

py.figure()
py.clf()
py.imshow( fnew, extent=[-1,1,-1,1], cmap=py.cm.jet)
# py.scatter( x, y, 30, fvals, cmap=py.cm.jet)
py.scatter( y, -x, 30, fvals, cmap=py.cm.jet)

py.show()

from enthought.mayavi import mlab

mlab.clf()
mlab.surf(xnew, ynew, fnew*2)

1 个答案:

答案 0 :(得分:1)

如果您使用

ynew, xnew = np.mgrid[-1:1:100j, -2:2:100j]

而不是

xnew, ynew = np.mgrid[-1:1:100j, -2:2:100j]

然后xnew会随着您在列之间移动而变化,ynew会随着您向下移动而变化。 (我将x范围从[-1,1]更改为[-2,2],以明确哪些数字控制哪个范围。)

将此与@ hitzg的建议结合起来,将origin='lower'添加到imshow的来电中,您就会得到:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
np.random.seed(2015)
def func(x,y):
    return (x+y)*np.cos(-5.0*x + 4.0*y)

x = np.random.uniform(-2.0, 2.0, size=50)
y = np.random.uniform(-1.0, 1.0, size=50)
fvals = func(x,y)

newfunc = interpolate.Rbf(x, y, fvals, function='multiquadric')
ynew, xnew = np.mgrid[-1:1:100j, -2:2:100j]

fnew = newfunc(xnew, ynew)

plt.figure()
plt.imshow(fnew, extent=[-2,2,-1,1], cmap=plt.cm.jet, origin='lower')
plt.scatter(x, y, s=30, c=fvals, cmap=plt.cm.jet)
plt.show()

enter image description here