2012-07-18 2 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를 추가 할 것이다 : 이 I 지금이 무엇 [j]])

누구든지이 작업을 수행하는 방법을 알고 있습니까?

감사합니다.

+1

코드를 추가하면 도움을 드릴 수 있습니다! – Qiau

+0

그가 필요로하는 것은 동일한 속도를 가진 위치 vectore들의 집합과 같은 색을 유지하는 카운트 라인 그래픽입니다. – Blas

답변

1

scatter (doc) 원하는대로 할 수 있습니다.

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

그러나 마커는 연결되지 않습니다. 각 세그먼트마다 다른 색의 선을 원한다면 많은 수의 두 점 선을 그려야 할 것이라고 생각합니다.

편집 : 나는이 문제를 해결 갈 것입니다 방법을 보여 일부 코드를 작성했습니다 와도

+0

이것이 좋은 대답이라고 생각합니다. 점 사이에 선분을 추가하려면 plt.platter (x, y)를 plt.scatter()와 함께 호출하기 만하면됩니다. – Vorticity

1

로 의견 제안 plot을 추가했다. 내 지식으로는 각 선분을 채색 할 방법이 없기 때문에 매 단계마다 플롯하고 적절한 색상을 선택하여 각 단계를 반복해야했습니다.

Code output

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() 
는 희망 코드와 코멘트 자체 문서화합니다 (년 Colorbar를 만들 수있는 맵핑이 부분은 아마도 가장 어려운, 가장 까다로운 부분이고, 당신은 심지어 하나를 싶지 않을 수도 있습니다!) 충분히 있습니다

0

quiver으로 시도 할 수도 있습니다. 방향 필드 (화살표) 플롯을 만듭니다.

enter image description here

당신이 원하는 경우

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

, 당신은 또한 그 위치에서 속도의 화살표 크기가 의존 할 수 있습니다.

관련 문제