Focus on IT Recommend

Home > matplotlib - Higher order interpolation for contour plots in python

matplotlib - Higher order interpolation for contour plots in python



Is anybody of you aware of a higher order interpolation method (Catmull-Rom splines, cubic interpolation, etc.) for 2D contouring in Python?

Skimage, Matplotlib, and OpenCV provide the functions measure.find_contours(), contours() and findContours() respectively, but all are based on linear interpolation (also known as marching squares), I'm looking into something with higher accuracy in Python, preferably. Any pointers would be highly appreciated.

In the image above I'm trying to extract iso-value 25 from the scalar field of f(x,y)=x^3+y^3. I'm looking for 6 points with better accuracy than the 6 red points given by linear interpolation.

python matplotlib interpolation spline spatial-interpolation
  this question
edited Dec 2 '13 at 13:23 asked Dec 1 '13 at 13:45 Gustavo Chavez 66 1 6 1   I think that you want to smooth your data before you pass it to contour. Have you seen this post?… Scipy's griddata function might also be of interest if you are not starting with regularly spaced data:… –  DanHickstein Dec 1 '13 at 22:51      Hmmm, I think that this question is probably a duplicate of this one:… (If not, add more detail to the question to clarify.) –  DanHickstein Dec 1 '13 at 22:56      Smoothing is a good option, however it creates more points than needed, i.e. the vertices are not positioned in the edges of the scalar field. I'm looking for a more accurate interpolation at the edges (which linear interpolation does not give). Just edited the original post and add an image –  Gustavo Chavez Dec 2 '13 at 13:20      also, higher order does not always mean more accurate Higher order also makes you very sensitive to noise. –  tacaswell Dec 3 '13 at 2:17      Agree, but wouldn't be a reasonable choice to pick a 3rd order interpolation for a family of cubic implicit functions to get the best possible approximation? –  Gustavo Chavez Dec 3 '13 at 5:24


1 Answers

For unstructured 2d-data (or triangulated data), you might be interested by the following class:

Recommend:python - Matplotlib: Data cubic interpolation (or FIT) for Contour plot

lot import matplotlib.pyplot as plta = [[1,1,1],[2,2,2],[3,3,3]]b = [[1,2,3],[1,2,3],[1,2,3]]c = [[3,2,1],[1,4,2],[4,5,1]]fig1 = plt.figure()ax1 = fig1.add_subplot(111)fig1.set_size_inches(3.54,3.54)#Create Contour plotcontour=ax

which provides a Clough-Tocher (cubic) interpolator from a user-defined Triangulation and field defined at triangulation nodes. It can also be used through the helper class UniformTriRefiner:

Nevertheless the choice of the adapted interpolation depends of course of your data set.

  this answer
answered Jan 19 '14 at 11:45 GBy 1,054 6 11      Hello there, thanks for the reply. I was aware of that routine but it's not quite what I'm looking for. I made a formal problem description on Latex in the current link in order to explain myself better: –  Gustavo Chavez Jan 19 '14 at 19:03      The restriction of the CubicTriInterpolator to one edge is a 3rd degree polynom ; you can easily get an analytical expression from the value of the function + derivatives at both ends. –  GBy Jan 19 '14 at 20:43


Recommend:python - Interpolation differences on polar contour plots in Matplotlib

b to get some results. Out of curiosity I also wanted to try out the same thing in python using the matplotlib but somehow I am seeing different sets of contour plots for the same input data. I am trying to figure out whats going on and if

------splitte line----------------------------