Following the links and suggestions of ImportanceOfBeingErnest in the comments, I pieced together the following solution for the case when a plot of line segments of different color is required.
Ideally, I would have wished each point to be of a particular color and the line segments to smoothly interpolate in the color-spectrum between these respective colors. But I guess, matplotlib is free software, so it's not too surprising that it does not have the most fancy features. Unless, of course, if it can be done, but I don't know how?
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
xy = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.hstack([xy[:-1], xy[1:]])
fig, ax = plt.subplots()
lc = LineCollection(segments, colors=np.random.rand(len(segments), 3))
ax.add_collection(lc)
ax.autoscale()
ax.set_title('A multi-color plot')
plt.show()

EDIT:
Just for fun, doing more steps and creating a gradient "by hand", as ImportanceOfBeingErnest suggested, we can get e.g. something like this:
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x = [0.]
while x[-1]<2 * np.pi :
x.append(x[-1] +1/(100*np.sqrt(1+4*x[-1]*x[-1]*np.cos(x[-1]*x[-1])*np.cos(x[-1]*x[-1]))))
x = np.array(x)
y = np.sin(x ** 2)
xy = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.hstack([xy[:-1], xy[1:]])
myColors = np.random.rand(len(segments), 3)
for i in range(len(segments)//10):
for j in range(9):
myColors[10*i+j+1] = myColors[10*i]+(myColors[10*(i+1)]-myColors[10*i])*(j+1)/10
fig, ax = plt.subplots()
lc = LineCollection(segments, colors=myColors)
ax.add_collection(lc)
ax.autoscale()
ax.set_title('A multi-color plot')
plt.show()

ax.scatter. If instead you want individual line segments to be colorized, you can use aLineCollectionas shown e.g. in this Q&A.ax.scatter(x,y, c=np.random.rand(len(x), 3))plotcreates aLine2Dobject, which is optimized for fast drawing. It would be bad style to let the same commandplotreturn different objects, depending on the input. That's why there isLineCollection, which can be used if different segments are supposed to have different properties.