2016-11-05 5 views
1

PolyCollection을 사용하여 동일한 직사각형 세트를 그려야하지만 올바른 위치에 배치하는 데 어려움이 있습니다. 다음 코드에서는 데이터 좌표로 지정된 offsets에 가운데에 세 개의 사각형을 넣으려고하지만 올바른 위치에 나타나지 않습니다.오프셋을 사용하여 PolyCollection을 올바르게 배치하는 방법은 무엇입니까?

from matplotlib import collections 

subplot_kw = dict(xlim=(0, 100), ylim=(0, 100)) 
fig, ax = plt.subplots(subplot_kw=subplot_kw, figsize=(5,5)) 
ivtx = 10 * np.array([-1,-1,1,1])/2.0 
jvtx = 20 * np.array([-1,1,1,-1])/2.0 
vtx = np.array(zip(jvtx, ivtx)) 
rectangles = collections.PolyCollection([vtx], offsets=[(0,0),(20,10),(40,60)], transOffset=ax.transData) 
ax.add_collection(rectangles) 

나는 문제가 transOffset이라고 생각합니다. 올바른 변환이란 무엇입니까?

rectangles are at the wrong offsets

답변

0

혼동 문서화 오프셋 및 transOffset의 역할. PolyCollection의 데이터 좌표에 offsets을 사용하려면 transOffset을 ID 변환으로 설정하고 offset_position을 "데이터"로 변경해야합니다.

collections.PolyCollection([vtx], offsets=[(0,0),(20,10),(40,60)], 
             transOffset=mtransforms.IdentityTransform(), 
             offset_position="data") 

완벽한 예 :

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import collections 
import matplotlib.transforms as mtransforms 

subplot_kw = dict(xlim=(0, 100), ylim=(0, 100)) 
fig, ax = plt.subplots(subplot_kw=subplot_kw, figsize=(5,5)) 
ivtx = 10 * np.array([-1,-1,1,1])/2.0 
jvtx = 20 * np.array([-1,1,1,-1])/2.0 
vtx = np.array(zip(jvtx, ivtx)) 
rectangles = collections.PolyCollection([vtx], offsets=[(0,0),(20,10),(40,60)], 
             transOffset=mtransforms.IdentityTransform(), 
             offset_position="data") 
ax.add_collection(rectangles) 

plt.show() 

enter image description here


대안은 즉 수동으로 변환, 직접 정점을 계산하는 과정이다 :

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

subplot_kw = dict(xlim=(-20,70), ylim=(-20, 70)) 
fig, ax = plt.subplots(figsize=(5,5),subplot_kw=subplot_kw) #, 
ivtx = 10 * np.array([-1,-1,1,1])/2.0 
jvtx = 20 * np.array([-1,1,1,-1])/2.0 
vtx = np.array(zip(jvtx, ivtx)) 

def o(vtx, o): 
    vtx = np.copy(vtx) 
    vtx[:,0] = vtx[:,0]+o[0] 
    vtx[:,1] = vtx[:,1]+o[1] 
    return vtx 

offsets=[(0,0),(20,10),(40,60)] 
rectangles = collections.PolyCollection([o(vtx,offset) for offset in offsets])  
ax.add_collection(rectangles) 

plt.show() 
+0

것을 무엇 나는 끝났다. 하고있어. 그래도 나는 오프셋이 얼마나 효과적인지 궁금하다. – physcheng

관련 문제