If you have a very long x-array, you can combine numpy's np.diff with np.nonzero to calculate the indices. np.diff would calculate the subsequent differences, which can be compared to a threshold. np.nonzero will return all indices where the comparison results in True. Looping through these indices lets you draw each part separately.
from matplotlib import pyplot as plt
import numpy as np
x = [0, 5, 10, 15, 100, 105, 110, 115]
y = [15, 10, 5, 0, 115, 110, 105, 100]
threshold = 20
indices = np.nonzero(np.diff(x) >= threshold)[0] + 1
for i0, i1 in zip(np.append(0, indices), np.append(indices, len(x))):
plt.plot(x[i0:i1], y[i0:i1], '-go')
plt.show()
Here is a more elaborate example:
from matplotlib import pyplot as plt
import numpy as np
N = 30
x = (np.random.randint(1, 5, N) * 5).cumsum()
y = np.random.randint(0, 10, N) * 5
plt.plot(x, y, 'r--') # this is how the complete line would look like
threshold = 20
indices = np.nonzero(np.diff(x) >= threshold)[0] + 1
for i0, i1 in zip(np.append(0, indices), np.append(indices, len(x))):
plt.plot(x[i0:i1], y[i0:i1], '-go')
plt.show()
