File Exchange

image thumbnail

Contours for triangular grids

version (341 KB) by Darren Engwirda
Generate smooth contours for functions defined on unstructured triangular grids


Updated 15 May 2006

No License

Plots the contours of a function defined on an unstructured triangular grid directly, without the need to interpolate back onto a Cartesian array.

I have attempted to replicate much of the functionality of the regular "contour" function.

Type "help tricontour"

Type "contourdemo" for some examples

You may also find my mesh generator "mesh2d.m" in the physics and Chem section helpful.

UPDATE 11/05/2006
- Can now be used with clabel
- A bit faster

UPDATE 15/05/2006
- Much more accurate (now uses quadratic interpolation)

Cite As

Darren Engwirda (2020). Contours for triangular grids (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (27)


Hi Darren,

do you know why the demo script does not work with clabel?

Error using clabel (line 77)
Not enough contour handles.



Zoi T.

Hello, and thank you for this contour plot method! The functionality missing for triangle grids in MATLAB is indeed a bit frustrating. I have a small problem with this method though. I am using MATLAB 2015b, and I guess there has been some changes in the contour handle in the meantime. In particular the patch array, which is returned from the tricontour method creates a "Not enough contour handles" error in the clabel-call. MATLAB's contour method returns a handle that is a "contour"-object. I couldn't figure out much about how this object is created, it seems I have no access to the method that is creating the handle. Does anyone have a work-around this?

Manuel A. Diaz


Great! any plans to make a version with filled contours? (like contourf) that'd be even better!

Kim Nguyen

could somone please help, ive tried following the examples but it keeps telling me i dont put in enough arguments.
i have x,y, and dz as my coords

Nachiket Patil


Thank you Darren. I appreciate your superb contribution.

l l




Why I got some artifacts when using my own triangulated mesh? It generated some "contours" at the places where no triangles exist. Does anybody know why?

The mesh is no problem; I double checked.


illoul lounes

very usful, must be integrated to matlab.

thanks !

Steve Fitzgerald

I'm sure this will be 5* when I get it to work; excuse my ignorance. I have a triangular mesh and x,y,z data that I plot with

trisurf(tri, x, y, z)

and I tried

[c, h] = tricontour([x y], tri, z, 15)

but I get an error where it tries flag(NaN). I'm pretty sure my data is OK, and trisurf works fine...

Xavy Kong

Sorry I missed three pnts when I shortened the lines. It should be like this.
A=[0 0 .5;1 0 .6; 1 1 .7;1 1 .7;2 1 .6;2 2 .5;1 1 .7;2 0 .4;2 1 .6];
x=A(:,1); y=A(:,2); z=A(:,3);
P=[x(:),y(:)]; T=[1,2,3;4,5,6;7,8,9];
I tried to use Fepoint format as in tecplot, but your suggestion not using pnt no.4 will plot the contour lines between my triangulars not within them. I am sorry that I did not quite understand the clauses arround 'bnd(next) = ce; % Found bnd edge' . Anyway, you have done a very nice job and I really appreciate your contribution and your assistance to me. I can try to fix my own problem later.

John D'Errico

In the test case given by Xavy, note that there are a pair of points at (x,y) == (1,1). This will cause any such routine to fail that is based on triangulating the data.

With replicates in your data, remove them, averaging the z values at those replicated points.

Of course, even this will not help your example, since you then try to create a triangulation

> T=[1,2,3;4,5,6;7,8,9];

You only have SIX points in the set of point in A. Surely you cannot create a triangulation that refers to points 7, 8 and 9 too! A valid triangulation of this data would be

T = [1 2 3;2 3 5;3 5 6];

See that I never needed to use point 4, but also that this is a valid triangulation. I would expect tricontour to work properly with this triangulation.

Xavy Kong

A=[0 0 .5;1 0 .6; 1 1 .7; 1 1 .7; 2 1 .6; 2 2 .5];
x=A(:,1); y=A(:,2); z=A(:,3);
P=[x(:),y(:)]; T=[1,2,3;4,5,6;7,8,9];
[c,h] = tricontour(P,T,z,3); % why not work ?
T = delaunayn(P);
[c,h] = tricontour(P,T,z,3); % % why not work ?
% This has to be improved?????????????

Jack Kohoutek

Nik F

Clemens Moser

Works very well, there is just a minor issue: the contours are not interpolated at the boundaries of the surface .

Friedemann Groh

Rabi Tahir

Works well... seems to be a bit slow, but I may be showing a bias (comparing with traditional fortran generated machine code)

prabhakaran sundhararaju

it's very usefull for chemical, mechanical and civil. to analysis critical domain. i wish to develop 3D domain also

Babak Asghari

Kong Zuor

This is what I have been looking for since I start using Matlab. Great job well done! I wish to see this code in the next Matlab release. I find this code very useful to those who solve CFD problems with matlab. The author deserve my five star ranking. I would encourage the author to advance this code for 3D case. I am free at last!
Many thanks

M. Oswald

Very good contribution. I wish the author could extend the tricontour function to handle 3D triangle meshes as well.

John D'Errico

Very nice. This is a function that should have been in Matlab itself since the day they provided trisurf and trimesh. It handled some troublesome cases I tried when I tried to break it. I'll give it a 5 rating because it deserves that rating. My only wish is that it was even more compatible with the contour functionality of matlab. For example, no handles are returned, in case you wish to set a line color or style. It might also be nice to provide a way to label the contours via clabel.

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: TRIGRADIENT.M, trisurfc

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!