Skip to content
Advertisement

How to put text at a fixed position when updating an animation

Here’s the whole code: (main problem is explained below)

JavaScript

I used matplotlib.animation to animate the graph. Everything went good.
Now I wanted to put the seed (the number) in top right corner of the graph, so I did this:
plt.text((i+5)*0.8,o*0.88, ("Seed: " +str(N)))
This add the text at every frame iteration (as I wanted) but the previous text remains in the graph and move back (because my axes updates).
So how can I put only one text at a fixed position?
I am using PyCharm on Windows 10.
(I am new to matplotlib and python, so any other suggestions are also appreciated.)

Advertisement

Answer

You have to initialize a matplotlib.axes.Axes.text and then dynamically set its text and position with set_text and set_position methods. You have also to return it in init and animate functions.

JavaScript

enter image description here

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