Show Sidebar Hide Sidebar

Ternary Plots in MATLAB

How to make a ternary plot in MATLAB. A ternary plot shows changes over 3 axes.

trace1 = struct(...
  'a', [0, 15, 20, 20, 5, 5, 0], ...
  'b', [70, 85, 80, 53, 53, 45, 50], ...
  'c', [30, 0, 0, 32, 42, 50, 50], ...
  'fill', 'toself', ...
  'fillcolor', '#8dd3c7', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'sandy loam', ...
  'type', 'scatterternary', ...
  'uid', 'a1a8b8');
trace2 = struct(...
  'a', [20, 35, 35, 28, 20], ...
  'b', [80, 65, 45, 45, 53], ...
  'c', [0, 0, 20, 27, 32], ...
  'fill', 'toself', ...
  'fillcolor', '#ffffb3', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'sandy clay loam', ...
  'type', 'scatterternary', ...
  'uid', 'c72344');
trace3 = struct(...
  'a', [40, 40, 28, 28], ...
  'b', [45, 20, 20, 45], ...
  'c', [15, 40, 52, 27], ...
  'fill', 'toself', ...
  'fillcolor', '#bebada', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'clay loam', ...
  'type', 'scatterternary', ...
  'uid', '3eab74');
trace4 = struct(...
  'a', [0, 10, 15, 0], ...
  'b', [90, 90, 85, 70], ...
  'c', [10, 0, 0, 30], ...
  'fill', 'toself', ...
  'fillcolor', '#fb8072', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'loamy sand', ...
  'type', 'scatterternary', ...
  'uid', 'b9dc9f');
trace5 = struct(...
  'a', [35, 35, 55], ...
  'b', [65, 45, 45], ...
  'c', [0, 20, 0], ...
  'fill', 'toself', ...
  'fillcolor', '#80b1d3', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'sandy clay', ...
  'type', 'scatterternary', ...
  'uid', '381ad2');
trace6 = struct(...
  'a', [60, 40, 40], ...
  'b', [0, 0, 20], ...
  'c', [40, 60, 40], ...
  'fill', 'toself', ...
  'fillcolor', '#fdb462', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'silty clay', ...
  'type', 'scatterternary', ...
  'uid', '8cc163');
trace7 = struct(...
  'a', [0, 28, 28, 12, 12, 0], ...
  'b', [50, 22, 0, 0, 8, 20], ...
  'c', [50, 50, 72, 88, 80, 80], ...
  'fill', 'toself', ...
  'fillcolor', '#b3de69', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'silty loam', ...
  'type', 'scatterternary', ...
  'uid', '6f33dc');
trace8 = struct(...
  'a', [0, 10, 0], ...
  'b', [100, 90, 90], ...
  'c', [0, 0, 10], ...
  'fill', 'toself', ...
  'fillcolor', '#fccde5', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'sand', ...
  'type', 'scatterternary', ...
  'uid', 'cf9e4b');
trace9 = struct(...
  'a', [28, 28, 40, 40], ...
  'b', [0, 20, 20, 0], ...
  'c', [72, 52, 40, 60], ...
  'fill', 'toself', ...
  'fillcolor', '#d9d9d9', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'silty clay loam', ...
  'type', 'scatterternary', ...
  'uid', 'cb4ee4');
trace10 = struct(...
  'a', [55, 100, 60, 40, 40], ...
  'b', [45, 0, 0, 20, 45], ...
  'c', [0, 0, 40, 40, 15], ...
  'fill', 'toself', ...
  'fillcolor', '#bc80bd', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'clay', ...
  'type', 'scatterternary', ...
  'uid', '05c24e');
trace11 = struct(...
  'a', [0, 0, 12, 12], ...
  'b', [0, 20, 8, 0], ...
  'c', [100, 80, 80, 88], ...
  'fill', 'toself', ...
  'fillcolor', '#ccebc5', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'silt', ...
  'type', 'scatterternary', ...
  'uid', 'bdc504');
trace12 = struct(...
  'a', [28, 28, 5, 5, 20], ...
  'b', [45, 22, 45, 53, 53], ...
  'c', [27, 50, 50, 42, 32], ...
  'fill', 'toself', ...
  'fillcolor', '#ffed6f', ...
  'line', struct('color', '#444'), ...
  'mode', 'lines', ...
  'text', 'loam', ...
  'type', 'scatterternary', ...
  'uid', 'ef23fc');
