user3286261
user3286261

Reputation: 501

How do I animate circles and line with matplotlib?

I can animate six circles, and I can animate a line. When I try to animate both, I cannot figure out what init() and animate() should return. For six circles I "return tuple(pins)" and for the line I "return line,". Each pin is a "class 'matplotlib.patches.Circle'" and the line is "class 'matplotlib.lines.Line2D'."

When I try to animate both the circles and the line, I have tried many different return statements without success. Here are the some of the results:

return line, tuple(pins) GIVES 'tuple' object has no attribute 'set_animated'

return tuple(pins) + (line) GIVES can only concatenate tuple (not "Line2D") to tuple

return tuple(pins) + tuple(line) GIVES 'Line2D' object is not iterable

Upvotes: 0

Views: 125

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339610

Note that you only need to return something from the animating function if you use blitting.

From the documentation:

If blit == True, func must return an iterable of all artists that were modified or created. This information is used by the blitting algorithm to determine which parts of the figure have to be updated. The return value is unused if blit == False and may be omitted in that case.

So just ommiting the return altogether may be the easiest option.

If you need/want to use blitting, you need to return an iterable of artists. This can e.g. be a tuple or a list. Unfortunately, it's not clear what pins is from the question.

Supposing pins is a list,

return pins + [line]

or if you want to make it a list,

return list(pins) + [line]

Supposing pins is a tuple,

return pins + (line,)

or if you want to make it a tuple,

return tuple(pins) + (line,)

Upvotes: 1

Related Questions