forked from marcharper/python-ternary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.py
258 lines (215 loc) · 8.14 KB
/
examples.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import math
import os
import random
from matplotlib import gridspec
from matplotlib import pyplot as plt
import ternary
def load_sample_trajectory_data(filename="curve.txt", directory="sample_data"):
full_filename = os.path.join(directory, filename)
points = []
with open(full_filename) as handle:
for line in handle:
points.append(list(map(float, line.split(' '))))
return points
def load_sample_heatmap_data(filename="sample_heatmap_data.txt",
directory="sample_data"):
"""Loads sample heatmap data."""
full_filename = os.path.join(directory, filename)
data = dict()
handle = open(full_filename)
for line in handle:
line = line.strip()
i, j, k, v = line.split(' ')
data[(int(i), int(j), int(k))] = float(v)
return data
def generate_random_heatmap_data(scale=5):
from ternary.helpers import simplex_iterator
d = dict()
for (i, j, k) in simplex_iterator(scale):
d[(i, j)] = random.random()
return d
def shannon_entropy(p):
"""Computes the Shannon Entropy at a distribution in the simplex."""
s = 0.
for i in range(len(p)):
try:
s += p[i] * math.log(p[i])
except ValueError:
continue
return -1.*s
def random_points(num_points=25, scale=40):
points = []
for i in range(num_points):
x = random.randint(1, scale)
y = random.randint(0, scale - x)
z = scale - x - y
points.append((x,y,z))
return points
def random_heatmap(scale=4):
d = generate_random_heatmap_data(scale)
fig, tax = ternary.figure(scale=scale, ax=ax)
tax.heatmap(d, style="t")
tax.boundary(color='black')
tax.set_title("Heatmap Test: Triangular")
ax = plt.subplot(gs[0,1])
fig, tax = ternary.figure(scale=scale, ax=ax)
tax.heatmap(d, style="d")
tax.boundary(color='black')
tax.set_title("Heatmap Test Dual")
plt.show()
if __name__ == '__main__':
# Show Coordinates
scale = 3
fig, tax = ternary.figure(scale=scale, permutation="120")
points_lists = [[(0, 0, 3), (1, 0, 2), (2, 0, 1)],
[(3, 0, 0), (2, 1, 0), (1, 2, 0)],
[(0, 3, 0), (0, 2, 1), (0, 1, 2)],
[(1, 1, 1)]]
colors = ['b', 'r', 'g', 'black']
markers = ['o', 'v', '*', 'd']
for i, points in enumerate(points_lists):
for point in points:
tax.scatter([tuple(point)], color=colors[i], marker=markers[i])
tax.annotate("".join(map(str, point)), tuple(point), color=colors[i])
tax.gridlines(multiple=1.)
## Boundary and Gridlines
scale = 40
fig, tax= ternary.figure(scale=scale)
left_kwargs = {'color': 'blue'}
right_kwargs = {'color': 'red'}
# Draw Boundary and Gridlines
tax.boundary(linewidth=2.0)
tax.gridlines(color="blue", multiple=5, left_kwargs=left_kwargs,
right_kwargs=right_kwargs)
# Draw Boundary and Gridlines
tax.boundary(linewidth=2.0)
tax.gridlines(color="black", multiple=5)
tax.gridlines(color="blue", multiple=1, linewidth=0.5)
# Set Axis labels and Title
fontsize = 20
tax.set_title("Simplex Boundary and Gridlines", fontsize=fontsize)
tax.left_axis_label("Left label $\\alpha^2$", fontsize=fontsize)
tax.right_axis_label("Right label $\\beta^2$", fontsize=fontsize)
tax.bottom_axis_label("Bottom label $\\Gamma - \\Omega$",
fontsize=fontsize)
tax.get_axes().axis('off')
tax.ticks(axis='lbr', clockwise=True, multiple=5, linewidth=1)
# Remove default Matplotlib Axes
tax.clear_matplotlib_ticks()
### Plot Various lines
scale = 40
fig, tax = ternary.figure(scale=scale)
# Draw Boundary and Gridlines
tax.boundary(linewidth=2.0)
tax.gridlines(color="blue", multiple=5)
# Set Axis labels and Title
fontsize = 20
tax.set_title("Various Lines", fontsize=20)
tax.left_axis_label("Left label $\\alpha^2$", fontsize=fontsize)
tax.right_axis_label("Right label $\\beta^2$", fontsize=fontsize)
tax.bottom_axis_label("Bottom label $\\Gamma - \\Omega$",
fontsize=fontsize)
# Draw lines parallel to the axes
tax.horizontal_line(16)
tax.left_parallel_line(10, linewidth=2., color='red', linestyle="--")
tax.right_parallel_line(20, linewidth=3., color='blue')
# Draw an arbitrary line
p1 = (12, 8, 10)
p2 = (2, 26, 2)
tax.line(p1, p2, linewidth=3., marker='s', color='green', linestyle=":")
tax.get_axes().axis('off')
tax.clear_matplotlib_ticks()
tax.ticks(axis='lbr', multiple=5, linewidth=1)
### Scatter Plot
scale = 40
fig, tax= ternary.figure(scale=scale)
tax.set_title("Scatter Plot", fontsize=20)
tax.boundary(linewidth=2.0)
tax.gridlines(multiple=5, color="blue")
# Plot a few different styles with a legend
points = random_points(30, scale=scale)
tax.scatter(points, marker='s', color='red', label="Red Squares")
points = random_points(30, scale=scale)
tax.scatter(points, marker='D', color='green', label="Green Diamonds")
tax.legend()
tax.clear_matplotlib_ticks()
tax.ticks(axis='lbr', multiple=5, linewidth=1)
## Sample trajectory plot
fig, tax = ternary.figure(scale=1.0)
tax.boundary()
tax.set_title("Plotting of sample trajectory data", fontsize=20)
points = load_sample_trajectory_data()
tax.gridlines(multiple=0.2, color="black")
tax.plot(points, linewidth=2.0, label="Curve")
tax.legend()
## Sample colored trajectory plot
fig, tax = ternary.figure(scale=1.0)
tax.boundary()
tax.set_title("Plotting of sample trajectory data", fontsize=20)
points = load_sample_trajectory_data()
tax.gridlines(multiple=0.2, color="black")
tax.plot_colored_trajectory(points, linewidth=2.0)
points = [(y,z,x) for (x,y,z) in points]
tax.plot_colored_trajectory(points, cmap="hsv", linewidth=2.0)
tax.legend()
tax.clear_matplotlib_ticks()
tax.ticks(axis='lbr', linewidth=1, multiple=0.1)
plt.show()
## Heatmap roundup
# Careful -- these can use a lot of RAM!
scale = 30
function = shannon_entropy
plt.fig()
gs = gridspec.GridSpec(2, 3)
ax = plt.subplot(gs[0, 0])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=True, style="triangular")
tax.boundary()
tax.set_title("Triangular with Boundary")
ax = plt.subplot(gs[1, 0])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=False, style="t")
tax.boundary()
tax.set_title("Triangular without Boundary")
ax = plt.subplot(gs[0, 1])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=True, style="dual-triangular")
tax.boundary()
tax.set_title("Dual Triangular with Boundary")
ax = plt.subplot(gs[1, 1])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=False, style="d")
tax.boundary()
tax.set_title("Dual Triangular without Boundary")
ax = plt.subplot(gs[0, 2])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=True, style="hexagonal")
tax.boundary()
tax.set_title("Hexagonal with Boundary")
ax = plt.subplot(gs[1, 2])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=False, style="h")
tax.boundary()
tax.set_title("Hexagonal without Boundary")
## Heatmaps from data
# Careful -- these can use a lot of RAM!
scale = 60
data = load_sample_heatmap_data()
plt.fig()
gs = gridspec.GridSpec(1, 3)
ax = plt.subplot(gs[0, 0])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmap(data, style="dual-triangular")
tax.boundary()
tax.set_title("Dual-Triangular Heatmap from Data")
ax = plt.subplot(gs[0, 1])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmap(data, style="triangular")
tax.boundary()
tax.set_title("Triangular Heatmap from Data")
ax = plt.subplot(gs[0, 2])
fig, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmap(data, style="hexagonal")
tax.boundary()
tax.set_title("Hexagonal Heatmap from Data")
plt.show()