椭圆面片相当于在圆面片中设置_半径?Matplotlib库

2024-09-19 23:42:19 发布

您现在位置:Python中文网/ 问答频道 /正文

总结一下要点:

我有一个函数,在matplotlib图上画一个圆。每次我回忆起这个函数时,我只需调整圆的大小(使用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图形。两个补丁都已经在构造函数中初始化了,我只想调整它们的大小。在


Tags: the函数self图形matplotlibheightsetratio
1条回答
网友
1楼 · 发布于 2024-09-19 23:42:19

这个答案假设问题是关于^{}中的椭圆。在

它有属性widthheightangle。可以将这些属性设置为

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

对于任何其他python对象,也可以使用setattr,比如

^{pr2}$

一些完整的例子:

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

相关问题 更多 >