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

yipeiwu_com5年前Python基础

本文实例讲述了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()

如下图所示:

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

相关文章

Pycharm在创建py文件时,自动添加文件头注释的实例

Pycharm在创建py文件时,自动添加文件头注释的实例

1.选择File -> Settings 2.选择 File and Code Templates -> Files -> Python Script 编辑代码的样式...

一个可以套路别人的python小程序实例代码

先简要介绍一下程序。  程序是使用pycharm工具,python语言所写。程序包括客户端 client.py 和服务器端 server.py 两部分,利用了python中的s...

Python 支持向量机分类器的实现

支持向量机(Support Vector Machine, SVM)是一类按监督学习(supervised learning)方式对数据进行二元分类的广义线性分类器(generalize...

Python的Flask框架中集成CKeditor富文本编辑器的教程

CKeditor是目前最优秀的可见即可得网页编辑器之一,它采用JavaScript编写。具备功能强大、配置容易、跨浏览器、支持多种编程语言、开源等特点。它非常流行,互联网上很容易找到相关...

Python编程中的for循环语句学习教程

Python编程中的for循环语句学习教程

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。 语法: for循环的语法格式如下: for iterating_var in sequence: s...