Skip to content

Commit b1db8a1

Browse files
committed
added barcode visualization
1 parent f234f54 commit b1db8a1

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

persim/visuals.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,44 @@ def plot_diagrams(
174174
if show is True:
175175
plt.show()
176176

177+
def plot_a_bar(p, q, c='b', linestyle='-'):
178+
plt.plot([p[0], q[0]], [p[1], q[1]], c=c, linestyle=linestyle, linewidth=1)
177179

180+
def plot_barcode(diagrams, show=False):
178181

182+
if not isinstance(diagrams, list):
183+
# Must have diagrams as a list for processing downstream
184+
diagrams = [diagrams]
185+
186+
dimensions = len(diagrams)
187+
188+
# barcodes
189+
for dim in range(dimensions):
190+
number_of_bars = len(diagrams[dim])
191+
print "Number of bars in dimension %d: %d" % (dim, number_of_bars)
192+
193+
number_of_bars_fin = 0
194+
number_of_bars_inf = 0
195+
196+
if number_of_bars > 0:
197+
fig = plt.figure()
198+
ax = plt.subplot("111")
199+
200+
for i in range(number_of_bars):
201+
birth = [diagrams[dim][i, 0], i]
202+
death = [diagrams[dim][i, 1], i]
203+
maximum_death = np.nanmax(diagrams[dim][diagrams[dim] < 1E308].flatten())
204+
if np.isinf(death[0]):
205+
number_of_bars_inf += 1
206+
plot_a_bar(birth, [1.05 * maximum_death, i])
207+
plt.scatter([1.05 * maximum_death], [i], c='b', s=10, marker='>')
208+
else:
209+
number_of_bars_fin += 1
210+
plot_a_bar(birth, death)
211+
## the line below is to plot a vertical red line showing the maximal finite bar length
212+
plt.plot([maximum_death, maximum_death], [0, number_of_bars - 1], c='r', linestyle='--', linewidth=0.5)
213+
214+
if show is True:
215+
title = "%d-dimensional bars: %d finite, %d infinite" % (dim, number_of_bars_fin, number_of_bars_inf)
216+
ax.set_title(title, fontsize=10)
217+
plt.show()

0 commit comments

Comments
 (0)