Python: interpolating in a triangular mesh

2024/9/27 5:51:18

Is there any decent Pythonic way to interpolate in a triangular mesh, or would I need to implement that myself? That is to say, given a (X,Y) point we'll call P, and a mesh (vertices at (X,Y) with value Z, forming triangular facets), estimate the value at P. So that means first find the facet that contains the point, then interpolate - ideally a higher order interpolation than just "linearly between the facet's vertices" (i.e., taking into account the neighboring facets)?

I could implement it myself, but if there's already something available in Python....

(I checked scipy.interpolate, but its "meshes" seem to just be regular point grids. This isn't a grid, it's a true 2D mesh; the vertices can be located anywhere.)

Answer

I often use matplotlib.tri for this purpose. Here Xv,Yv are the vertices (or nodes) of the triangles, and Zv the values at those nodes:

from matplotlib.tri import Triangulation, LinearTriInterpolator, CubicTriInterpolator#you can add keyword triangles here if you have the triangle array, size [Ntri,3]
triObj = Triangulation(Xv,Yv) #linear interpolation
fz = LinearTriInterpolator(triObj,Zv)
Z = fz(X,Y)
#cubic interpolation
fzc = CubicTriInterpolator(triObj,Zv)
Zc = fz(X,Y)
https://en.xdnf.cn/q/71488.html

Related Q&A

Customizing pytest junitxml failure reports

I am trying to introspect test failures and include additional data into the junit xml test report. Specifically, this is a suite of functional tests on an external product, and I want to include the p…

python nltk keyword extraction from sentence

"First thing we do, lets kill all the lawyers." - William ShakespeareGiven the quote above, I would like to pull out "kill" and "lawyers" as the two prominent keywords to …

Getting the parameter names of scipy.stats distributions

I am writing a script to find the best-fitting distribution over a dataset using scipy.stats. I first have a list of distribution names, over which I iterate:dists = [alpha, anglit, arcsine, beta, bet…

Does Python 3 gzip closes the fileobj?

The gzip docs for Python 3 states thatCalling a GzipFile object’s close() method does not close fileobj, since you might wish to append more material after the compressed dataDoes this mean that the g…

pip stopped working after upgrading anaconda v4.4 to v5.0

I ran the command conda update anaconda to update anaconda v4.4 to v5.0After anaconda was successfully upgraded to v5.0, I had problems running pip.This is the error output I see after running pip;Trac…

Python Django- How do I read a file from an input file tag?

I dont want the file to be saved on my server, I just want the file to be read and printed out in the next page. Right now I have this.(index.html)<form name="fileUpload" method="post…

ImportError: cannot import name AutoModelWithLMHead from transformers

This is literally all the code that I am trying to run: from transformers import AutoModelWithLMHead, AutoTokenizer import torchtokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small&…

UnicodeEncodeError: ascii codec cant encode characters in position 0-6: ordinal not in range(128)

Ιve tried all the solution that I could find, but nothing seems to work: teext = str(self.tableWidget.item(row, col).text())Im writing in greek by the way...

selenium PhantomJS send_keys doesnt work

I am using selenium and PhantomJS for testing. I followed Seleniums simple usage, but send_keys doesnt work on PhantomJS, it works on Firefox. Why? I have to use button.click() instead?#!/usr/bin/pyt…

Replace values in column of Pandas DataFrame using a Series lookup table

I want to replace a column of values in a DataFrame with a more accurate/complete set of values generated by a look-up table in the form of a Series that I have prepared.I thought I could do it this wa…