'matplotlib.axes.Axes.draw_artist() not working for plt.plot([],[], animated=True)

I am trying to do real time plotting using blitting, and one of the features that some tutorials have suggested is using plt.plot(..., animated=True) so that lines can be redrawn individually. However, when I try using this, nothing gets drawn, so can I get some help understanding why this works:

fig, ax  = plt.subplots(figsize=(11,9))

ax.set_ylim(0., 2.)
ax.set_xlim(0., 100.)

artists = (ax.plot([], [], color='gold')[0],
           ax.plot([], [], color='green')[0],
           ax.plot([], [], color='blue')[0])

# Shows the plot but doesn't stop the program
plt.show(block=False)
            
# Save a copy of the figure before filling it
plt.pause(0.1)
background = fig.canvas.copy_from_bbox(fig.bbox)

for i in range(len(artists)):
    artists[i].set_xdata(np.arange(100))
    artists[i].set_ydata(np.ones(100))

# Copy image to GUI state, but may not show yet
fig.canvas.draw()
# Flush any pending GUI events, paint the window
fig.canvas.flush_events()

But this doesn't:

fig, ax  = plt.subplots(figsize=(11,9))

ax.set_ylim(0., 2.)
ax.set_xlim(0., 100.)

artists = (ax.plot([], [], animated=True, color='gold')[0],
           ax.plot([], [], animated=True, color='green')[0],
           ax.plot([], [], animated=True, color='blue')[0])

# Shows the plot but doesn't stop the program
plt.show(block=False)
            
# Save a copy of the figure before filling it
plt.pause(0.1)
background = fig.canvas.copy_from_bbox(fig.bbox)

for i in range(len(artists)):
    artists[i].set_xdata(np.arange(100))
    artists[i].set_ydata(np.ones(100))
    ax.draw_artist(artists[i])

# Copy image to GUI state, but may not show yet
fig.canvas.draw()
# Flush any pending GUI events, paint the window
fig.canvas.flush_events()

To clarify, in this second example, the figure and axes get drawn, but the Line2D artists are not drawn. I am using axes.draw_artist(Line2D) like the documentation says to do.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source