[英]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.