0

I am dealing with a csv data set of the following structure:

https://i.stack.imgur.com/nLqiq.png

Ideally, I would like to find an interpolated function c = c(a, b) and then invert it, i.e. 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):

enter image description here

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. What would be the best way to do this? The data set I am using can be found here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
RataData
  • 3
  • 1
  • Maybe you could just 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). Optionally, you could also extract these curves: [extracting values from contour lines](https://stackoverflow.com/questions/17051131/matplotlib-extracting-values-from-contour-lines). – JohanC Jan 19 '21 at 11:42
  • Please do not post data/code/error messages as images. Post the text directly here on SO. – Mr. T Jan 19 '21 at 12:34

1 Answers1

1

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). 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). For the filled contour, a color is assigned to each triangle vertex and these colors are interpolated (Gouraud shading). 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.

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

illustrating plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Hi @JohanC, many thanks that solved most of my issues. Just one more technical question. I tried overlaying one single contour with the rest of the image using: `ax.tricontour(df.a.values,df.b.values,df.c.values, levels = [c], color='white') ax.tricontourf(df.a.values,df.b.values,df.c.values, 100)`. While this works, it does not want to change the color of the single contour and complains `UserWarning: The following kwargs were not used by contour: 'color'`. Any idea how I can prevent that? – RataData Jan 19 '21 at 12:51
  • For some unknown reason, in this function the only way to name the argument is `colors=`. Many other functions have `color=` without `s` or even `c=`. – JohanC Jan 19 '21 at 13:13