Did you already have a look at https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/multicolored_line.html?
EDIT: as suggested in the comments, a minimal working example could be
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x = np.linspace(0,1, 100)
y = np.linspace(0,1, 100)
cols = np.linspace(0,1,len(x))
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
fig, ax = plt.subplots()
lc = LineCollection(segments, cmap='viridis')
lc.set_array(cols)
lc.set_linewidth(2)
line = ax.add_collection(lc)
fig.colorbar(line,ax=ax)
yes but I can't seem to get it to work on a simple line
Simple lines in matplotlib, i.e. Line2d-objects, only support one color at once. If you want to have a color gradient, you will need to work with LineCollections.
The technique is to mesh the line into small pieces and plot each with a color. The provided link shows how it works indeed.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review