2017-11-11 13 views
-1

중요한 비트를 요약하면 다음과 같습니다.Circle Patch의 set_radius에 해당하는 타원 패치? Matplotlib

저는 matplotlib 그래프에 원을 그려주는 함수가 있습니다. 매번 함수를 기억할 때마다 그래프의 같은 위치에 항상 있어야하기 때문에 (set_radius를 사용하여) 원의 크기를 간단히 조정합니다. 이렇게하면 엉망이되지 않습니다.

타원형 패치로 같은 것을하고 싶지만 이번에는 높이, 너비 및 각도를 변경할 수 있습니다. 내 코드는 좀 더 설명 드리겠습니다 문맥 조금 있으면 그러나 나는 set_radius

def Moment_Of_Inertia(self): 
    """Plot the moment of Inertia ellipse, with the ratio factor """ 

    # my code to get ellipse/circle properties 
    self.limitradius = findSBradius(self.RawImage,self.SBLimit)[0] 
    MoIcall = mOinertia(self.RawImage,self.limitradius) 
    self.ratio=MoIcall[0] # get the axes ratio 
    self.height=1 
    Eigenvector = MoIcall[1] 
    self.EllipseAngle np.degrees(np.arctanh((Eigenvector[1]/Eigenvector[0]))) 

    # This is the part I am not sure how to do 
    self.MoIellipse.set(width=self.ratio*15) 
    self.MoIellipse.set(height=self.height*15) 
    self.MoIellipse.set(angle= self.EllipseAngle) 

    # It works with a circle patch 
    self.circleLimit.set_radius(self.limitradius) 
    self.circleLimit.set_visible(True) 
    self.MoIellipse.set_visible(True) 
    self.canvas.draw() 

의 동등한를 찾을 수없는, 내가 Tkinter의 창에서하기 matplotlib 그래프를 포함하는 것을 시도하고있다. 두 패치는 이미 생성자에서 초기화되어 있으며 크기를 조정하려고합니다.

+0

무엇 '.MoIellipse'입니까? 읽고 [mcve]를 이해하십시오. – ImportanceOfBeingErnest

+0

이것은 생성자에서 이미 초기화 한 패치의 이름입니다. – Speddy

답변

0

이 답변은 질문이 타원에 대한 것이라고 가정합니다 (matplotlib.patches.Ellipse).

이것은 속성이 width, 및 angle입니다. 다른 파이썬 객체로, 당신은 또한 setattr을 사용할 수 있습니다 당신은

setattr(ellipse,"width", 2) 

처럼

ellipse = matplotlib.patches.Ellipse((0,0),.5,.5) 
ellipse.width = 1 
ellipse.height = 2 
ellipse.angle = 60 

으로 일부 완료 예를 들어 이러한 속성을 설정할 수 있습니다

import matplotlib.pyplot as plt 
import matplotlib.widgets 

class sliderellipse(matplotlib.widgets.Slider): 
    def __init__(self,*args,**kwargs): 
     self.ellipse = kwargs.pop("ellipse", None) 
     self.attr = kwargs.pop("attr", "width") 
     matplotlib.widgets.Slider.__init__(self,*args,**kwargs) 
     self.on_changed(self.update_me) 

    def update_me(self,val=None): 
     setattr(self.ellipse,self.attr, val) 
     self.ax.figure.canvas.draw_idle() 

fig, axes = plt.subplots(nrows=4, 
         gridspec_kw={"height_ratios" : [1,.05,.05,.05], 
             "hspace" : 0.5}) 
axes[0].axis([-1,1,-1,1]) 
axes[0].set_aspect("equal") 
ellipse = matplotlib.patches.Ellipse((0,0),.5,.5) 
axes[0].add_patch(ellipse) 
labels = ["width", "height","angle"] 
maxs = [2,2,360] 
sl = [] 
for ax,lab,m in zip(axes[1:],labels,maxs): 
    sl.append(sliderellipse(ax,lab,0,m,ellipse=ellipse,attr=lab)) 

plt.show() 

enter image description here

+0

시간 내 주셔서 감사합니다.이게 정확히 제가 찾고있는 것입니다. – Speddy

+0

이 경우에는 [someone answers] (https://stackoverflow.com/help/)를 참조하십시오. 누군가 응답). – ImportanceOfBeingErnest