This answer makes use of itertools.groupby. We start by flipping the array so when we iterate over it we go bottom to top, and for each row we use groupby to capture the continuous groups and their size. Based on that the first loop builds the dots and lines.
Then we transpose the array and iterate over the columns doing basically the same thing but swapping the x and y.
You could almost surely turn this into a function with a bit more thought, to basically handle rows and columns without almost duplicating the code.
import numpy as np
from itertools import groupby
import matplotlib.pyplot as plt
mat = np.array([[0.2, 0.2, 0.1, 0.1, 0.1],
[0.2, 0.2, 0.1, 0.1, 0.0],
[0.2, 0.2, 0.1, 0.1, 0.0],
[0.2, 0.1, 0.1, 0.0, 0.0]])
mat = np.flip(mat,axis=0)
for r in range(mat.shape[0]-1,-1,-1):
c = 0
l = [(key, len(list(it))) for (key, it) in groupby(mat[r])]
for p in l:
if p[1]==1:
plt.plot(c,r, color='red',
linestyle='solid',
marker='o',
markerfacecolor='black',
markeredgecolor='black',
markersize=12)
c+=1
else:
x = []
y = []
for i in range(p[1]):
x.append(c)
y.append(r)
c+=1
plt.plot(x, y, color='red',
linestyle='solid',
marker='o',
markerfacecolor='black',
markeredgecolor='black',
markersize=12)
r+=1
mat = np.transpose(mat)
for r in range(mat.shape[1],-1,-1):
c = 0
l = [(key, len(list(it))) for (key, it) in groupby(mat[r])]
for p in l:
if p[1]==1:
plt.plot(r,c,
color='red',
linestyle='solid',
marker='o',
markerfacecolor='black',
markeredgecolor='black',
markersize=12)
c+=1
else:
x = []
y = []
for i in range(p[1]):
x.append(c)
y.append(r)
c+=1
plt.plot(y, x,
color='red',
linestyle='solid',
marker='o',
markerfacecolor='black',
markeredgecolor='black',
markersize=12)
r+=1
Output
