matplotlib imshow() with irregular spaced data points

一曲冷凌霜 提交于 2019-12-01 04:26:38

I recommend using the griddata-method for interpolation. A sample would be:

import numpy as np
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt

xs0 = np.random.random((1000)) * np.pi - np.pi/2
ys0 = np.random.random((1000)) * 3.5
zs0 = np.random.random((1000))

N = 30j
extent = (-np.pi/2,np.pi/2,0,3.5)

xs,ys = np.mgrid[extent[0]:extent[1]:N, extent[2]:extent[3]:N]

resampled = griddata(xs0, ys0, zs0, xs, ys)

plt.imshow(resampled.T, extent=extent)
plt.plot(xs0, ys0, "r.")
plt.plot(xs, ys, "b.")
plt.title("imshow for irregularly spaced data using griddata")
plt.show()

I guess transition from your 3*X-array to three X-arrays is obvious.

The result is:

Red points show the "original" positions of the data, blue points for the now regularly spaced data.

griddata returns a masked array. All points for which the interpolation cannot be evaluated are masked and then plotted as white areas.

HTH, Thorsten

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!