3

If I understand correctly, the function:

matplotlib.pyplot.plot(x, y)

plots len(x)-1 separate line segments - one going from (x[0], y[0]) to (x[1],y[1]), one going from (x[1],y[1]) to (x[2], y[2]), etc. In my application, I want to display a curve consisting of a series of line segments connecting data points in this way, but there is an extra piece of data (z) associated with the transition between each of these data points, that I want to represent by the color of the line segment. Clearly one way of doing this is the following:

for i in range(len(x)-1)):
    matplotlib.pyplot.plot(x[i:i+2],y[i:i+2], color=z[i])

but is there a way to do it that doesn't involve a separate call to matplotlib.pyplot.plot for each line segment?

Alex319
  • 3,818
  • 9
  • 34
  • 40

2 Answers2

1

You should be able to use matplotlib.collections.LineCollection which accepts a colors parameter (sequence of RGBA tuples).

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
1

If you implement a colorbar, something similar to this, then you should be able to do it in one line. The trick may be to pick a colorscale that changes on the order of your line segments.

Community
  • 1
  • 1
cosmosis
  • 6,047
  • 3
  • 32
  • 28