Я написал небольшую функцию, которая принимает два массива y1
, y2
(x1
, x2
необязательны) и соединяет их точки данных по вертикали .
def draw_lines_between(*, ax, x1=None, x2=None, y1, y2, color_list=None, **kwargs):
assert len(y1) == len(y2)
assert isinstance(color_list, list)
n = len(y1)
if x1 is None:
x1 = np.arange(n)
if x2 is None:
x2 = x1
if color_list is None:
color_list = [None for i in range(n)]
elif len(color_list) < n:
color_list = [color_list] * n
h = np.zeros(n, dtype=object)
for i in range(n):
h[i] = ax.plot((x1[i], x2[i]), (y1[i], y2[i]), color=color_list[i], **kwargs)[0]
return h
import matplotlib.pyplot as plt
import numpy as np
n = 10
y1 = np.random.random(n)
y2 = np.random.random(n) + 1
x1 = np.arange(n)
color_list = [str(x) for x in np.round(np.linspace(0., 0.8, n), 2)]
fig, ax = plt.subplots()
ax.plot(x1, y1, 'r')
ax.plot(x1, y2, 'b')
draw_lines_between(ax=ax, x1=x1, y1=y1, y2=y2, color_list=color_list)
![Draw lines between lines](https://i.stack.imgur.com/ptmEk.png)