![](/img/trans.png)
[英]Python: How to interpolate 'unstructured' 2D Fourier transform data
[英]How to interpolate/display 2D data and invert 2D interpolations
我正在處理具有以下結構的 csv 數據集:
https://i.stack.imgur.com/nLqiq.png
Ideally, I would like to find an interpolated function c = c(a, b) and then invert it, ie so that I can specify a value c and it will return a number or an array of numbers such that the interpolated functional form holds . 和
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')
我似乎非常接近某種插值(盡管我不明白這種插值是如何計算的):
但是,從這里我不知道如何獲得插值的 function (我也嘗試過玩 interpol2D 但在這里也沒有運氣)特別是如何從那里反轉它。 最好的方法是什么? 我使用的數據集可以在這里找到
您可以調用plt.tricontour(df.a.values, df.b.values, df.c.values, levels=[specific_c])
繪制與特定 c 值(或 c 值列表)相對應的曲線。 或者,您可以提取這些曲線: 從等高線提取值。
輪廓算法的工作方式可能是首先將點划分為三角形( Delaunay triangulation )。 對於填充輪廓,為每個三角形頂點分配一種顏色,並對這些 colors 進行插值( Gouraud 着色)。 對於線輪廓,在頂點位於所選 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.