Python 椭圆面片等效于在圆面片中设置_半径?Matplotlib
总结一下重要的一点: 我有一个函数,它在matplotlib图形上绘制一个圆。每次我回忆起这个函数时,我只是简单地调整圆的大小(使用set_radius),因为它总是需要在图形的同一位置(在中心)。这样就不会太乱了 我想做同样的事情与椭圆补丁,但这一次能够改变高度,宽度和角度,它是在。但是,我找不到任何与set_半径等效的值Python 椭圆面片等效于在圆面片中设置_半径?Matplotlib,python,matplotlib,tkinter,ellipse,Python,Matplotlib,Tkinter,Ellipse,总结一下重要的一点: 我有一个函数,它在matplotlib图形上绘制一个圆。每次我回忆起这个函数时,我只是简单地调整圆的大小(使用set_radius),因为它总是需要在图形的同一位置(在中心)。这样就不会太乱了 我想做同样的事情与椭圆补丁,但这一次能够改变高度,宽度和角度,它是在。但是,我找不到任何与set_半径等效的值 def Moment_Of_Inertia(self): """Plot the moment of Inertia ellipse, with the ratio
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图。这两个补丁已经在构造函数中初始化,我只想调整它们的大小。这个答案假设问题是关于椭圆的 它具有属性
宽度
、高度
和角度
。您可以将这些属性设置为
ellipse = matplotlib.patches.Ellipse((0,0),.5,.5)
ellipse.width = 1
ellipse.height = 2
ellipse.angle = 60
对于任何其他python对象,也可以使用setattr
,如
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()
什么是
.moi
?请阅读并理解。这是我已经在Constructor中初始化的补丁的名称。感谢您,这正是我正在寻找的