cI 之前将此发布在 code review , 但有人告诉我它更合适,所以把它移到了这里。
基本上,我想创建一些不规则采样数据的颜色图。我在使用 matplotlib.mlab.griddata 进行插值方面取得了一些成功。但是,当我绘制插值数据(使用 matplotlib.pyplot.imshow)时,域的边缘似乎留空。如果我增加网格密度(增加代码中的 N),情况会变得更好,但不能解决问题。
我已附上我的代码,并想上传一张我可以生成的情节图片,但我仍然缺乏发布图片的声誉;)
编辑:现在已经改变了,上传了Ajean提出的改变后的情节:
.有人可以帮我解决问题吗?
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.mlab import griddata
# Generate Data
X=np.random.random(100)
Y=2*np.random.random(100)-1
Z=X*Y
# Interpolation
N=100j
extent=(0,1,-1,1)
xs,ys = np.mgrid[extent[0]:extent[1]:N, extent[2]:extent[3]:N]
resampled=griddata(X,Y,Z,xs,ys,interp='nn')
#Plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('X')
ax.set_ylabel('Y')
cplot=ax.imshow(resampled.T,extent=extent)
ticks=np.linspace(-1,1,11)
cbar=fig.colorbar(magplot,ticks=ticks,orientation='vertical')
cbar.set_label('Value', labelpad=20,rotation=270,size=16)
ax.scatter(X,Y,c='r')
最佳答案
这是因为您对 random 的调用不会在边界角处为您提供任何值,因此没有可插值的内容。如果将 X 和 Y 定义更改为
# Just include the four corners
X=np.concatenate([np.random.random(100),[0,0,1,1]])
Y=np.concatenate([2*np.random.random(100)-1,[-1,1,1,-1]])
您将填写整个内容。
关于python - 具有不规则间隔样本的 2D 彩色图 (matplotlib.mlab.griddata),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25747393/