forked from marcharper/python-ternary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.py
149 lines (126 loc) · 5.13 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
import math
import random
import matplotlib
from matplotlib import pyplot, gridspec
import ternary
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
if __name__ == '__main__':
## Boundary and Gridlines
scale = 40
figure, ternary_ax = ternary.figure(scale=scale)
left_kwargs = {'color': 'blue'}
right_kwargs = {'color': 'red'}
# Draw Boundary and Gridlines
ternary_ax.boundary(color="black", linewidth=2.0)
ternary_ax.gridlines(color="blue", multiple=5, left_kwargs=left_kwargs,
right_kwargs=right_kwargs)
# Draw Boundary and Gridlines
ternary_ax.boundary(color="black", linewidth=2.0)
ternary_ax.gridlines(color="blue", multiple=5)
# Set Axis labels and Title
fontsize = 20
ternary_ax.set_title("Simplex Boundary and Gridlines", fontsize=fontsize)
ternary_ax.left_axis_label("Left label $\\alpha^2$", fontsize=fontsize)
ternary_ax.right_axis_label("Right label $\\beta^2$", fontsize=fontsize)
ternary_ax.bottom_axis_label("Bottom label $\\Gamma - \\Omega$", fontsize=fontsize)
# Remove default Matplotlib Axes
ternary_ax.clear_matplotlib_ticks()
### Plot Various lines
scale = 40
figure, ternary_ax = ternary.figure(scale=scale)
# Draw Boundary and Gridlines
ternary_ax.boundary(color="black", linewidth=2.0)
ternary_ax.gridlines(color="blue", multiple=5)
# Set Axis labels and Title
fontsize = 20
ternary_ax.set_title("Various Lines", fontsize=20)
ternary_ax.left_axis_label("Left label $\\alpha^2$", fontsize=fontsize)
ternary_ax.right_axis_label("Right label $\\beta^2$", fontsize=fontsize)
ternary_ax.bottom_axis_label("Bottom label $\\Gamma - \\Omega$", fontsize=fontsize)
# Draw lines parallel to the axes
ternary_ax.horizontal_line(16)
ternary_ax.left_parallel_line(10, linewidth=2., color='red', linestyle="--")
ternary_ax.right_parallel_line(20, linewidth=3., color='blue')
# Draw an arbitrary line
p1 = (12,8,10)
p2 = (2, 26, 2)
ternary_ax.line(p1, p2, linewidth=3., marker='s', color='green', linestyle=":")
### Scatter Plot
scale = 40
figure, ternary_ax = ternary.figure(scale=scale)
ternary_ax.set_title("Scatter Plot", fontsize=20)
ternary_ax.boundary(color="black", linewidth=2.0)
ternary_ax.gridlines(multiple=5, color="blue")
# Plot a few different styles with a legend
points = random_points(30, scale=scale)
ternary_ax.scatter(points, marker='s', color='red', label="Red Squares")
points = random_points(30, scale=scale)
ternary_ax.scatter(points, marker='D', color='green', label="Green Diamonds")
ternary_ax.legend()
## Sample trajectory plot
figure, tax = ternary.figure(scale=1.0)
tax.boundary(color='black')
tax.set_title("Plotting of sample trajectory data", fontsize=20)
points = []
with open("curve.txt") as handle:
for line in handle:
points.append(map(float, line.split(' ')))
tax.gridlines(multiple=0.2, color="black")
tax.plot(points, linewidth=2.0, label="Curve")
tax.legend()
## Sample colored trajectory plot
figure, tax = ternary.figure(scale=1.0)
tax.boundary(color='black')
tax.set_title("Plotting of sample trajectory data", fontsize=20)
points = []
with open("curve.txt") as handle:
for line in handle:
points.append(map(float, line.split(' ')))
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()
## Heatmap roundup
# Careful -- these can use a lot of RAM!
scale = 60
function = shannon_entropy
pyplot.figure()
gs = gridspec.GridSpec(2,2)
ax = pyplot.subplot(gs[0,0])
figure, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=True, style="triangular")
tax.boundary(scale=scale+1, color='black')
tax.set_title("Triangular with Boundary")
ax = pyplot.subplot(gs[0,1])
figure, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=False, style="t")
tax.boundary(scale=scale+1, color='black')
tax.set_title("Triangular without Boundary")
ax = pyplot.subplot(gs[1,0])
figure, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=True, style="hexagonal")
tax.boundary(scale=scale, color='black')
tax.set_title("Hexagonal with Boundary")
ax = pyplot.subplot(gs[1,1])
figure, tax = ternary.figure(ax=ax, scale=scale)
tax.heatmapf(function, boundary=False, style="h")
tax.boundary(scale=scale, color='black')
tax.set_title("Hexagonal without Boundary")
pyplot.show()