Python + matplotlib to dynamically draw image instance code (Interactive Drawing ),

Source: Internet
Author: User

Python + matplotlib to dynamically draw image instance code (Interactive Drawing ),

This article focuses on python + matplotlib to dynamically draw images (Interactive Drawing). The detailed introduction and implementation Code are as follows.

Recently, we have been studying the dynamic obstacle avoidance algorithm. During algorithm simulation in Python, We need to display the current location and trajectory of obstacles and animals in real time, and use Anaconda's Python package set, in Spyder, use Python3.5 and matplotlib to dynamically display paths and interactive plotting (similar to Matlab ).

Anaconda is a Python release for scientific computing. It supports Linux, Mac, and Windows systems and provides package management and environment management functions, it can easily solve the coexistence of multiple versions of python, switch, and various third-party package installation problems. Anaconda manages package and environment using tools/command conda, and has included Python and related tools. Anaconda official address: https://www.continuum.io/downloads/

Matplotlib is the most famous Drawing Library in python. It provides a complete set of command APIs similar to matlab and is very suitable for interactive plotting. It can also be easily used as a drawing control and embedded into GUI applications. Matplotlib's pyplot sub-database provides a drawing API similar to matlab to help you quickly draw 2D charts. Its documentation is quite complete and there are hundreds of thumbnails on the Gallery page, open all source programs. Official Address: matplotlib http://matplotlib.org/

In the survey of matplotlib, the animation method and interactive drawing method are similar to matlab. However, the animation method is not flexible and is not suitable for real-time dynamic display of paths, at last, interactive mode is used ). For more information, see http://matplotlib.org/users/shell.html.

The interactive property of the pyplot interface controls whether a figure canvas is drawn on every pyplot command. if interactive is False, then the figure state is updated on every plot command, but will only be drawn on explicit callto draw (). when interactive is True, then every pyplot command triggers a draw.

When pl. ion () is added to the drawing statement, the interaction mode is enabled. Now, after interpreting all the commands, the python interpreter will give you a picture, but it will not end the session, but will wait for you to communicate with him. If you continue to add statements to the Code, after running, you will see the changes in the image in real time. When pl. ioff () is added to the drawing statement or pl. ion () is not added, the interaction mode is disabled. Add pl. show () to the end of the Code to display the image. After interpreting all the commands, the python interpreter will show you a picture and end the session. If you continue to add statements to the code, it will not work unless you close the current image and run it again.

After the interactive drawing mode is adopted, the Movement Track and current position of the obstacle can be easily drawn. matplotlib and matlab are very similar. The basic functions of matlab can be found in matplotlib, so the code in matlab can be quickly transplanted to python!

Sample Code:

#-*-Coding: UTF-8-*-"Created on Sat Mar 25 23:28:29 2017 @ author: wyl" import matplotlib. pyplot as pltfrom matplotlib. patches import Circleimport numpy as npimport math plt. close () # clf () # Clear the graph fig () # Clear the coordinate axis close () # close the window fig = plt. figure () ax = fig. add_subplot (1, 1) ax. axis ("equal") # sets the XY axis ratio plt when the image is displayed. grid (True) # Add a grid plt. ion () # interactive mode onIniObsX = 0000 IniObsY = 4000 IniObsAngle = 135 IniObsSpeed = 10 * math. sqrt (2) # meter/second print ('start simulator') try: for t in range (180): # obstacle vessel track obsX = 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) delete track # The following figure shows the distance between the two ships to plt. pause (0.001) failed t Exception as err: print (err)

Demo result:

Summary

The above is all about the python + matplotlib Implementation of Dynamic drawing of image instance code (Interactive Drawing) in this article, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.