Я немного изменил ваш код, но в основном просто слил его с рецептом matplotlib
, на который ссылался комментатор.Это работает для меня:
import numpy as np
import matplotlib.pyplot as plt
x_ = np.linspace(1, 200, 200)
y_ = np.random.randint(low=1, high=100, size=200)
# You need to interpolate because your line segments are short.
x = np.linspace(1, 200, 4000)
y = np.interp(x, x_, y_)
# Now follow the maplotlib recipe.
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
fig, ax = plt.subplots(figsize=(15, 5))
norm = plt.Normalize(y.min(), y.max())
lc = LineCollection(segments, cmap='viridis', norm=norm)
lc.set_array(y)
lc.set_linewidth(2)
line = ax.add_collection(lc)
plt.colorbar(line, ax=ax)
ax.set_xlim(x.min(), x.max())
ax.set_ylim(-10, 110)
plt.show()
Результат:
