我在 python 和 matplotlib 中有一个简单的问题。我有 3 个列表:x、y 和 rho,其中 rho[i] 在点 x[i]、y[i] 处具有密度。x 和 y 的所有值都在 -1 之间。和 1. 但它们没有特定的顺序。
如何制作密度 rho(在点 x、y 处插值)的等高线图(如 imshow)。
非常感谢你。
编辑:我使用大型数组:x、y 和 rho 有 10,000 到 1,000,000 个元素
我在 python 和 matplotlib 中有一个简单的问题。我有 3 个列表:x、y 和 rho,其中 rho[i] 在点 x[i]、y[i] 处具有密度。x 和 y 的所有值都在 -1 之间。和 1. 但它们没有特定的顺序。
如何制作密度 rho(在点 x、y 处插值)的等高线图(如 imshow)。
非常感谢你。
编辑:我使用大型数组:x、y 和 rho 有 10,000 到 1,000,000 个元素
您需要插入您的rho
值。没有一种方法可以做到这一点,并且“最佳”方法完全取决于您应该纳入插值的先验信息。
不过,在我开始对“黑盒”插值方法进行抨击之前,径向基函数(例如,“薄板样条”是一种特殊类型的径向基函数)通常是一个不错的选择。如果你有数百万个点,这个实现将是低效的,但作为一个起点:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
# Generate data:
x, y, z = 10 * np.random.random((3,10))
# Set up a regular grid of interpolation points
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)
# Interpolate
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)
plt.imshow(zi, vmin=z.min(), vmax=z.max(), origin='lower',
extent=[x.min(), x.max(), y.min(), y.max()])
plt.scatter(x, y, c=z)
plt.colorbar()
plt.show()
您可以使用 scipy griddata
(需要 Scipy >= 0.10),这是一种基于三角测量的方法。
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
# Generate data: for N=1e6, the triangulation hogs 1 GB of memory
N = 1000000
x, y = 10 * np.random.random((2, N))
rho = np.sin(3*x) + np.cos(7*y)**3
# Set up a regular grid of interpolation points
xi, yi = np.linspace(x.min(), x.max(), 300), np.linspace(y.min(), y.max(), 300)
xi, yi = np.meshgrid(xi, yi)
# Interpolate; there's also method='cubic' for 2-D data such as here
zi = scipy.interpolate.griddata((x, y), rho, (xi, yi), method='linear')
plt.imshow(zi, vmin=rho.min(), vmax=rho.max(), origin='lower',
extent=[x.min(), x.max(), y.min(), y.max()])
plt.colorbar()
plt.show()
还有反距离加权插值 - 类似于 RBF,但对于大点数应该更好:Inverse Distance Weighted (IDW) Interpolation with Python