2015-01-30 5 views
1

pyplot의 triplot 함수를 사용하여 scipy.spatial.Delaunay에 의해 생성 된 삼각형 목록을 그려서 각 삼각형을 그릴 수 있고 개별 색상으로 채울 수 있습니까? 내가 만든 기본 파이썬 스크립트는Matplotlib triplot의 삼각형을 개별 색상으로 채우기

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.spatial import Delaunay 
import matplotlib.image as mpimg 

h = 300 
w = 1000 

npts = 30 

pts = np.zeros((npts,2)) 
pts[:,0] = np.random.randint(0,w,npts) 
pts[:,1] = np.random.randint(0,h,npts) 
tri = Delaunay(pts) 

plt.xlim(0, w) 
plt.ylim(0, h) 

# Determine the color used for each triangle based upon the orthocenter 
# of the triangle and the corresponding pixel color in a background image. 

centers = np.sum(pts[tri.simplices], axis=1, dtype='int')/3.0 
colors = [img[y,x] for x,y in centers] 

# This plots the edges of each triangle with no fill. I'd like to 
# include the colors list to plot a fill for each. 

plt.triplot(pts[:,0], pts[:,1], tri.simplices.copy()) 

plt.show() 

내가 해당 삼각형의 색상이 들어있는 색상 목록을 통과 할 수있는 triplot 몇 가지 인수가 거기에있다. 적절한 삼각형을 사용하여 각 삼각형을 반복적으로 그릴 수는 있지만 더 우아하고 빠른 방법이 있다면 좋을 것입니다.

답변

4

찾고있는 기능은 pyplot.tripcolor에 포함되어 있습니다. ,

다음 인수는 C해야합니다 : 해당 설명서에서

, 당신은 "스마트"이며 포인트 또는 삼각형 색상을 지정 여부를 추측하려고 것을 볼 수 있습니다 색상 값이 인 경우 삼각형의 한 점당 하나씩 색상 값이 점으로 정의 된 경우 하나당 또는 삼각형으로 색상 값이 인 경우 삼각형의 삼각형 당 하나입니다. 삼각 측량에 동일한 수의 점과 삼각형이있는 경우 색 점 값이 점으로 정의되어 있다고 가정합니다. 삼각형의 색상 값을 사용하려면 kwarg facecolors = C 대신 C 대신 C를 사용하십시오.

귀하의 예를 계속하려면 :

여기
import numpy as np 
import matplotlib.pyplot as plt 
from scipy.spatial import Delaunay 

h = 300 
w = 1000 
npts = 500 
pts = np.zeros((npts,2)) 
pts[:,0] = np.random.randint(0,w,npts) 
pts[:,1] = np.random.randint(0,h,npts) 
tri = Delaunay(pts) 
plt.xlim(0, w) 
plt.ylim(0, h) 
centers = np.sum(pts[tri.simplices], axis=1, dtype='int')/3.0 
colors = np.array([ (x-w/2.)**2 + (y-h/2.)**2 for x,y in centers]) 
plt.tripcolor(pts[:,0], pts[:,1], tri.simplices.copy(), facecolors=colors, edgecolors='k') 
plt.gca().set_aspect('equal') 
plt.show() 

나는 단지 삼각형의 중심과 이미지의 중심 (사이의 거리의 색상을 기반으로 나는 적절한 이미지를 가지고 있지 않기 때문에).

enter image description here

관련 문제