【问题标题】:Ellipse patch equivalent to set_radius in the Circle Patch? Matplotlib椭圆补丁相当于圆形补丁中的set_radius? Matplotlib
【发布时间】:2018-04-24 15:11:36
【问题描述】:

总结重要的一点:

我有一个函数可以在 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 图。两个补丁都已经在构造函数中初始化了,我只想调整它们的大小。

【问题讨论】:

标签: python matplotlib tkinter ellipse


【解决方案1】:

此答案假定问题是关于来自matplotlib.patches.Ellipse 的椭圆。

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

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

至于其他任何python对象,也可以使用setattr,like

setattr(ellipse,"width", 2)

一些完整的例子:

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()

【讨论】:

  • 感谢您的宝贵时间,这正是我要找的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
相关资源
最近更新 更多