【问题标题】:AttributeError: 'AxesSubplot' object has no attribute 'Circle' Embedding matplotlib in tkinterAttributeError:“AxesSubplot”对象没有属性“Circle”在 tkinter 中嵌入 matplotlib
【发布时间】:2020-12-09 09:48:13
【问题描述】:

我正在尝试将轨道模拟嵌入到 tkinter 框架中,我的绘图工作正常我现在只是尝试在图中输入圆圈来表示行星。我查看了有关如何在 FigureCanvasTkAgg 中绘制圆圈的文档,但找不到任何内容,希望有人能提供帮助。

代码如下:

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

错误:

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

非常感谢任何帮助。

【问题讨论】:

  • 请也发布完整的错误代码。
  • 有完整的错误码

标签: python matplotlib tkinter


【解决方案1】:

Axes 实例没有Circle 方法。那是matplotlib.patches的一部分,可以单独导入。

另外,当你将补丁添加到ax 时,你不需要做ax.gca(),因为你已经有一个当前轴的句柄(即不需要.gca(),它代表get当前坐标轴)。

最后,Axes 没有show() 方法。这是 pyplot 模块中的一个函数,可以称为plt.show()

如果您还没有它们,请添加以下导入:

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

然后将你的函数修改为:

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

【讨论】:

  • 不客气。您不应该编辑问题中的代码:未来的读者将无法看到问题所在,以及为什么答案有帮助!
猜你喜欢
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
相关资源
最近更新 更多