简体   繁体   English

AttributeError: 'AxesSubplot' object 没有属性 'Circle' 在 tkinter 中嵌入 matplotlib

[英]AttributeError: 'AxesSubplot' object has no attribute 'Circle' Embedding matplotlib in tkinter

I'm trying to embed an orbital simulation into a tkinter frame, I have the plot working properly I am now just trying to input circles into the graph to represent planets.我正在尝试将轨道模拟嵌入到 tkinter 框架中,我的 plot 工作正常我现在只是想在图中输入圆圈来表示行星。 I have had a look for documentation on how to draw circles in FigureCanvasTkAgg but can't find anything and was hoping someone could help.我查看了有关如何在 FigureCanvasTkAgg 中绘制圆圈的文档,但找不到任何东西,希望有人能提供帮助。

Here's the code:这是代码:

matplotlib.use('TkAgg')
root = Tk.Tk()
root.wm_title("Orbital Simulation")
fig = plt.Figure()
canvas = FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

ax=fig.add_subplot(111)
fig.subplots_adjust(bottom=0.25)

gridArea = [0, 200, 0, 200]  # margins of the coordinate grid
ax.axis(gridArea)  # create new coordinate grid
ax.grid(b="on")  # place grid

.    
.
.

def placeObject(self):
    drawObject = ax.Circle(self.position, radius=self.radius, fill=False, color="black")
    ax.gca().add_patch(drawObject)
    ax.show()

Error:错误:

drawObject = ax.Circle(self.position, radius=self.radius, fill=False, color="black") AttributeError: 'AxesSubplot' object has no attribute 'Circle' drawObject = ax.Circle(self.position, radius=self.radius, fill=False, color="black") AttributeError: 'AxesSubplot' object 没有属性 'Circle'

Any help is greatly appreciated.任何帮助是极大的赞赏。

Axes instances do not have a Circle method. Axes实例没有Circle方法。 That is part of matplotlib.patches , and can be imported separately.这是matplotlib.patches的一部分,可以单独导入。

Also, when you add the patch to the ax , you don't need to do ax.gca() , since you already have a handle for the current Axes (ie no need to .gca() , which stands for get current axes).此外,当您将补丁添加到ax时,您不需要执行ax.gca() ,因为您已经有了当前 Axes 的句柄(即不需要.gca() ,它代表获取当前轴)。

Finally, Axes do not have a show() method.最后, Axes没有show()方法。 That is a function from the pyplot module, that can be called as plt.show()那是来自 pyplot 模块的 function ,可以称为plt.show()

If you don't have them already, add the following imports:如果您还没有它们,请添加以下导入:

import matplotlib.pyplot as plt
from matplotlib.patches import Circle

And then modify your function to:然后将您的 function 修改为:

def placeObject(self):
    drawObject = Circle(self.position, radius=self.radius, fill=False, color="black")
    ax.add_patch(drawObject)
    plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "matplotlib:AttributeError:“AxesSubplot”对象没有属性“add_axes”" - matplotlib: AttributeError: 'AxesSubplot' object has no attribute 'add_axes' Tkinter AttributeError: 'AxesSubplot' object has no attribute 'canvas' and matplotlib graphs don't update until I resize the tkinter window - Tkinter AttributeError: 'AxesSubplot' object has no attribute 'canvas' and matplotlib graphs don't update until I resize the tkinter window AttributeError:模块“matplotlib”没有属性“AxesSubplot” - AttributeError: module 'matplotlib' has no attribute 'AxesSubplot' AttributeError:“AxesSubplot”对象没有属性“海岸​​线” - AttributeError: 'AxesSubplot' object has no attribute 'coastlines' AttributeError: 'AxesSubplot' object 没有属性 'sharex' - AttributeError: 'AxesSubplot' object has no attribute 'sharex' AttributeError: 'AxesSubplot' object 没有属性 'get_xtickabels' - AttributeError: 'AxesSubplot' object has no attribute 'get_xtickabels' 子图AttributeError:'AxesSubplot'对象没有属性'get_extent' - subplot AttributeError: 'AxesSubplot' object has no attribute 'get_extent' 'AxesSubplot' 对象没有属性 'colorbar' - 'AxesSubplot' object has no attribute 'colorbar' Matplotlib和Tkinter实时图(AttributeError:'NoneType'对象在Tkinter回调中没有属性'update'异常) - Matplotlib and Tkinter Real time Plot (AttributeError: 'NoneType' object has no attribute 'update' Exception in Tkinter callback) AttributeError: 'str' 对象在 matplotlib 中没有属性 'pop' - AttributeError: 'str' object has no attribute 'pop' in matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM