22import os
33import random
44
5- import matplotlib
6- from matplotlib import pyplot , gridspec
5+ from matplotlib import gridspec
6+ from matplotlib import pyplot as plt
77
88import ternary
99
10+
1011def load_sample_trajectory_data (filename = "curve.txt" , directory = "sample_data" ):
1112 full_filename = os .path .join (directory , filename )
1213 points = []
@@ -15,6 +16,7 @@ def load_sample_trajectory_data(filename="curve.txt", directory="sample_data"):
1516 points .append (list (map (float , line .split (' ' ))))
1617 return points
1718
19+
1820def load_sample_heatmap_data (filename = "sample_heatmap_data.txt" ,
1921 directory = "sample_data" ):
2022 """Loads sample heatmap data."""
@@ -27,13 +29,15 @@ def load_sample_heatmap_data(filename="sample_heatmap_data.txt",
2729 data [(int (i ), int (j ), int (k ))] = float (v )
2830 return data
2931
32+
3033def generate_random_heatmap_data (scale = 5 ):
3134 from ternary .helpers import simplex_iterator
3235 d = dict ()
33- for (i ,j , k ) in simplex_iterator (scale ):
34- d [(i ,j )] = random .random ()
36+ for (i , j , k ) in simplex_iterator (scale ):
37+ d [(i , j )] = random .random ()
3538 return d
3639
40+
3741def shannon_entropy (p ):
3842 """Computes the Shannon Entropy at a distribution in the simplex."""
3943 s = 0.
@@ -44,6 +48,7 @@ def shannon_entropy(p):
4448 continue
4549 return - 1. * s
4650
51+
4752def random_points (num_points = 25 , scale = 40 ):
4853 points = []
4954 for i in range (num_points ):
@@ -53,28 +58,29 @@ def random_points(num_points=25, scale=40):
5358 points .append ((x ,y ,z ))
5459 return points
5560
61+
5662def random_heatmap (scale = 4 ):
5763 d = generate_random_heatmap_data (scale )
58- figure , tax = ternary .figure (scale = scale , ax = ax )
64+ fig , tax = ternary .figure (scale = scale , ax = ax )
5965 tax .heatmap (d , style = "t" )
6066 tax .boundary (color = 'black' )
6167 tax .set_title ("Heatmap Test: Triangular" )
6268
63- ax = pyplot .subplot (gs [0 ,1 ])
64- figure , tax = ternary .figure (scale = scale , ax = ax )
69+ ax = plt .subplot (gs [0 ,1 ])
70+ fig , tax = ternary .figure (scale = scale , ax = ax )
6571 tax .heatmap (d , style = "d" )
6672 tax .boundary (color = 'black' )
6773 tax .set_title ("Heatmap Test Dual" )
68- pyplot .show ()
74+ plt .show ()
6975
7076if __name__ == '__main__' :
7177 # Show Coordinates
7278 scale = 3
73- figure , tax = ternary .figure (scale = scale , permutation = "120" )
74- points_lists = [[(0 ,0 , 3 ), (1 ,0 , 2 ), (2 ,0 , 1 )],
75- [(3 ,0 , 0 ), (2 ,1 , 0 ), (1 ,2 , 0 )],
76- [(0 ,3 , 0 ), (0 ,2 , 1 ), (0 ,1 , 2 )],
77- [(1 ,1 , 1 )]]
79+ fig , tax = ternary .figure (scale = scale , permutation = "120" )
80+ points_lists = [[(0 , 0 , 3 ), (1 , 0 , 2 ), (2 , 0 , 1 )],
81+ [(3 , 0 , 0 ), (2 , 1 , 0 ), (1 , 2 , 0 )],
82+ [(0 , 3 , 0 ), (0 , 2 , 1 ), (0 , 1 , 2 )],
83+ [(1 , 1 , 1 )]]
7884 colors = ['b' , 'r' , 'g' , 'black' ]
7985 markers = ['o' , 'v' , '*' , 'd' ]
8086 for i , points in enumerate (points_lists ):
@@ -85,77 +91,81 @@ def random_heatmap(scale=4):
8591
8692 ## Boundary and Gridlines
8793 scale = 40
88- figure , ternary_ax = ternary .figure (scale = scale )
94+ fig , tax = ternary .figure (scale = scale )
8995
9096 left_kwargs = {'color' : 'blue' }
9197 right_kwargs = {'color' : 'red' }
9298
9399 # Draw Boundary and Gridlines
94- ternary_ax .boundary (linewidth = 2.0 )
95- ternary_ax .gridlines (color = "blue" , multiple = 5 , left_kwargs = left_kwargs ,
100+ tax .boundary (linewidth = 2.0 )
101+ tax .gridlines (color = "blue" , multiple = 5 , left_kwargs = left_kwargs ,
96102 right_kwargs = right_kwargs )
97103
98104 # Draw Boundary and Gridlines
99- ternary_ax .boundary (linewidth = 2.0 )
100- ternary_ax .gridlines (color = "black" , multiple = 5 )
101- ternary_ax .gridlines (color = "blue" , multiple = 1 , linewidth = 0.5 )
105+ tax .boundary (linewidth = 2.0 )
106+ tax .gridlines (color = "black" , multiple = 5 )
107+ tax .gridlines (color = "blue" , multiple = 1 , linewidth = 0.5 )
102108
103109 # Set Axis labels and Title
104110 fontsize = 20
105- ternary_ax .set_title ("Simplex Boundary and Gridlines" , fontsize = fontsize )
106- ternary_ax .left_axis_label ("Left label $\\ alpha^2$" , fontsize = fontsize )
107- ternary_ax .right_axis_label ("Right label $\\ beta^2$" , fontsize = fontsize )
108- ternary_ax .bottom_axis_label ("Bottom label $\\ Gamma - \\ Omega$" , fontsize = fontsize )
111+ tax .set_title ("Simplex Boundary and Gridlines" , fontsize = fontsize )
112+ tax .left_axis_label ("Left label $\\ alpha^2$" , fontsize = fontsize )
113+ tax .right_axis_label ("Right label $\\ beta^2$" , fontsize = fontsize )
114+ tax .bottom_axis_label ("Bottom label $\\ Gamma - \\ Omega$" ,
115+ fontsize = fontsize )
116+ tax .get_axes ().axis ('off' )
109117
110- ternary_ax .ticks (axis = 'lbr' , clockwise = True , multiple = 5 , linewidth = 1 )
118+ tax .ticks (axis = 'lbr' , clockwise = True , multiple = 5 , linewidth = 1 )
111119
112120 # Remove default Matplotlib Axes
113- ternary_ax .clear_matplotlib_ticks ()
121+ tax .clear_matplotlib_ticks ()
114122
115123 ### Plot Various lines
116124 scale = 40
117- figure , ternary_ax = ternary .figure (scale = scale )
125+ fig , tax = ternary .figure (scale = scale )
118126
119127 # Draw Boundary and Gridlines
120- ternary_ax .boundary (linewidth = 2.0 )
121- ternary_ax .gridlines (color = "blue" , multiple = 5 )
128+ tax .boundary (linewidth = 2.0 )
129+ tax .gridlines (color = "blue" , multiple = 5 )
122130
123131 # Set Axis labels and Title
124132 fontsize = 20
125- ternary_ax .set_title ("Various Lines" , fontsize = 20 )
126- ternary_ax .left_axis_label ("Left label $\\ alpha^2$" , fontsize = fontsize )
127- ternary_ax .right_axis_label ("Right label $\\ beta^2$" , fontsize = fontsize )
128- ternary_ax .bottom_axis_label ("Bottom label $\\ Gamma - \\ Omega$" , fontsize = fontsize )
133+ tax .set_title ("Various Lines" , fontsize = 20 )
134+ tax .left_axis_label ("Left label $\\ alpha^2$" , fontsize = fontsize )
135+ tax .right_axis_label ("Right label $\\ beta^2$" , fontsize = fontsize )
136+ tax .bottom_axis_label ("Bottom label $\\ Gamma - \\ Omega$" ,
137+ fontsize = fontsize )
129138
130139 # Draw lines parallel to the axes
131- ternary_ax .horizontal_line (16 )
132- ternary_ax .left_parallel_line (10 , linewidth = 2. , color = 'red' , linestyle = "--" )
133- ternary_ax .right_parallel_line (20 , linewidth = 3. , color = 'blue' )
140+ tax .horizontal_line (16 )
141+ tax .left_parallel_line (10 , linewidth = 2. , color = 'red' , linestyle = "--" )
142+ tax .right_parallel_line (20 , linewidth = 3. , color = 'blue' )
134143 # Draw an arbitrary line
135- p1 = (12 ,8 , 10 )
144+ p1 = (12 , 8 , 10 )
136145 p2 = (2 , 26 , 2 )
137- ternary_ax .line (p1 , p2 , linewidth = 3. , marker = 's' , color = 'green' , linestyle = ":" )
146+ tax .line (p1 , p2 , linewidth = 3. , marker = 's' , color = 'green' , linestyle = ":" )
138147
139- ternary_ax .clear_matplotlib_ticks ()
140- ternary_ax .ticks (axis = 'lbr' , multiple = 5 , linewidth = 1 )
148+ tax .get_axes ().axis ('off' )
149+ tax .clear_matplotlib_ticks ()
150+ tax .ticks (axis = 'lbr' , multiple = 5 , linewidth = 1 )
141151
142152 ### Scatter Plot
143153 scale = 40
144- figure , ternary_ax = ternary .figure (scale = scale )
145- ternary_ax .set_title ("Scatter Plot" , fontsize = 20 )
146- ternary_ax .boundary (linewidth = 2.0 )
147- ternary_ax .gridlines (multiple = 5 , color = "blue" )
154+ fig , tax = ternary .figure (scale = scale )
155+ tax .set_title ("Scatter Plot" , fontsize = 20 )
156+ tax .boundary (linewidth = 2.0 )
157+ tax .gridlines (multiple = 5 , color = "blue" )
148158 # Plot a few different styles with a legend
149159 points = random_points (30 , scale = scale )
150- ternary_ax .scatter (points , marker = 's' , color = 'red' , label = "Red Squares" )
160+ tax .scatter (points , marker = 's' , color = 'red' , label = "Red Squares" )
151161 points = random_points (30 , scale = scale )
152- ternary_ax .scatter (points , marker = 'D' , color = 'green' , label = "Green Diamonds" )
153- ternary_ax .legend ()
154- ternary_ax .clear_matplotlib_ticks ()
155- ternary_ax .ticks (axis = 'lbr' , multiple = 5 , linewidth = 1 )
162+ tax .scatter (points , marker = 'D' , color = 'green' , label = "Green Diamonds" )
163+ tax .legend ()
164+ tax .clear_matplotlib_ticks ()
165+ tax .ticks (axis = 'lbr' , multiple = 5 , linewidth = 1 )
156166
157167 ## Sample trajectory plot
158- figure , tax = ternary .figure (scale = 1.0 )
168+ fig , tax = ternary .figure (scale = 1.0 )
159169 tax .boundary ()
160170 tax .set_title ("Plotting of sample trajectory data" , fontsize = 20 )
161171 points = load_sample_trajectory_data ()
@@ -164,7 +174,7 @@ def random_heatmap(scale=4):
164174 tax .legend ()
165175
166176 ## Sample colored trajectory plot
167- figure , tax = ternary .figure (scale = 1.0 )
177+ fig , tax = ternary .figure (scale = 1.0 )
168178 tax .boundary ()
169179 tax .set_title ("Plotting of sample trajectory data" , fontsize = 20 )
170180 points = load_sample_trajectory_data ()
@@ -176,47 +186,47 @@ def random_heatmap(scale=4):
176186 tax .clear_matplotlib_ticks ()
177187 tax .ticks (axis = 'lbr' , linewidth = 1 , multiple = 0.1 )
178188
179- pyplot .show ()
189+ plt .show ()
180190
181191 ## Heatmap roundup
182192 # Careful -- these can use a lot of RAM!
183- scale = 60
193+ scale = 30
184194 function = shannon_entropy
185- pyplot . figure ()
186- gs = gridspec .GridSpec (2 ,3 )
195+ plt . fig ()
196+ gs = gridspec .GridSpec (2 , 3 )
187197
188- ax = pyplot .subplot (gs [0 ,0 ])
189- figure , tax = ternary .figure (ax = ax , scale = scale )
198+ ax = plt .subplot (gs [0 , 0 ])
199+ fig , tax = ternary .figure (ax = ax , scale = scale )
190200 tax .heatmapf (function , boundary = True , style = "triangular" )
191201 tax .boundary ()
192202 tax .set_title ("Triangular with Boundary" )
193203
194- ax = pyplot .subplot (gs [1 ,0 ])
195- figure , tax = ternary .figure (ax = ax , scale = scale )
204+ ax = plt .subplot (gs [1 , 0 ])
205+ fig , tax = ternary .figure (ax = ax , scale = scale )
196206 tax .heatmapf (function , boundary = False , style = "t" )
197207 tax .boundary ()
198208 tax .set_title ("Triangular without Boundary" )
199209
200- ax = pyplot .subplot (gs [0 ,1 ])
201- figure , tax = ternary .figure (ax = ax , scale = scale )
210+ ax = plt .subplot (gs [0 , 1 ])
211+ fig , tax = ternary .figure (ax = ax , scale = scale )
202212 tax .heatmapf (function , boundary = True , style = "dual-triangular" )
203213 tax .boundary ()
204214 tax .set_title ("Dual Triangular with Boundary" )
205215
206- ax = pyplot .subplot (gs [1 ,1 ])
207- figure , tax = ternary .figure (ax = ax , scale = scale )
216+ ax = plt .subplot (gs [1 , 1 ])
217+ fig , tax = ternary .figure (ax = ax , scale = scale )
208218 tax .heatmapf (function , boundary = False , style = "d" )
209219 tax .boundary ()
210220 tax .set_title ("Dual Triangular without Boundary" )
211221
212- ax = pyplot .subplot (gs [0 ,2 ])
213- figure , tax = ternary .figure (ax = ax , scale = scale )
222+ ax = plt .subplot (gs [0 , 2 ])
223+ fig , tax = ternary .figure (ax = ax , scale = scale )
214224 tax .heatmapf (function , boundary = True , style = "hexagonal" )
215225 tax .boundary ()
216226 tax .set_title ("Hexagonal with Boundary" )
217227
218- ax = pyplot .subplot (gs [1 ,2 ])
219- figure , tax = ternary .figure (ax = ax , scale = scale )
228+ ax = plt .subplot (gs [1 , 2 ])
229+ fig , tax = ternary .figure (ax = ax , scale = scale )
220230 tax .heatmapf (function , boundary = False , style = "h" )
221231 tax .boundary ()
222232 tax .set_title ("Hexagonal without Boundary" )
@@ -225,24 +235,24 @@ def random_heatmap(scale=4):
225235 # Careful -- these can use a lot of RAM!
226236 scale = 60
227237 data = load_sample_heatmap_data ()
228- pyplot . figure ()
238+ plt . fig ()
229239 gs = gridspec .GridSpec (1 , 3 )
230- ax = pyplot .subplot (gs [0 ,0 ])
231- figure , tax = ternary .figure (ax = ax , scale = scale )
240+ ax = plt .subplot (gs [0 , 0 ])
241+ fig , tax = ternary .figure (ax = ax , scale = scale )
232242 tax .heatmap (data , style = "dual-triangular" )
233243 tax .boundary ()
234244 tax .set_title ("Dual-Triangular Heatmap from Data" )
235245
236- ax = pyplot .subplot (gs [0 ,1 ])
237- figure , tax = ternary .figure (ax = ax , scale = scale )
246+ ax = plt .subplot (gs [0 , 1 ])
247+ fig , tax = ternary .figure (ax = ax , scale = scale )
238248 tax .heatmap (data , style = "triangular" )
239249 tax .boundary ()
240250 tax .set_title ("Triangular Heatmap from Data" )
241251
242- ax = pyplot .subplot (gs [0 ,2 ])
243- figure , tax = ternary .figure (ax = ax , scale = scale )
252+ ax = plt .subplot (gs [0 , 2 ])
253+ fig , tax = ternary .figure (ax = ax , scale = scale )
244254 tax .heatmap (data , style = "hexagonal" )
245255 tax .boundary ()
246256 tax .set_title ("Hexagonal Heatmap from Data" )
247257
248- pyplot .show ()
258+ plt .show ()
0 commit comments