python - 具有不规则间隔样本的 2D 彩色图 (matplotlib.mlab.griddata)

标签 python matplotlib plot interpolation

cI 之前将此发布在 code review , 但有人告诉我它更合适,所以把它移到了这里。

基本上,我想创建一些不规则采样数据的颜色图。我在使用 matplotlib.mlab.griddata 进行插值方面取得了一些成功。但是,当我绘制插值数据(使用 matplotlib.pyplot.imshow)时,域的边缘似乎留空。如果我增加网格密度(增加代码中的 N),情况会变得更好,但不能解决问题。

我已附上我的代码,并想上传一张我可以生成的情节图片,但我仍然缺乏发布图片的声誉;)

编辑:现在已经改变了,上传了Ajean提出的改变后的情节:enter image description here

.有人可以帮我解决问题吗?

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/

相关文章:

r - ggplot 为每个值获取颜色

matlab - 如何在直方图箱上方显示标签?

python - 在 Python 2.7 中运行的代码在 Python 3.5.1 中中断

Python 和 sqlite3 - 将文本文件导入数据库

python - 如何从 Python 中的 self 方法获取 self 对象名称

python - 我如何在 Seaborn 的同一个 catplot 上显示种类 ="swarm"和种类 ="point"?

python - 如何在 matplotlib 中绘制 3 列饼图?

r - 如何更改 ggplot 中的标签(图例)?

python - 从一组问题中随机选择

python-2.7 - matplotlib.pyplot 中的阴影不确定性/误差区域