2012-04-20 1 views
9

두 배열의 데이터 (예 : ax.plot(x,y))에서 플롯 된 선의 색상을 변경하려고합니다. 색깔은 색인으로 xy로 증가해야한다. 나는 본질적으로 배열의 xy에있는 데이터의 자연스러운 '시간'매개 변수화를 캡처하려고합니다.matplotlib : 데이터의 자연 시간 매개 변수화를 캡처하는 선의 색상 변경

fig = pyplot.figure() 
ax = fig.add_subplot(111) 
x = myXdata 
y = myYdata 

# length of x and y is 100 
ax.plot(x,y,color=[i/100,0,0]) # where i is the index into x (and y) 

색상은 진한 적색과 밝은 빨간색에 검은 색에서 변화와 라인을 생산하기 위해 : 완벽한 세계에서

, 나는 무언가 같이합니다.

내가 명시 적으로 약간의 시간 '배열에 의해 파라미터 기능을 음모를 꾸미고 잘 작동 examples를 봐 왔지만, 나는 그것이 원시 데이터와 함께 동작하지 않습니다 ...

답변

10

두 번째 예는 하나의 당신입니다 당신의 예제에 맞게 편집했는데, 무슨 일이 벌어지고 있는지 이해하기 위해 내 의견을 읽으십시오 :

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib.collections import LineCollection 

x = myXdata 
y = myYdata 
t = np.linspace(0,1,x.shape[0]) # your "time" variable 

# set up a list of (x,y) points 
points = np.array([x,y]).transpose().reshape(-1,1,2) 
print points.shape # Out: (len(x),1,2) 

# set up a list of segments 
segs = np.concatenate([points[:-1],points[1:]],axis=1) 
print segs.shape # Out: (len(x)-1, 2, 2) 
        # see what we've done here -- we've mapped our (x,y) 
        # points to an array of segment start/end coordinates. 
        # segs[i,0,:] == segs[i-1,1,:] 

# make the collection of segments 
lc = LineCollection(segs, cmap=plt.get_cmap('jet')) 
lc.set_array(t) # color the segments by our parameter 

# plot the collection 
plt.gca().add_collection(lc) # add the collection to the plot 
plt.xlim(x.min(), x.max()) # line collections don't auto-scale the plot 
plt.ylim(y.min(), y.max()) 
+0

재구성 및 연결에 어떤 일이 일어나는지 말해 주셔서 감사합니다. 이것은 잘 작동하고 있습니다. –

+0

선분 사이의 전환을 부드럽게하려면'segs = np.concatenate ([점 [: - 2], 점 [1 : -1], 점 [2 :]], 축 = 1)'대신 할 수 있습니다. – shockburner