2012-07-18 121 views
1

我是Matplotlib的新手。我每秒都有一个人的位置,我正在试图做一个图表来展示这一点。我已经设法展示它,但现在我希望它根据它们的速度显示不同的颜色。所以,我需要plt.plot()颜色取决于每对点之间的距离,而不是始终相同。 这是我现在:如何在Matplotlib中以不同颜色绘制

x = [i[0] for i in walk] 
y = [i[1] for i in walk] 
plt.clf() 
fig = plt.gcf() 
plt.axis([0, 391, 0, 578]) 
im = plt.imread('field.png') 
cancha = plt.imshow(im) 
plt.plot(x,y) 
plt.axis('off') 
plt.savefig(IMG_DIR + 'match.png',bbox_inches='tight') 
plt.clf() 

我想补充一些变量,根据距离限定的颜色([X [I],值Y [i]],[X [j]的,Y [j]])

有谁知道如何做到这一点?

谢谢!

+1

添加一段代码,我们将能够帮助您! – Qiau 2012-07-18 21:31:34

+0

他需要的是一个线条图形,它将相同的颜色设置为具有相同速度的位置向量集合 – Blas 2012-07-18 21:56:40

回答

1

scatter会做你想做的(doc)。

plt.scatter(x,y,c=distance(x,y)) 
plt.plot(x,y,'-') # adds lines between points 

但是,这不会连接标记。如果你想在每一段上有不同颜色的线,我认为你将不得不绘制大量的两点线。

编辑:添加plot由涡

+0

我认为这是一个很好的答案。要在点之间添加线段,只需将plt.plot(x,y)与plt.scatter()一起调用,但每个人都要调用一次。 – Vorticity 2012-07-19 04:22:05

1

的意见建议我写一些代码来证明我将如何去解决这个问题。据我所知,没有办法为每一条线段着色,因此我必须循环每一步,每次绘制(并选择合适的颜色)。

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() 

Code output

希望代码和注释有足够的自我记录(可映射部分创建彩条也许是最难的,也是最棘手的部分,你甚至可能不会想要一个!)

0

您也可以尝试quiver。它会绘制方向字段(箭头)。

import pylab as plt 

x=[12, 13, 14, 15, 16] 
y=[14, 15, 16, 17, 18] 
speed=[1,2,3,4,5] 

# Determine the direction by the difference between consecutive points 
v_x=[j-i for i, j in zip(x[:-1], x[1:])] 
v_x.append(v_x[-1]) # The last point 
v_y=[j-i for i, j in zip(y[:-1], y[1:])] 
v_y.append(v_y[-1]) # The last point 

plt.quiver(x,y,v_x,v_y,speed) 
plt.colorbar() 
plt.xlim(11,17) 
plt.ylim(13,19) 
plt.show() 

enter image description here

如果你愿意,你也可以使箭头的大小依赖于该位置的速度。

相关问题