How to make an animation in SymPy using Python

In summary: OiWJWtKf5&rank=4&sort=desc&async=1In summary, the author is trying to plot ##f(x) = sin x## and provides instructions on how to do so. They also mention that SymPy may have something to do with the animation. They provide an example of how to create an animation with multiple lines and text.
  • #1
JD_PM
1,131
158
I want to make an animation where a (red) ball departs from (0,0) and draws the function ##f(x) = sin x##

This is what I know:

How to plot ##f(x) = sin x##

Captura de pantalla (573).png


To do so I followed instructions from the worksheet. Actually I'd have done:

Captura de pantalla (575).png


But I guess they want to me to it in that way because I have to do less work while coding for the red dot to draw the entire sine curve.

Now to plot just a piece of the function:

Captura de pantalla (574).png

If we set i=50 and x runs from 0 to 200, we are aiming at plotting just 1/4 of ##f(x) = sin x## .

The line ax.plot(X, Y, 'o', color='red') locates the point at i=50 (I guess 'o' is just the command for the point).

But now, how can I code such an animation of ##f(x) = sin x## from (0,0)? I have read I should import:

import matplotlib.animation as animation

I've been looking for exercises on animations in SymPy in Python but found nothing.

Any help is appreciated.
 
Technology news on Phys.org
  • #3
I think that SymPy has nothing to do with animations. It is a functionality of matplotlib. You may want to check this link describing it. You can find there several examples, providing full code. It should be easy to pick one and rewrite it for your needs.
 
  • Informative
Likes JD_PM
  • #4
lomidrevo said:
I think that SymPy has nothing to do with animations. It is a functionality of matplotlib. You may want to check this link describing it. You can find there several examples, providing full code. It should be easy to pick one and rewrite it for your needs.

Thanks, in that link I found what I was looking for. There's a final issue now.

Screenshot (333).png


How can I make the leading point red?
 
  • #5
You should have second line element (matplotlib.lines.Line2D) for the red dot to be updated in your "update" function. In the third line of the code from your screenshot, you get only one line element, that is your blue line. So you should create an empty list of the line elements and using the "append()" method to add new line elements as many as you may need them to update.

Here is an example (including also a text field updated in the graph):
https://stackoverflow.com/questions/20624408/matplotlib-animating-multiple-lines-and-text?rq=1
 
  • Like
Likes JD_PM

1. What is SymPy and how does it relate to animation?

SymPy is a Python library for symbolic mathematics. It allows users to manipulate mathematical expressions and perform calculations symbolically, making it useful for creating animations that involve mathematical concepts.

2. How do I install SymPy?

To install SymPy, you can use pip or conda, depending on which package manager you prefer. Simply run the command "pip install sympy" or "conda install sympy" in your terminal or command prompt.

3. Can I create animations in SymPy using only Python?

Yes, you can create animations in SymPy using only Python. SymPy has a built-in plotting module that allows you to create and save animated plots using pure Python code.

4. Are there any tutorials or resources available for learning how to make animations in SymPy?

Yes, there are many tutorials and resources available for learning how to make animations in SymPy. The SymPy website has a documentation section with tutorials and examples, and there are also many user-created tutorials and videos available online.

5. Can I export my SymPy animation to a video file?

Yes, you can export your SymPy animation to a video file using the matplotlib.animation module. This module allows you to save your animated plots as a video file in various formats, such as mp4 or gif.

Similar threads

  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
312
  • Programming and Computer Science
Replies
9
Views
8K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top