的意見建議我寫一些代碼來證明我將如何去解決這個問題。據我所知,沒有辦法爲每一條線段着色,因此我必須循環每一步,每次繪製(並選擇合適的顏色)。
import matplotlib.pyplot as plt
import numpy
x = numpy.array([1, 1.5, 5, 1, 4, 4])
y = numpy.array([1, 2, 1, 3, 5, 5])
# calculate the absolute distance for each step
distances = numpy.abs(numpy.diff((x**2 + y**2)**0.5))
ax = plt.axes()
# pick a colormap, and define a normalization to take distances to the range 0-1
cmap = plt.get_cmap('jet')
norm = plt.normalize(min(distances), max(distances))
# loop through each walk segment, plotting the line as coloured by
# the distance of the segment, scaled with the norm and a colour chosen
# using the normed distance and the cmap
for i in range(1, len(x)):
distance = distances[i-1]
x0, y0 = x[i-1], y[i-1]
x1, y1 = x[i], y[i]
ax.plot([x0, x1], [y0, y1], '-', color=cmap(norm(distance)))
# put points for each observation (no colouring)
ax.scatter(x, y)
# create a mappable suitable for creation of a colorbar
import matplotlib.cm as cm
mappable = cm.ScalarMappable(norm, cmap)
mappable.set_array(distance)
# create the colorbar
cb = plt.colorbar(mappable)
cb.set_label('Distance/meters')
# add some additional information
plt.title("Person 1's walk path")
plt.xlabel('x/meters')
plt.ylabel('y/meters')
# add some additional text to show the total distance walked.
# The coordinates are in axes coordinates (ax.transAxes).
plt.text(0.99, 0.01, 'Total distance: %.02f meters' % numpy.sum(distances),
transform=ax.transAxes, horizontalalignment='right')
plt.show()
希望代碼和註釋有足夠的自我記錄(可映射部分創建彩條也許是最難的,也是最棘手的部分,你甚至可能不會想要一個!)
添加一段代碼,我們將能夠幫助您! – Qiau 2012-07-18 21:31:34
他需要的是一個線條圖形,它將相同的顏色設置爲具有相同速度的位置向量集合 – Blas 2012-07-18 21:56:40