ben green
ben green

Reputation: 33

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. 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.

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'

Any help is greatly appreciated.

Upvotes: 3

Views: 5238

Answers (1)

tmdavison
tmdavison

Reputation: 69203

Axes instances do not have a Circle method. That is part of matplotlib.patches, and can be imported separately.

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 (i.e. no need to .gca(), which stands for get current axes).

Finally, Axes do not have a show() method. That is a function from the pyplot module, that can be called as 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:

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

Upvotes: 1

Related Questions