EDIT #2: As the comments and duplication tags suggest, the answer can indeed be found here
EDIT: I'm not asking about a colorbar that needs to be added to the plot as suggested by HS-nebula. I'm asking about adding a colormap to plt.plot OR using plt.scatter to create a line
My goal is to plot time series data (y-values) with the time (x-values) coded in color.
While it's straightforward to use a colormap on a scatterplot, I'm unable to connect the dots to a line.
When using "plot" I get the line but can't seem to find any option to use the colormap.
This is best illustrated:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
x_points = 50
colors = cm.cool(np.linspace(0, 1, x_points))
x = np.linspace(-np.pi, np.pi, x_points)
y = np.sin(x)
fig=plt.figure()
ax1=fig.add_subplot(211)
ax1.scatter(x,y, color=colors)
ax2=fig.add_subplot(212)
ax2.plot(x,y)
I'd need a plot that has the color of the top plot on the line structure of the bottom plot.