Rectangle 27 0

python matplotlib: update position of patches (or: set_xy for circles)?


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

class InteractiveCircle(object):
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.ax.axis('equal')

        self.circ = Circle((0.5, 0.5), 0.1)
        self.ax.add_artist(self.circ)
        self.ax.set_title('Click to move the circle')

        self.fig.canvas.mpl_connect('button_press_event', self.on_click)

    def on_click(self, event):
        if event.inaxes is None:
            return
        self.circ.center = event.xdata, event.ydata
        self.fig.canvas.draw()

    def show(self):
        plt.show()


InteractiveCircle().show()

@JoeKington So how do I do this for an arc? I don't want to move its centre rather I want to change its starting and ending theta i.e. the spread of the arc.

As a simple (non-draggable) example:

It's a bit annoying that it's inconsistent, but to update the position of a circle, set circ.center = new_x, new_y.

Thank you very much, that did the trick! Since I always advocate the RTFM concept, I'm wondering where I could find this information? I can't see it in patches.Circle, patches.Ellipse, patches.Patch, or artist.Artist.

Unfortunately, it's undocumented. I had to dig through the code to find it (ipython is also a big help). These sort of "corners" of matplotlib definitely need better documentation...

Note