0

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)

enter image description here

I'd need a plot that has the color of the top plot on the line structure of the bottom plot.

dtadres
  • 217
  • 3
  • 12
  • 1
    As far as I have read the existing posts on this topic, it is not possible to have a continuously varying line with a colormap. The alternative is to have multiple line collections, each having a different color and extrapolate it to a large number of lines so that you get somewhere close to a smoothly varying colors. – Sheldore Apr 22 '19 at 20:46
  • 1
    Read [this](https://stackoverflow.com/questions/13622909/matplotlib-how-to-colorize-a-large-number-of-line-segments-as-independent-gradi/13649811#13649811) – Sheldore Apr 22 '19 at 20:48
  • 1
    @Bazingaa One single LineCollection is enough. But else that's correct. I think it makes sense to close this as duplicate? – ImportanceOfBeingErnest Apr 22 '19 at 21:30
  • @ImportanceOfBeingErnest: Yes, thanks for closing ;) – Sheldore Apr 22 '19 at 21:45

0 Answers0