2013-08-18 1 views
0

OK ...이 질문을하는 방법을 모르겠다.Matplotlib이 무작위 순서로 배열되어있을 때 Matplotlib에 대한 데이터 포인트 정렬

일부 데이터의 2 차원 등고선 플롯을 생성하려고합니다 (블렌더 평면의 점에서 계산하여 생성). 이 데이터 포인트를 얻는 순서는 무작위이지만 각 z 값의 x, y 좌표를 알고 있습니다. 다른 말로하면 [x, y, z] 세개의 정렬되지 않은 컬렉션이 있습니다.

내 질문은 ... Matplotlib로 음모를 꾸밀 수있는 배열 집합으로 이러한 데이터 요소를 매쉬하는 가장 간단한 방법은 무엇입니까?

답변

1

이는) 데이터가 균등하게 그리드에 있다고 가정하고 b) 사용자가 그리드의 모든이

from pylab import * # mostly to make my fake data work 
import copy 

# make some fake data 
X, Y = np.meshgrid(range(10), range(10)) 
xyz = zip(X.flat, Y.flat, np.random.rand(100)) # make sure you have a list of tuples 
xyz_org = copy.copy(xyz) 

# randomize the tuples 
shuffle(xyz) 
# check we changed the order 
assert xyz != xyz_org 
# re-sort them 
xyz.sort(key=lambda x: x[-2::-1]) # sort only on the first two entries 

# check we did it right 
assert xyz == xyz_org 

# extract the points and re-shape to a grid 
X_n, Y_n, z = [np.array(_).reshape(10, 10) for _ in zip(*xyz)] 

# check we re-created X and Y correctly 
assert np.all(X_n == X) 
assert np.all(Y_n == Y) 

# make the plot 
plt.contour(X_n, Y_n, z) 
포인트