3

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.

2
  • Post the full error code too please. Commented Dec 9, 2020 at 9:53
  • there is the full error code Commented Dec 9, 2020 at 9:59

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome. You shouldn't edit the code in the question: future readers won't be able to see what the problem was, and why the answer helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.