-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathternaxes.m
115 lines (93 loc) · 3.57 KB
/
ternaxes.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
% TERNAXES create ternary axis
% HOLD_STATE = TERNAXES(MAJORS) creates a ternary axis system using the system
% defaults and with MAJORS major tickmarks.
% Author: Carl Sandrock 20050211
% To Do
% Modifications
% 20160405 (SA) Added lines to change the order/direction of axes (i.e.
% clockwise or counter-clockwise) cooresponding to user-specified
% option on terncoords
% Modifiers
% (CS) Carl Sandrock
% (SA) Shahab Afshari
function [hold_state, cax, next] = ternaxes(majors)
if nargin < 1
majors = 10;
end
% TODO: Handle these as options
direction = 'clockwise';
percentage = false;
%TODO: Get a better way of offsetting the labels
xoffset = 0.25;
yoffset = 0.01;
% get hold state
cax = newplot;
next = lower(get(cax,'NextPlot'));
hold_state = ishold;
% get x-axis text color so grid is in same color
tc = get(cax,'xcolor');
ls = get(cax,'gridlinestyle');
% Hold on to current Text defaults, reset them to the
% Axes' font attributes so tick marks use them.
fAngle = get(cax, 'DefaultTextFontAngle');
fName = get(cax, 'DefaultTextFontName');
fSize = get(cax, 'DefaultTextFontSize');
fWeight = get(cax, 'DefaultTextFontWeight');
fUnits = get(cax, 'DefaultTextUnits');
set(cax, 'DefaultTextFontAngle', get(cax, 'FontAngle'), ...
'DefaultTextFontName', get(cax, 'FontName'), ...
'DefaultTextFontSize', get(cax, 'FontSize'), ...
'DefaultTextFontWeight', get(cax, 'FontWeight'), ...
'DefaultTextUnits','data')
% only do grids if hold is off
if ~hold_state
%plot axis lines
hold on;
plot ([0 1 0.5 0],[0 0 sin(1/3*pi) 0], 'color', tc, 'linewidth',1,...
'handlevisibility','off');
set(gca, 'visible', 'off');
% plot background if necessary
if ~ischar(get(cax,'color'))
patch('xdata', [0 1 0.5 0], 'ydata', [0 0 sin(1/3*pi) 0], ...
'edgecolor',tc,'facecolor',get(gca,'color'),...
'handlevisibility','off');
end
% Generate labels
majorticks = linspace(0, 1, majors + 1);
majorticks = majorticks(1:end-1);
if percentage
multiplier = 100;
else
multiplier = 1;
end
if ~strcmp(direction, 'clockwise')
labels = num2str(majorticks'*multiplier);
else
labels = num2str(majorticks(end:-1:1)'*multiplier);
end
zerocomp = zeros(size(majorticks)); % represents zero composition
% Plot right labels (no c - only b a)
[lxc, lyc] = terncoords(1-majorticks, majorticks, zerocomp);
text(lxc+0.05, lyc-0.025, [repmat(' ', length(labels), 1) labels]);
% Plot bottom labels (no b - only a c)
[lxb, lyb] = terncoords(majorticks, zerocomp, 1-majorticks); % fB = 1-fA
text(lxb-0.115, lyb-0.065, labels, 'VerticalAlignment', 'Top');
% Plot left labels (no a, only c b)
[lxa, lya] = terncoords(zerocomp, 1-majorticks, majorticks);
text(lxa-0.035, lya+0.09, labels);
nlabels = length(labels)-1;
for i = 1:nlabels
plot([lxa(i+1) lxb(nlabels - i + 2)], [lya(i+1) lyb(nlabels - i + 2)], ls, 'color', tc, 'linewidth',0.25,...
'handlevisibility','off');
plot([lxb(i+1) lxc(nlabels - i + 2)], [lyb(i+1) lyc(nlabels - i + 2)], ls, 'color', tc, 'linewidth',0.25,...
'handlevisibility','off');
plot([lxc(i+1) lxa(nlabels - i + 2)], [lyc(i+1) lya(nlabels - i + 2)], ls, 'color', tc, 'linewidth',0.25,...
'handlevisibility','off');
end;
end;
% Reset defaults
set(cax, 'DefaultTextFontAngle', fAngle , ...
'DefaultTextFontName', fName , ...
'DefaultTextFontSize', fSize, ...
'DefaultTextFontWeight', fWeight, ...
'DefaultTextUnits', fUnits );