Python uses matplotlib to implement a method of drawing a rectangle in a coordinate system

Source: Internet
Author: User
This example describes how Python uses matplotlib to implement a rectangle in a coordinate system. Share to everyone for your reference. The implementation method is as follows:

Import Matplotlib.pyplot as Pltfrom matplotlib.patches import Rectangleclass Annotate (object):  def __init__ (self):    self.ax = PLT.GCA ()    self.rect = Rectangle ((0,0), 1, 1)    self.x0 = none    self.y0 = none    self.x1 = none
  self.y1 = None    self.ax.add_patch (self.rect)    self.ax.figure.canvas.mpl_connect (' button_press_event ', self.on_press)    self.ax.figure.canvas.mpl_connect (' button_release_event ', self.on_release)  def on_press ( Self, event):    print ' press '    self.x0 = event.xdata    self.y0 = Event.ydata  def on_release (self, event ):    print ' release '    self.x1 = event.xdata    self.y1 = Event.ydata    self.rect.set_width (self.x1- self.x0)    self.rect.set_height (self.y1-self.y0)    self.rect.set_xy ((self.x0, self.y0))    Self.ax.figure.canvas.draw () A = Annotate () plt.show ()

As shown in the following:

Hopefully this article will help you with Python programming.

  • Related Article

    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.