Move Artist from one Axes to Another on the Same Figure

Viewed 424

I'm attempting to plot some data onto a subplot, and then move part of that data onto another subplot in the same figure when a user selects it. I could just remove the artist from one axes and replot it on the other, but that seems to be too slow. Instead, I want to just move the artist from one axes to the other.

I can easily remove the artist from the axes, but adding to the other one doesn't seem to do anything. relevant code:

artist.remove()
artist.axes = self.ax1
self.ax1.add_line(artist)
self.fig.canvas.draw_idle()

after this code runs, the selected artist is no longer on the first axes, but it will not show up on the second axes. More specifically, the artist is on the axes (ax2.children has one more element in it, artist.axes is now equal to ax2), but it is not visually showing up even after redrawing everything.

here is a full working example that demonstrates the issue:

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
fig = plt.figure(figsize=(10,10))
ax1 = fig.add_subplot(121, picker=True)
ax2 = fig.add_subplot(122, picker=True)

v = [0,1,2,3,4]

plt.ion()

ax1.plot(v, picker=5)

def onclick(event):
    artist = event.artist
    print(artist)
    if isinstance(artist, Line2D):
        artist.remove()
        artist.axes = ax2
        ax2.add_line(artist)
        fig.canvas.draw()


fig.canvas.mpl_connect('pick_event', onclick)

plt.show()

input('...')
1 Answers
Related