Get from Ico-github-logo

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

  • deg2rad(deg)
  • extractpositional(args, n...
  • fractions(A, B, C)
    FRACTIONS normalise ternary data
  • simpletri(N)
    SIMPLETRI return simple triangulation for square datasets
  • ternaxes(majors)
    TERNAXES create ternary axis
  • terncontour(A, B, C, Z, I)
    TERNCONTOUR plot contours on ternary phase diagram
  • terncoords(fA, fB, fC)
    TERNCOORDS calculate rectangular coordinates of fractions on a ternary plot
  • ternlabel(A, B, C)
    TERNLABEL label ternary phase diagram
  • ternpcolor(varargin)
    TERNPCOLOR plot pseudo color diagram on ternary axes
  • ternplot(A, B, C, varargin)
    TERNPLOT plot ternary phase diagram
  • ternplot_pro(dataA, dataB...
    TERNPLOT_PRO plot ternary phase diagram, where the facecolors of the
  • ternsurf(A, B, C, Z, vara...
    TERNSURF plot surface diagram for ternary phase diagram
  • vertexlabel(A, B, C, offset)
    VERTEXLABEL label ternary phase diagram at vertices
  • terndemo.m
    Simple two-d plot
  • extractpositionalTest.m
    Normal parsing
  • View all files
4.6 | 44 ratings Rate this file 79 Downloads (last 30 days) File Size: 151 KB File ID: #2299 Version: 1.1
image thumbnail



Carl Sandrock (view profile)


28 Aug 2002 (Updated )

Plots ternary phase data on a ternary phase diagram.

| Watch this File

File Information

Plot ternary diagrams in Matlab


This file inspired Wilson Solver(), Gtl Jetfuel Ternplot With Neural Network, 3 D Ternplot, Gtl 3 D Ternplot, Entropy Triangle, Starplot, and Shepard Ternary Plot.

MATLAB release MATLAB 7.3 (R2006b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (75)
05 Jul 2016 Michael Szell  
30 Jun 2016 Carl Sandrock

Carl Sandrock (view profile)

The method you are using is correct, but remember that these x and y values will be in the rectangular coordinates that ternplot uses internally to plot the values. Unfortunately you will have to convert them back to fraction data yourself.

Comment only
29 Jun 2016 Yogesh Harshe

In ternplot, after having plotted the contour lines, is it possible to retrieve or extract the contour points along with the isocontour values?

I am trying to get it this way,

[cc, hh] = terncontour(SFCRec(:,1), SFCRec(:,2), SFCRec(:,3),SFCRec(:,i+3),v);
xPointsOfContour = get(hh(1), 'xdata'); % Get points along the first contour
yPointsOfContour = get(hh(1), 'ydata');
zPointsOfContour = get(hh(1), 'zdata');
hold on;

but I dont get the right points along the countour and their corresponding z values.

Comment only
25 Jun 2016 Carl Sandrock

Carl Sandrock (view profile)

It appears that the problem is related to the new graphics system object orientation ( I have made some changes that may cause it to work in the older version you are using. The original functions (all except ternplot_pro, which does histogramming and is newer) should work fine all the way back to 2006b.

Comment only
24 Jun 2016 Yogesh Harshe

Thanks Carl for answer. However, when I run tern_demo I get the same error.

Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure
Variables As Structures Displays Warning, for details.
> In ternplot_pro at 75
In terndemo at 20
Reference to non-existent field 'Vertices'.

Error in ternplot_pro (line 76)
v = h.Vertices;

Error in terndemo (line 20)

Comment only
24 Jun 2016 Carl Sandrock

Carl Sandrock (view profile)

Have a look at terndemo.m, which shows how each of the functions can be used. If you just want to plot the data points you should use ternplot instead of ternplot_pro.

Comment only
23 Jun 2016 Yogesh Harshe

Can you please share data set used to generate these figures? I get an error, when I use three variables dataA, dataB, and dataC with N*1 form.
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure
Variables As Structures Displays Warning, for details.
> In ternplot_pro at 75
Reference to non-existent field 'Vertices'.

Error in ternplot_pro (line 76)
v = h.Vertices;

my usual variable set looks like:

dataA =


Comment only
10 Jun 2016 leemink90  
08 Jun 2016 Carl Sandrock

Carl Sandrock (view profile)

@James: there is unfortunately not currently a method to "zoom" on the simplex, so the vertices are always the pure components. I would welcome any attempts to change this.

Regarding the labels, I find it least ambiguous to use the vertexlabel routine to label the vertices.

Comment only
07 Jun 2016 James Dutton

@Carl: Thank you, I was able to straighten everything out. I do have another question. My graphs do not fill the entire triangle whereas the examples do. Am I missing something? My data is pretty concentrated so I am not sure if that is the reason why. Also, the chart titles do not seem to match with the proper axis. What axis goes to which label. Ex. I do a ternpcolor(A,B,C,D); then ternlabel('A label','B label' ,'C label;);
Is this correct?

Comment only
03 Jun 2016 Carl Sandrock

Carl Sandrock (view profile)

@James: if you supply three arguments to ternpcolor instead of 4, it will use the third argument (C in your case) as the z-coordinate and recalculate the third composition as C=1 - (A+B). If you just want to plot positions, you should use ternplot. I see no reason why you should always see the same result with different inputs, though.

Comment only
02 Jun 2016 James Dutton

Can anyone help me out, I am trying to plot 3 vectors of values. However, whenever I do this I am getting the same graph.
I have my three vectors A B and C, size 1X625, I am using ternpcolor(A,B,C). No matter what my values are, I am getting the exact same chart. Is there something I am missing?

Comment only
13 May 2016 Carl Sandrock

Carl Sandrock (view profile)

@Michael I have fixed the problem with terndemo not working, and added a better way of handling options.

Comment only
12 May 2016 Michael Szell

Unfortunately, as of the 20160405 modification, this is broken. ternplot has parameters A,B,C, majors, varargin, so it does not work as ternplot(A,B). Because of this, also terndemo is broken.

Comment only
13 Apr 2016 Carl Sandrock

Carl Sandrock (view profile)

@Neill I am more than happy to help, but I need more information. Try going to the GitHub page and registering an issue - this forum is not a great place for interactive debugging.

Comment only
12 Apr 2016 Neil Cheong

Hi @carl. If I'm working with a large data array with more than a hundred data like in @theak 's case, specifying the Z component may be complicated. Is there any function to 'auto-generate' such matrix according to my data size and value range?

Comment only
23 Mar 2016 jiang Han  
12 Mar 2016 Carl Sandrock

Carl Sandrock (view profile)

@theak, I don't understand how you would plot a 10k x 4 array on the ternary diagram. You would need three coordinates. I don't understand the data part of your arguments. Assuming your compositions were the first three columns of your data, perhaps you could try ternplot(data(:, 1), data(:, 2), data(:, 3))

Comment only
10 Mar 2016 theak

theak (view profile)

Doesn't seem to work with my data. Sent a dataset of 10k x 4 points to this function and it just displays a blank plot. no error returned. tried running as A, B, data, as well as A, B, C, data. also tried normalizing A, B and C to 1. Nothing seemed to work.

Maybe the dataset was too big...

01 Mar 2016 Carl Sandrock

Carl Sandrock (view profile)

Unfortunately this is quite hard at the moment. If you're in a hurry, you can just edit line 62 of ternaxes.m to take out the *100. All the fractions are handled as 0-1 internally, it's just the labels that are shown as percentages.

Comment only
29 Feb 2016 Muna Mohamad Kamal

thank you calr Sandrock,
May I know, can we change the scale to range of 0 to 1.0 insteatd of 0 to 100

Comment only
20 Feb 2016 Carl Sandrock

Carl Sandrock (view profile)

A good place to start is the terndemo.m file. You can run it to see output directly and perhaps modify it by removing the parts you don't need.

Comment only
19 Feb 2016 Muna Mohamad Kamal

Hi, I new to MatLab. I need to draw ternary diagrams in MatLab.

I need guide how to use this file. Thx

Comment only
18 Feb 2016 Bikai Jin

Very easy to use

12 Jan 2016 Carl Sandrock

Carl Sandrock (view profile)

Since barycentric coordinates don't really make sense outside the vertices, I didn't take negative values into account. You can try just finding the coordinates you're interested in using TERNCOORDS which should work fine with negatives and then using a normal plot statement from there. You can use TERNAXES to draw the axes.

Comment only
11 Jan 2016 iab

iab (view profile)

This is a very handy tool. Is it possible to use it with negative values and without the normalization? I am having problems getting the axes to plot.

Comment only
08 Nov 2015 Daniel Choukroun  
25 Sep 2015 Xinlei Huang  
30 Aug 2015 Carl Sandrock

Carl Sandrock (view profile)

Hi @kanes, you should be able to plot bultiple lines using "hold" and ternplot as in normal 2d plots. Perhaps you can post an issue with some sample code on the github repository for this project?

Comment only
29 Aug 2015 kanes

kanes (view profile)

It is a great function, it is very helpful! Thank you very much!

I would like to know how to draw more than one graph on the same figure with a loop . because I tried to use 'for-loop', but there was no more scale and the boundary after plotting.

Reagard and thanks for your time!

24 Jul 2015 cheng joylin  
24 Jul 2015 cheng joylin  
21 May 2015 Carl Sandrock

Carl Sandrock (view profile)

I have started a GitHub repository for this project - you can register feature requests there.

Comment only
04 Feb 2015 Brett Sanders

Thanks for the function. One problem I am experiencing is that the labeling produced by "vertexlabel" is not consistent with the placement by "ternplot". I presently use the following code to place the labels in the right spot:


Have others experienced this problem? Can it be corrected?

Comment only
28 Aug 2014 Elizabeth Jones

Elizabeth Jones (view profile)

  • 1 file
  • 4.57143

Great function, thanks for putting this up. It worked right away, without any tweaking, which is not always the case for files on FileExchange but is always much appreciated. One feature that I did not see right away is the ability to set the size and style of the tick mark labels and axis labels, i.e. change from size 10 to size 12, make bold, etc. I had to go and select each tick mark label individually to change its size. Other than that, super program.

27 Aug 2014 Degang

Degang (view profile)

Suggestions for improvements:
1) add an option to toggle whether to plot the grid lines or not.
2) terncontour allows passing additional comments to contour function

29 Jun 2014 Manuel A. Diaz  
04 May 2014 Qibiao Yu  
28 Apr 2014 Ni

Ni (view profile)

16 Jan 2014 Hee Jung Kang


16 Jan 2014 Hee Jung Kang  
27 Dec 2013 paolo

paolo (view profile)

02 Sep 2013 Yuri K

Yuri K (view profile)

Another suggestion. The vertex labels should be at 100%, not 0%.

Comment only
02 Sep 2013 Yuri K

Yuri K (view profile)

Great function! -1 because labeling with gname cannot be used due to data sorting. Make the sort optional and/or return the sorting index.

12 Aug 2013 Ricardo

Hi, this is really useful. :)) Anyhow, just to tell you that you can make this work on Octave (not very well, but decent enough to get things done), if in ternaxes.m you change
-cax = newplot;
+cax = gca();

Comment only
09 Aug 2013 Zhang Johann

It's really useful!

03 Dec 2012 Stephan Münzberg

Many Thanks for this great tool! Only one question:
I want to have only a small part of the phase diagram because the solubilities in my experiments are small. How can I do this?

23 May 2012 Scott Rowe

I agree with Marco, it would be useful if the program supported a 3d axis with scaling.

14 May 2012 Marco

Marco (view profile)

14 May 2012 Marco

Marco (view profile)

14 May 2012 Marco

Marco (view profile)

Hello, i need help with your ternplot routine. First of all, great tool for us chem. eng. students! Good work!
I want to know if its posible to add a label in the vertical axis in the 3d portion of the diagram. I need to add a temperature scale in the vertical axis.
Regards and thanks for your time!

Marco Scetta.

Comment only
14 Mar 2012 Stefan

Stefan (view profile)

The perfect ternary plot tool...simple to use and easy to adapt to specific demands :) Thanks Carl!!!

30 Jun 2011 Dominique

Thanks a lot, it works really well. I would like to plot tie-lines as well. Do you have any idea how I can do that?

14 Jun 2011 Julia

Julia (view profile)

I need to make Piper trilinear diagrams using matlab. The ternary phase diagram is great, but it is only part of the Piper trilinear diagram (used to plot hydrogeochemical facies). Does anyone know how to create a Piper trilinear diagram in matlab?

Comment only
03 Jun 2011 Anthony Pollington

I'm pretty sure that vertexlabel.m is not working quite correctly. If the data input using ternplot is
% b
% / \
% / \
% c --- a

% this function plots
% c
% / \
% / \
% a --- b

% and the vertex labelling needs to be input as vertexlabel(C,A,B)

Comment only
18 May 2011 Leonid

Leonid (view profile)

10 May 2011 Julia

Julia (view profile)

When I used ternplot some of my points are off the grid (outside the triangle) and I haven't been able to figure out the cause. Has this happened to anyone else? Can anyone help me fix the problem?


Comment only
27 Apr 2011 hamid mehdizadeh  
31 Jul 2008 Catherine Klapperich  
21 Oct 2007 Giuseppe Cardillo

Your work is excellent. Anyway there is a bug in ternplot.
Change line 33 in
function hpol = ternplot(A, B, C, varargin)
or, in alternative, change line 55 in
handles = q;

08 Aug 2007 parvez alam  
11 Nov 2006 Wolfgang Mozart

I've been trying to use your solution for my data in excel file. I exported my data into matlab, but I got two errors:
??? Input argument "A" is undefined.

Error in ==> ternplot at 40
C = 1 - (A+B);

??? Input argument "A" is undefined.

Error in ==> ternlabel at 18
r(1) = text(0.35*sin(deg2rad(30)), 0.5, A, 'rotation', 60, 'horizontalalignment', 'center');
How can I solve this problem?

Comment only
11 Nov 2006 Wolfgang Mozart

Excellent work! Have you an Idea to get 3D ternary phase plot?

03 May 2006 Marcela edith  
25 Jan 2006 soroush taghinejad  
09 Jan 2006 Karel Verscheure

Excellent work! Maybe someone can extend this to make advanced contourplots (liquidus surfaces in color etc.)?

10 Feb 2005 Yash Mahendra

Thank you so much!! It's a boon for chem engg students!

28 Dec 2004 John Dow

It's really cool!
it would be nice to have an option to draw more than one graph on the same figure. (hold on, hold off)

11 Nov 2004 roxxot jang


23 Oct 2004 B N

thanks a lot

02 Oct 2004 Priam Pillai

Excellent program. I used it for my lab reports works great.

28 Nov 2003 joe salisbury


14 Nov 2003 W Jirat

Very useful and amazing work.

21 Aug 2003 Aanhanger...:) NOT!!!!!!!!!

Die massa toets was nie funny nie.
Ek is nie 'n student in jou klas nie, maar ek
het gehoor....

17 Jun 2003 A. Schumann

These 2 routines do exactly what I've been looking for hours now. Thanks !

25 Jun 2016 1.1

* Migrated to a GitHub repository
* Fixed the problem with vertexlabel using inconsistent labelling to the rest of the plotting routines.

Contact us