0

Consider the following MWE

import matplotlib.pyplot as plt
l = [8, 5, 7, 10, 3, 3, 7, 5, 2, 6]
plt.plot(range(len(l)), l, marker=".", markersize=20)

enter image description here

I would like the line segments to be colored according to the contents of l. So the first segment would be color 8, the second color 5 etc. I don't mind too much which color pallete these colors are chosen from as long as the colors are clearly distinct.

How can you do that?

Simd
  • 19,447
  • 42
  • 136
  • 271
  • 1
    Also consider this: `pairs = list(zip(l[:-1], l[1:]))`, `for i, pair in enumerate(pairs):` `plt.plot([i, i+1], pair, c=f'C{l[i]}', marker=".", markersize=20)` – K.Cl Jul 04 '22 at 17:48
  • @K.Cl How is C defined? – Simd Jul 04 '22 at 18:14
  • `'C0'`, `'C1'`, ..., '`Cn`' are shorthands for the n-th color of the current color cycle you have active. – K.Cl Jul 04 '22 at 18:24

0 Answers0