Python使用matplotlib实现在坐标系中画一个矩形的方法

ringa_lee
Release: 2018-05-10 16:44:05
Original
4419 people have browsed it

本文实例讲述了Python使用matplotlib实现在坐标系中画一个矩形的方法。分享给大家供大家参考。具体实现方法如下:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
class 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()
Copy after login

如下图所示:

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!