Good answer by @michael_j_ward (+1). Just to give an alternative using the usual chart commands, this is also a possible solution.
The first thing you need to consider is that you need to convert your data from what you have:
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0]
In what you want to build:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0]
nx, ny = [x[0]], [y[0]]
for i in range(0, len(y)-1):
nx.append(x[i])
ny.append(y[i])
if y[i] == 0 and y[i+1] == 1:
nx.append(x[i])
ny.append(y[i+1])
elif y[i] == 1 and y[i+1] == 0:
nx.append(x[i+1])
ny.append(y[i])
nx.append(x[-1])
ny.append(y[-1])
plt.plot(x, y, c='blue', label='Your data')
plt.plot(nx, ny, c='black', linestyle='--', label='New data')
plt.ylim(-1, 2)
plt.yticks([0, 1], ['0', '1'])
plt.legend()
plt.show()
the comparison of which is as follows:

, :
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0]
nx, ny = [x[0]], [y[0]]
for i in range(0, len(y)-1):
nx.append(x[i])
ny.append(y[i])
if y[i] == 0 and y[i+1] == 1:
nx.append(x[i])
ny.append(y[i+1])
elif y[i] == 1 and y[i+1] == 0:
nx.append(x[i+1])
ny.append(y[i])
nx.append(x[-1])
ny.append(y[-1])
for i in range(1,len(ny)):
if ny[i] == 1 and ny[i-1] == 1:
choice = 'r'
else:
choice = 'g'
plt.plot([nx[i-1], nx[i]], [ny[i-1], ny[i]], c=choice)
plt.ylim(-1, 2)
plt.yticks([0, 1], ['0', '1'])
plt.grid()
plt.show()
:
