Dynamically update images in matplotlib based on Python (interactive drawing)

Source: Internet
Author: User

  Recently, in the research of dynamic obstacle avoidance algorithm, it is necessary to display the current position and trajectory of obstacles and moving objects in real-time in Python language, and use Anaconda's Python package collection.  Use the Python3.5 language and Matplotlib in the Spyder to implement dynamic display and interactive plotting of paths (similar to MATLAB). Anaconda is a Python release for scientific computing that supports Linux, MAC, Windows, and provides package management and environmental management capabilities that can easily resolve multiple versions of Python coexistence, switching, and various third-party package installation issues. Anaconda uses the tool/command Conda to manage the package and environment, and it already contains Python and related companion tools. Anaconda Official Address: Https://www.continuum.io/downloads/matplotlib is Python's most famous drawing library, which provides a complete set of command APIs similar to those of MATLAB, making it ideal for interactive mapping. It can also be easily used as a drawing control, embedded in GUI applications. Among them, Matplotlib's Pyplot Sub-Library provides a drawing API similar to MATLAB, which makes it easy for users to quickly draw 2D charts, its documentation is quite complete, and there are hundreds of thumbnails on the gallery page, and the source program opens. Matplotlib official address: http://matplotlib.org/in the research matplotlib dynamic drawing curve method, and Matlab similar to have animation method and interactive drawing, but animation method flexibility is not high, is not suitable for the real-time dynamic display of the path, the interactive drawing mode (interactive mode) is used at the end of this paper.  See http://matplotlib.org/users/shell.html for details. The interactive property of the Pyplot interface controls whether a figure canvas are drawn on every pyplot comma nd. If interactive is false, then the figure state was updated on every plot command, but would only be drawn on Explicit Calls To draw ().  When interactive is true, then every pyplot command triggers a draw. When Pl.ion () is added to a drawing statement, interactive mode is turned on. At this point the Python interpreter interprets all the commands and gives you a picture, but does not end the conversation, but waits for you to communicate with him. If you continue to add statements to your code, you'll see the changes in the graphics in real time after run. When Pl.ioff () is added to a drawing statement or no pl.ion () is added, the interactive mode is turned off. To add Pl.show () at the end of the code, you can display the picture. After explaining all the commands, the Python interpreter gives you a picture and ends the session.  If you continue to add a statement to your code, it won't work until you close the current picture and run again. After using the interactive drawing mode, it is easy to draw the motion trajectory and the current position of the obstacle, deeply feel matplotlib and matlab very similar, basic MATLAB function can be found in the matplotlib, So the code in MATLAB can also be ported to Python quickly! code example:
#-*-coding:utf-8-*-"""Created on Sat 23:28:29 2017@author:wyl"""ImportMatplotlib.pyplot as Plt fromMatplotlib.patchesImportCircleImportNumPy as NPImportMath plt.close ()#CLF () # Clear the CLA () # Clear Axis close () # Close windowfig=plt.figure () Ax=fig.add_subplot (1,1,1) Ax.axis ("Equal")#set the XY axis scale when the image is displayedPlt.grid (True)#Add MeshPlt.ion ()#Interactive mode oniniobsx=0000Iniobsy=4000Iniobsangle=135Iniobsspeed=10*MATH.SQRT (2)#m/SPrint('Start Simulation')Try:     forTinchRange (180):        #Obstacle vessel TrajectoryObsx=iniobsx+iniobsspeed*math.sin (INIOBSANGLE/180*MATH.PI) *T Obsy=iniobsy+iniobsspeed*math.cos (INIOBSANGLE/180*MATH.PI) *T Ax.scatter (obsx,obsy,c='b', marker='.')#Scatter Chart        #Ax.lines.pop (1) Deleting traces        #The figure below, the distance between the two boatsPlt.pause (0.001)exceptException as err:Print(ERR)

Dynamically update images in matplotlib based on Python (interactive drawing)

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.