I want to use a matplotlib.collections.LineCollection
object, starting from the two Numpy arrays x
and y
>>> from matplotlib.collections import LineCollection
>>> from numpy import array, linspace
>>> x = linspace(0, 2, 5)
>>> y = 1-(1-x)**2
The single thing that's strictly required to instantiate a LineCollection
is a data structure composed of a list of segments, each segment being a list of points, each point being a tuple.
Using my two vectors x
and y
I can do
>>> segments = np.array(list(zip( zip(x, x[1:]), zip(y, y[1:])))) .transpose((0,2,1))
>>> print(segments)
[[[0. 0. ]
[0.5 0.75]]
[[0.5 0.75]
[1. 1. ]]
[[1. 1. ]
[1.5 0.75]]
[[1.5 0.75]
[2. 0. ]]]
My question. Is it possible to construct segments
in a less cryptic manner?