pythonmatplotlibscipycontourqhull

Contour of scattered data via interpolation or QHull in python


I'm trying to plot a contour at z = .95 out of my data however, I couldn't manage to interpolate as I want. I tried to use griddata as follows

from scipy.interpolate import griddata
N = 1000
xi = np.linspace(min(x), max(y), N)
yi = np.linspace(min(x), max(y), N)
c = griddata((np.array(x),np.array(y)), 
              np.array(z), (xi[None,:],
              yi[:,None]), method='linear')
fig, sys = plt.subplots()

sys.contour(xi, yi, c, levels = [.95],
     colors=('darkred',),linestyles=('solid',),linewidths=(2,))

also as can be seen in the graph below I tried to use qhull by cutting the z-axis at 0.95.

a = genfromtxt('data.txt')[:,[0,1]] #data where z <= .95
hull = ConvexHull(a)
sys.plot(a[hull.vertices,0], a[hull.vertices,1], color='red', 
          linestyle='--', lw=2.5, zorder=90, label=r"QHUL")

Below I tried to illustrate both methods and also how it essentially should look like (its a different data just for illustration purposes), however, due to the dip in my data around (1.7, 420) I am getting zigzags in interpolation for that region which I couldn't even fix by treating pieces of data separately and QHULL method just misses accuracy of the data thus I can not use it. Is there any way to interpolate the data to get a similar curve as shown below?

Thanks!

The output of the code

My data is as follows (x,y,z);

1.950e+00   1.500e+02   9.557e-01
1.950e+00   4.800e+02   9.302e-01
1.950e+00   3.100e+02   9.467e-01
1.900e+00   5.500e+02   9.493e-01
1.700e+00   6.000e+02   9.359e-01
1.700e+00   5.500e+02   9.447e-01
8.430e-01   7.800e+02   9.906e-01
1.300e+00   9.000e+02   9.349e-01
1.655e+00   8.132e+02   9.406e-01
1.138e+00   8.453e+02   9.542e-01
1.728e+00   4.895e+02   9.335e-01
1.953e+00   2.254e+02   9.507e-01
1.932e+00   4.706e+01   9.552e-01
1.661e+00   8.081e+02   9.287e-01
1.956e+00   9.931e+00   9.320e-01
1.947e+00   4.457e+01   9.396e-01
1.949e+00   9.769e+01   9.575e-01
1.912e+00   4.441e+02   9.616e-01
1.956e+00   3.739e+01   9.344e-01
1.953e+00   1.042e+02   9.277e-01
1.957e+00   0.000e+00   9.329e-01
1.938e+00   3.455e+01   9.411e-01
1.946e+00   6.045e+01   9.381e-01
1.951e+00   8.227e+01   9.571e-01
1.962e+00   2.500e+01   9.478e-01
1.951e+00   2.778e+01   9.559e-01
1.949e+00   6.736e+01   9.630e-01
1.949e+00   1.097e+02   9.331e-01
1.708e+00   4.998e+02   9.526e-01
1.951e+00   1.250e+02   9.516e-01
1.730e+00   4.642e+02   9.332e-01
1.912e+00   4.780e+02   9.558e-01
1.927e+00   5.145e+02   9.401e-01
1.712e+00   5.203e+02   9.519e-01
1.722e+00   5.470e+02   9.396e-01
1.962e+00   1.117e+02   9.519e-01
1.962e+00   2.195e+01   9.269e-01
1.962e+00   3.366e+01   9.514e-01
1.959e+00   9.610e+01   9.270e-01
1.959e+00   4.537e+01   9.281e-01
1.959e+00   6.488e+01   9.277e-01
1.959e+00   7.659e+01   9.346e-01
1.953e+00   4.537e+01   9.615e-01
1.950e+00   1.820e+02   9.552e-01
1.950e+00   1.702e+02   9.547e-01
1.950e+00   1.415e+01   9.389e-01
1.947e+00   2.639e+02   9.517e-01
1.947e+00   2.015e+02   9.533e-01
1.941e+00   3.029e+02   9.533e-01
1.935e+00   2.873e+02   9.573e-01
1.959e+00   1.415e+01   9.314e-01
1.959e+00   2.439e+00   9.335e-01
1.899e+00   5.137e+02   9.549e-01
1.896e+00   5.371e+02   9.563e-01
1.888e+00   5.839e+02   9.531e-01
1.870e+00   5.917e+02   9.553e-01
1.722e+00   4.746e+02   9.468e-01
1.716e+00   4.278e+02   9.604e-01
1.704e+00   5.644e+02   9.482e-01
1.683e+00   5.800e+02   9.574e-01
1.609e+00   6.854e+02   9.477e-01
1.263e+00   8.766e+02   9.417e-01
1.198e+00   8.532e+02   9.524e-01
1.172e+00   8.532e+02   9.394e-01
1.927e+00   3.807e+02   9.540e-01
1.582e+00   8.424e+02   9.569e-01
1.000e+00   8.415e+02   9.526e-01
8.817e-01   7.985e+02   9.348e-01
1.954e+00   3.139e+00   9.364e-01
1.932e+00   3.583e+02   9.585e-01
1.910e+00   5.018e+02   9.500e-01
1.891e+00   5.628e+02   9.505e-01
1.858e+00   5.987e+02   9.470e-01
1.752e+00   4.874e+02   9.974e-01
1.711e+00   4.803e+02   9.477e-01
1.698e+00   5.341e+02   9.545e-01
1.687e+00   5.628e+02   9.570e-01
1.638e+00   6.596e+02   9.525e-01
1.624e+00   7.996e+02   9.559e-01
1.624e+00   8.211e+02   9.523e-01
1.619e+00   6.632e+02   9.550e-01
1.611e+00   8.283e+02   9.510e-01
1.605e+00   8.354e+02   9.537e-01
1.597e+00   6.776e+02   9.566e-01
1.592e+00   8.426e+02   9.445e-01
1.956e+00   7.908e+01   9.259e-01

Solution

  • It turns out that the data span and interpolation splitting is important

    N = 40
    x = linspace(0.5,2.4,N)
    y = linspace(0.,1100.,N)
    
    mean_CL = griddata((Mgo,Mn1), mean_CLs, (x[None,:], y[:,None]), method='linear')
    
    sc.contour(x,y,mean_CL,levels = [.95],colors=('darkred',),linestyles=('solid',),linewidths=(2,))
    

    did the job. However, instead of having data clustered in one region, one might need to span the entire x-y plane, points don't need to be too close I gathered grid 25x0.025 and it worked perfectly.