我想使用matplotlib.collections.LineCollection
对象,从两个Numpy数组x
和y
开始
>>> from matplotlib.collections import LineCollection
>>> from numpy import array, linspace
>>> x = linspace(0, 2, 5)
>>> y = 1-(1-x)**2
实例化LineCollection
的唯一条件是由segments列表组成的数据结构,每个segment是points的列表,每个point] >作为元组。
使用我可以使用的两个向量x
和y
>>> 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. ]]]
我的问题。是否可以以不太隐秘的方式构造
segments
?
我想使用matplotlib.collections.LineCollection对象,从两个Numpy数组x和y开始>>>从matplotlib.collections导入LineCollection >>>从numpy import ...>]
xy = np.array([x, y])
segments = np.array([[xy[:, i], xy[:, i+1]] for i in range(xy.shape[1]-1)])