This article mainly introduces how to use matplotlib to draw a rectangle in the coordinate system in Python, involving the techniques related to drawing graphics in the matplotlib module, for more information about how to use matplotlib to draw a rectangle in a coordinate system, see the example in this article. Share it with you for your reference. The specific 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:
I hope this article will help you with Python programming.