Python learning-use matplotlib to draw dynamic multi-graph and pythonmatplotlib

Source: Internet
Author: User

Python learning-use matplotlib to draw dynamic multi-graph and pythonmatplotlib

Matplotlib is often used to draw mathematical function diagrams recently. However, it is not until today that matplotlib is used to draw dynamic graphs and multiple dynamic graphs.

1. Reference text

First of all, I would like to thank the authors of several articles and help me learn how to draw them. You can also refer to their texts.

2. program source code

First, paste the program source code and explain it step by step.

<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 figure, 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([], [], lw=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, 100)       y = np.sin(2 * np.pi * (x - 0.01 * i))      line.set_data(x, y)          x2 = np.linspace(0, 2, 100)       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 did with this program.

3.1 create subgraphs and blank lines
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, we can see that a figure object is created first, and then fig. add_subplot (, 1, xlim = (0, 2), ylim = (-4, 4) is to create a subgraph, for more information, see [4] Introduction to subgraphs in "using Python for scientific computing.
3.2 Create a function called when an animation occurs.
Init () is the function that our animation calls when creating the base frame of an animation. Here we use a very simple function that does nothing about line. This function must return the line object, which is very important because it can tell the content to be updated after the animation, that is, the content of the action is line. -- From (http://mytrix.me/2013/08/matplotlib-animation-tutorial)
In the above section, I explained what the Init () function is doing. Because my program is special, I hope to display two subgraphs, 3.1, in one graph. So I have to create two blank line lines in the two axes ax1 and ax2, line2 and return these two lines in Init.


Fig 3.1

def init():      line.set_data([], [])      line2.set_data([], [])      return line,line2
3.3 animation function next you need an animation function to modify your graph in this animation function. Similarly, I need to display two items in a graph, so in the animation function, I updated two graphs and returned line and line2
def animate(i):    x = np.linspace(0, 2, 100)       y = np.sin(2 * np.pi * (x - 0.01 * i))      line.set_data(x, y)          x2 = np.linspace(0, 2, 100)       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,line2

3.4 at the end of the animation display, you need to use the following two statements to display the animation. Here, you need to adjust the interval parameter (this parameter specifies the time interval, otherwise, the same situation will occur (when you use the bits = True option ). At the same time (http://mytrix.me/2013/08/matplotlib-animation-tutorial/) for us to illustrate the role of several parameters, I'm not repeat:

This object needs to exist continuously. To assign it to a variable, we select a 100-frame animation, why is it 100 frames ......, In addition, it is not necessarily a number here. It can be a generator or iterable. For details, see the API description.) and the interval between frames is 20 ms. bits is a very important keyword, it tells the animation that only the modified part is repainted. Combined with the time saved above, BITs = true will make the animation display very quickly.


Fig 3.2

anim1=animation.FuncAnimation(fig, animate, init_func=init,  frames=50, interval=10)  plt.show()  

End 3.5

The above work has been explained. Let's take a look at the results. I am also a beginner in programming. I am very grateful to anyone who wants to know more about my blog.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.