简体   繁体   English

如何插值/显示 2D 数据和反转 2D 插值

[英]How to interpolate/display 2D data and invert 2D interpolations

I am dealing with a csv data set of the following structure:我正在处理具有以下结构的 csv 数据集:

https://i.stack.imgur.com/nLqiq.png 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. 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 . With

 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')

I seem to get pretty close to some kind of interpolation (even though I don't understand how exactly this interpolation is computed):我似乎非常接近某种插值(尽管我不明白这种插值是如何计算的):

在此处输入图像描述

However, from here I don't know how I can get the interpolated function (I also tried playing with interpol2D but no luck here either) an especially how to invert it from there.但是,从这里我不知道如何获得插值的 function (我也尝试过玩 interpol2D 但在这里也没有运气)特别是如何从那里反转它。 What would be the best way to do this?最好的方法是什么? The data set I am using can be found here我使用的数据集可以在这里找到

You could call plt.tricontour(df.a.values, df.b.values, df.c.values, levels=[specific_c]) which draws curves corresponding to the specific c-value (or list of c-values).您可以调用plt.tricontour(df.a.values, df.b.values, df.c.values, levels=[specific_c])绘制与特定 c 值(或 c 值列表)相对应的曲线。 Optionally, you could extract these curves: extracting values from contour lines .或者,您可以提取这些曲线: 从等高线提取值

The way the contour algorithm probably works, is first dividing the points into triangles ( Delaunay triangulation ).轮廓算法的工作方式可能是首先将点划分为三角形( Delaunay triangulation )。 For the filled contour, a color is assigned to each triangle vertex and these colors are interpolated ( Gouraud shading ).对于填充轮廓,为每个三角形顶点分配一种颜色,并对这些 colors 进行插值( Gouraud 着色)。 For the line contours, onto each triangle where the vertices are on different sides of the chosen c-value, interpolate the value on the triangle edges and connect them with lines.对于线轮廓,在顶点位于所选 c 值不同侧的每个三角形上,插入三角形边缘上的值并将它们与线连接。

Here is an illustrating example:这是一个说明性示例:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM