Plotting · contour export
Contour Zones for Tecplot: Exporting Contour Lines
A visualization package can draw its own contours from a grid, but sometimes the contour lines themselves are the product: a script computes the level sets, and the plotting program only displays them. Exporting contour lines as zones keeps full control over the levels, makes the lines available for further analysis, and works with any tool that reads polylines. This page shows how contour segments are found on a grid and how they are written out as zones in the ASCII layout described under exporting data to Tecplot. It is one of the plotting methods under Data and files.
Finding the crossing on one cell
The classic method is marching squares. Take a grid cell with four corner values. For a chosen level c, mark each corner as above or below c. A contour line crosses an edge exactly when its two corners lie on opposite sides. With four corners there are 16 above-or-below patterns; most produce one line segment through the cell, two patterns produce two segments, and the all-above and all-below patterns produce none.
Where the line crosses an edge follows from linear interpolation along that edge. If the corner values are z₁ and z₂, the crossing is at fraction t = (c − z₁) / (z₂ − z₁) of the way from the first corner to the second. Take z₁ = 2, z₂ = 6 and c = 3: t = (3 − 2) / (6 − 2) = 0.25. On an edge running from x = 0 to x = 1, the crossing sits at x = 0.25. Linear interpolation is exact for a function that is linear along the edge and a good approximation when the grid is fine.
def edge_point(p1, p2, z1, z2, c):
t = (c - z1) / (z2 - z1)
return (p1[0] + t * (p2[0] - p1[0]),
p1[1] + t * (p2[1] - p1[1]))
The division is safe because the function is only called when z1 and z2 are on opposite sides of c, so they are different.
Joining segments into lines
Marching squares delivers loose segments, one or two per cell. Neighboring cells share edges, so the end of one segment equals the start of another. Joining them means matching endpoints, which is most reliable when each crossing is identified by its edge, say the pair of corner indices, instead of by floating-point coordinates. Follow a chain of segments until it returns to its start, giving a closed loop, or leaves the grid, giving an open line. For a bowl-shaped function such as x² + y², the contours at levels inside the grid are closed circles; at levels larger than the grid reaches, they are open arcs cut off at the boundary.
Cells where the four corners alternate above and below form a saddle, and the two ways of pairing the crossings give different pictures. The usual fix is to evaluate the average of the four corners: if it is above the level, connect so that the high corners stay joined, and otherwise the low ones. Without such a rule, the lines may cross each other, which a contour never should.
Writing one zone per line
An open or closed line is a one-dimensional ordered zone: a single index with as many points as the line has vertices. Closed loops should repeat the first point at the end, so the plot closes without a gap. Put the level value in a zone title or as a constant variable, so the plotting program can color lines by level.
def write_line_zone(f, pts, level):
f.write(f'ZONE T="level {level:g}", I={len(pts)}, DATAPACKING=POINT\n')
for x, y in pts:
f.write(f"{x:.8g} {y:.8g} {level:.8g}\n")
With a variable list of X, Y and Z, the third column holds the constant level, giving every line its height in 3D as well.
Choosing levels and checking
Pick levels by the rules under contour levels. Then check the output in three ways: every vertex of a line at level c should interpolate back to a value within rounding of c, the number of zones should equal the number of connected lines, and a quick plot of the exported lines over the original data should show them lying on the color boundaries. Counting rows against the declared size of each zone before loading avoids the most frequent file errors, as discussed in comparing two files.