data = {trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8, trace9, trace10, trace11, trace12};
layout = struct(...
    'autosize', true, ...
    'height', 545, ...
    'showlegend', false, ...
    'ternary', struct(...
      'aaxis', struct(...
        'linewidth', 2, ...
        'min', 0.01, ...
        'ticks', 'outside', ...
        'ticksuffix', '%', ...
        'title', 'clay'), ...
      'baxis', struct(...
        'linewidth', 2, ...
        'min', 0.01, ...
        'ticks', 'outside', ...
        'ticksuffix', '%', ...
        'title', 'sand'), ...
      'caxis', struct(...
        'linewidth', 2, ...
        'min', 0.01, ...
        'ticks', 'outside', ...
        'ticksuffix', '%', ...
        'title', 'silt'), ...
      'sum', 100), ...
    'title', 'Simple Ternary Contour Plot with Python', ...
    'width', 1044);
response = plotly(data, struct('layout', layout));
plot_url = response.url
To make this plot, you'll need the the Ternplot package
from the MATLAB File exchange % add data % An + Bn + Cn = 1 % Z controls contour A = [0 .2 .2 .2 0 .6 .75 .9 0 1 .8 .3]; B = [1 .2 .4 .1 0 .4 .05 0 .8 0 .05 .3]; C = [0 .6 .4 .7 1 0 .2 .1 .2 0 .15 .4]; Z = [.1 .5 .1 .2 1 .8 .4 0 .1 .6 1 .7]; % create figure fig = figure; terncontour(A, B, C, Z) ternlabel('A', 'B', 'C') % Plotly pf = fig2plotly(fig, 'strip', false, 'filename', 'YOUR_FILENAME'); % Edit the contour fill and color in Plotly's online editor
To make this plot, you'll need the the Ternplot package
from the MATLAB File exchange % add data % An + Bn + Cn = 1 % Z controls contour A = [0 .2 .2 .2 0 .6 .75 .9 0 1 .8 .3]; B = [1 .2 .4 .1 0 .4 .05 0 .8 0 .05 .3]; C = [0 .6 .4 .7 1 0 .2 .1 .2 0 .15 .4]; Z = [.1 .5 .1 .2 1 .8 .4 0 .1 .6 1 .7]; % create figure fig = figure; terncontour(A, B, C, Z) ternlabel('A', 'B', 'C') % Plotly pf = fig2plotly(fig, 'strip', false, 'filename', 'YOUR_FILENAME'); % Edit the contour fill and color in Plotly's online editor
To make this plot, you'll need the the Ternplot package
from the MATLAB File exchange %%% Add Data % Alkalai Or OrA = [79.30 54.10 68.64 87.70 80.36 72.98 75.70 78.06 ... 71.88 66.01 60.82 75.47 66.27 66.72 76.57 76.31]; % Alkalai An AnA = [2.53 3.64 2.96 1.94 3.45 1.75 1.74 1.43 0.85 0.88 ... 0.88 0.86 1.02 1.24 0.71 0.50]; % Alkalai Ab AbA = [18.17 42.26 28.40 10.36 16.19 25.27 22.56 20.51 ... 27.27 33.11 38.30 23.67 32.71 32.04 22.72 23.19]; % Plagioclase Or OrP = [4.16 8.07 6.60 1.79 4.60 8.81 6.55 6.08 5.77 ... 6.45 21.69 7.78 6.75 6.85 6.34 6.02]; % Plagioclase An AnP = [71.81 28.29 39.49 87.15 70.96 26.72 33.76 35.96 ... 21.00 15.31 4.68 20.04 13.24 15.12 20.66 16.75]; % Plagioclase Ab AbP = [24.03 63.64 53.91 11.06 24.44 64.47 59.69 57.96 ... 73.23 78.24 73.63 72.18 80.01 78.03 73.00 77.23]; % Barium Or OrB = [29.03 68.60 70.91 11.00 34.00 44.47]; % Barium An AnB = [31.80 10.29 19.49 17.15 10.96 26.72]; % Barium Ab AbB = [29.03 60.60 50.91 11.00 24.00 44.47]; % Create figure with ternplot(a,b,c,'marker_specifications') % To plot multiple groups, turn hold on and repeat ternplot() fig = figure; ternplot(OrA, AnA, AbA, 'x', 'markersize', 6) hold on ternplot(OrP, AnP, AbP, 'o', 'markersize', 6) ternplot(OrB, AnB, AbB, '*', 'markersize', 6) ternlabel('Or','An','Ab') legend('Alkalai','Plagioclase','Barium') hold off % Plotly! pf = fig2plotly(fig, 'strip', false, 'filename', 'ADD_YOUR_FILENAME'); pf.plotly; % Edit the contour fill and color in Plotly's online editor
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.