如何 interpolate/display 二维数据和反转二维插值
How to interpolate/display 2D data and invert 2D interpolations
我正在处理以下结构的 csv 数据集:
https://i.stack.imgur.com/nLqiq.png
理想情况下,我想找到一个插值函数 c = c(a, b) 然后反转它,即这样我就可以指定一个值 c 并且它将 return 一个数字或一个数组的数字,使得内插函数形式成立。有
df = pd.read_csv('data.txt', sep=",", header=None)
plt.tricontourf(df.a.values, df.b.values, df.c.values, 50)
plt.plot(df.a.values, df.b.values, 'k+', markersize = 3, alpha=0.3, color='white')
我似乎非常接近某种插值(尽管我不明白这种插值是如何计算的):
但是,从这里我不知道如何获得插值函数(我也尝试过使用 interpol2D 但在这里也没有运气),尤其是如何从那里反转它。最好的方法是什么?我使用的数据集可以找到here
您可以调用 plt.tricontour(df.a.values, df.b.values, df.c.values, levels=[specific_c])
来绘制对应于特定 c 值(或 c 值列表)的曲线。或者,您可以提取这些曲线:extracting values from contour lines.
轮廓算法的工作方式可能是首先将点分成三角形 (Delaunay triangulation). For the filled contour, a color is assigned to each triangle vertex and these colors are interpolated (Gouraud shading)。对于线轮廓,在顶点位于所选 c 值不同侧的每个三角形上,在三角形边上插值并用线连接它们。
这是一个说明性的例子:
import numpy as np
import matplotlib.pyplot as plt
a = np.random.uniform(-1.5, 1.5, 5000)
b = np.random.uniform(-1.5, 1.5, 5000)
c = (a ** 2 + b ** 2 - 1) ** 3 - a ** 2 * b ** 3
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(14, 4), sharex=True, sharey=True,
gridspec_kw={'width_ratios': [5, 4, 4]})
cntf = ax1.tricontourf(a, b, c, levels=np.linspace(-2, 2, 101), cmap='RdYlGn')
plt.colorbar(cntf, ax=ax1)
cnt = ax2.tricontour(a, b, c, levels=[0], colors='crimson', linewidths=4)
verts = np.concatenate([p.vertices for p in cnt.collections[0].get_paths()])
ax3.scatter(verts[:, 0], verts[:, 1], s=1, c='turquoise')
plt.show()
我正在处理以下结构的 csv 数据集:
https://i.stack.imgur.com/nLqiq.png
理想情况下,我想找到一个插值函数 c = c(a, b) 然后反转它,即这样我就可以指定一个值 c 并且它将 return 一个数字或一个数组的数字,使得内插函数形式成立。有
df = pd.read_csv('data.txt', sep=",", header=None)
plt.tricontourf(df.a.values, df.b.values, df.c.values, 50)
plt.plot(df.a.values, df.b.values, 'k+', markersize = 3, alpha=0.3, color='white')
我似乎非常接近某种插值(尽管我不明白这种插值是如何计算的):
但是,从这里我不知道如何获得插值函数(我也尝试过使用 interpol2D 但在这里也没有运气),尤其是如何从那里反转它。最好的方法是什么?我使用的数据集可以找到here
您可以调用 plt.tricontour(df.a.values, df.b.values, df.c.values, levels=[specific_c])
来绘制对应于特定 c 值(或 c 值列表)的曲线。或者,您可以提取这些曲线:extracting values from contour lines.
轮廓算法的工作方式可能是首先将点分成三角形 (Delaunay triangulation). For the filled contour, a color is assigned to each triangle vertex and these colors are interpolated (Gouraud shading)。对于线轮廓,在顶点位于所选 c 值不同侧的每个三角形上,在三角形边上插值并用线连接它们。
这是一个说明性的例子:
import numpy as np
import matplotlib.pyplot as plt
a = np.random.uniform(-1.5, 1.5, 5000)
b = np.random.uniform(-1.5, 1.5, 5000)
c = (a ** 2 + b ** 2 - 1) ** 3 - a ** 2 * b ** 3
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(14, 4), sharex=True, sharey=True,
gridspec_kw={'width_ratios': [5, 4, 4]})
cntf = ax1.tricontourf(a, b, c, levels=np.linspace(-2, 2, 101), cmap='RdYlGn')
plt.colorbar(cntf, ax=ax1)
cnt = ax2.tricontour(a, b, c, levels=[0], colors='crimson', linewidths=4)
verts = np.concatenate([p.vertices for p in cnt.collections[0].get_paths()])
ax3.scatter(verts[:, 0], verts[:, 1], s=1, c='turquoise')
plt.show()