In the near future, we often use matplotlib to draw mathematical function diagrams, but how to use matplotlib to draw dynamic graphs, and to draw dynamic multi-graphs, until today to learn.

1. Reference text

First of all thank the author of several words, help me learn how to draw, we can also refer to their text.

- Http://blog.csdn.net/rumswell/article/details/11731003: The author gives a number of examples of the source code, but there is no detailed explanation, the source code before the secret, see for yourself.
- http://mytrix.me/2013/08/matplotlib-animation-tutorial/: The author's explanation is very detailed, mainly about the Matplotlib official example, you can refer to.

- Http://blog.yangyu.me/2014/08/06/matplotlib-graphing-series/: The author, given a different example, and very detailed, tells us how to learn the matplotlib drawing step-by-step
- HTTP://SEBUG.NET/PAPER/BOOKS/SCIPYDOC/MATPLOTLIB_INTRO.HTML#ID4: Use Python to do scientific calculations, very good and informative books.

2. Program source code
First put out the program source code, step by step to explain.

<span style= "font-family:simsun;font-size:10px;" >import NumPy as NP from matplotlib import Pyplot as plt from matplotlib import Animation # First set up the fi Gure, the axis, and the plot element we want to animate fig = plt.figure () ax1 = Fig.add_subplot (2,1,1,xlim= (0, 2), Ylim = ( -4, 4)) Ax2 = Fig.add_subplot (2,1,2,xlim= (0, 2), ylim= ( -4, 4)) Line, = Ax1.plot ([], [], lw=2) line2, = Ax2.plot ([], [], L w=2) def init (): Line.set_data ([], []) Line2.set_data ([], []) return line,line2# animation function. This is called sequentially def animate (i): x = Np.linspace (0, 2, +) y = Np.sin (2 * np.pi * (x-0.01 * i)) Line.set_data (x, y) x2 = np.linspace (0, 2, +) y2 = Np.cos (2 * np.pi * (x2-0.01 * i)) * Np.sin (2 * NP) . Pi * (x-0.01 * i)) line2.set_data (x2, y2) return line,line2anim1=animation. Funcanimation (Fig, animate, Init_func=init, frames=50, interval=10) plt.show () </span>

3. Explanation

Now, let's explain what I've done with this program.

3.1 Creating a sub-chart, blank line

Fig = plt.figure () ax1 = Fig.add_subplot (2,1,1,xlim= (0, 2), ylim= ( -4, 4)) Ax2 = Fig.add_subplot (2,1,2,xlim= (0, 2), ylim= (-4 , 4)) Line, = Ax1.plot ([], [], lw=2) line2, = Ax2.plot ([], [], lw=2)

in the above program can be seen, first established a figure object, then Fig.add_subplot (2,1,1,xlim= (0, 2), ylim= (-4, 4)) is to create a sub-map, about the concept and practice of the sub-diagram, you can see the following text "4 " make scientific calculations with Python" for A description of the sub-graph.

3.2 Create a function that is called when an animation occurs

Init () is a function that our animations call when creating an animated foundation frame (base frame). Here we use a very simple function that doesn't do anything to line. This function must return the line object, this is very important, because this can tell the animation to update the content, that is, the content of the action is line. --From (http://mytrix.me/2013/08/matplotlib-animation-tutorial/ )

The
above paragraph explains what the Init () function is for, because my program is special and I want to be able to display two sub-graphs, 3.1 in a single graph. So I have to create two blank lines in two axes ax1 and ax2 Line,line2 and return the two line in Init ().

Figure 3.1

DEF init (): line.set_data ([], []) line2.set_data ([], []) return line,line2

3.3 Animation Functions Next you need an animation function, in this animation function to modify your diagram, the same I need a picture to show two things, so in the animation function, I updated two graphs, and returned to line and Line2
def animate (i): x = Np.linspace (0, 2, +) y = np.sin (2 * np.pi * (x-0.01 * i)) line.set_data (x, y) x2 = Np.linspace (0, 2, +) y2 = Np.cos (2 * np.pi * (x2-0.01 * i)) * Np.sin (2 * np.pi * (x-0.01 * i)) Line2.set_dat A (x2, y2) return line,line2

3.4 displaying animations finally you need to use the following two statements to display the animation, here is a note, you need to adjust the interval parameter (this parameter indicates the time interval) size, otherwise there will be 3.2 the same situation (when you use, blit=true this option). At the same time (http://mytrix.me/2013/08/matplotlib-animation-tutorial/) gave us a description of the role of several parameters, I am not in the retelling:
This object needs to persist, all we have to assign it to a variable, we choose a 100-frame animation (Translator Note: Your top of the code or 200 frames, how to get here to become 100 frames ...), in addition, here is not necessarily a number, it can be a generator Or iterable, see API description) and the interval between frames and frames 20ms,blit is a very important keyword that tells the animation to redraw only the modified part, combined with the time saved above, Blit=true will make the animation appear very, very fast.

Figure 3.2
Anim1=animation. Funcanimation (Fig, animate, Init_func=init, frames=50, interval=10) plt.show ()

3.5 End
The above work has been explained, to see the results. Program Write bad, I also just beginner, hope to see the blog people, can give me a lot of advice, greatly appreciated.

Python Learning-use matplotlib to draw dynamic multi-graphs