Skip to content
Advertisement

How do I animate a rotating link in matplotlib plot?

I will keep the problem simple.

Let’s say I have a link of length 15 units and I want to make it animate in the matplotlib plot as the value of theta(angle between the link and x axis) varies from 0 to 90 degrees. The link should rotate about the (0,0) coordinates, i.e. the link is fixed at the (0,0) coordinates.

Obviously trigonometry rules will be applied to find the coordinates of the other end of the link while one end is fixed at (0,0) coordinates.

I just want to use purely matplotlib and numpy modules.

Advertisement

Answer

As you might know already, matplotlib provides built-in support for animations. The FuncAnimation class is the simplest interface for native matplotlib animations.

JavaScript

I have taken the liberty of using polar coordinates. If you are not familiar with this, do check this example from the official documentation.

USER GUIDE: https://matplotlib.org/3.3.3/api/animation_api.html#funcanimation

Here’s what the above code generates: Here's what the above code generates

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement