Search code examples
pythonmatplotlibcontournonlinear-functions

How can I get Isocontour's xy coordinates for contour plot?


I am trying to get isosurface's x-y coordinates from 3D plot. Here is my attempt;

import matplotlib.pyplot as plt

from numpy import pi, cos, sin, linspace, meshgrid

x = linspace(0,50,1000)
y = linspace(0,50,1000)

n = 5
L = 50
t = 0

def gyroid(x, y, n, L, t):
    tanım1 = (sin(2*pi*n*x/L) * cos(2*pi*n*y/L) + sin(2*pi*n*y/L) + cos(2*pi*n*x/L))
    return tanım1*tanım1 - t**2

XX, YY = meshgrid(x, y)
z = gyroid(XX, YY, n, L, t)

thickness = 0.1
contour = plt.contour(XX, YY, z,levels=[thickness])
# Attempt to get x-y coordinates
dat0= contour.allsegs[0][0]
plt.plot(dat0[:,0],dat0[:,1])

The gyroid function is normally looks like; 3D plot

I am getting isocontour for z = 0.1 plane; Void plot

I need xy pairs of these voids. But when I try, the code is only getting lower left coordinates. It is clear that function is strongly nonlinear, but is there any way to retrieve these coordinates?

Thanks for your responses in advance.


Solution

  • You specify contour.allsegs[0][0] so you get the very first line of the first contour line.

    for lines in contour.allsegs:
        for line in lines:
            X, Y = line[:,0], line[:,1]
            plt.plot(X, Y)
    

    enter image description here