Home  >  Article  >  Backend Development  >  How to draw a rectangle in python

How to draw a rectangle in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-25 17:15:2110812browse

How to draw a rectangle in python

How to draw a rectangle in python? Let me explain the specific steps below:

The Rectangle parameter of plt:

The first parameter is the coordinate (x, y), which is the starting point coordinate of the rectangle drawing. This starting point The coordinates are not drawn blindly from the lower left corner, but correspond to the origin of the coordinates in the entire figure, which is (0, 0).

The second parameter is the width of the rectangle

The third parameter is the height of the rectangle

Note: In the fast rcnn code, the roi box is drawn in the image, The origin of the image is in the upper left corner, but the origin of the coordinate axis is in the lower left corner, so even if the parameters in the Rectangle are exactly the same, what is drawn in the image and the coordinate axis are different.

Related recommendations: "Python Video Tutorial"

plt.axis('off') means not to display the coordinate axis

The first code:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
rect = plt.Rectangle((0.1,0.1),0.5,0.3)
ax.add_patch(rect)
plt.show()

Second code:

import matplotlib.pyplot as plt
fig = plt.figure()  #创建图
ax = fig.add_subplot(111)  #创建子图
plt.gca().add_patch(plt.Rectangle((0.1,0.1),0.5,0.3))
plt.show()

The current chart and subgraph can be obtained using plt.gcf() and plt.gca(), which represent Get Current Figure and Get Current respectively. Axes. In the pyplot module, many functions process the current Figure or Axes object. For example: plt.plot() will actually obtain the current Axes object ax through plt.gca(), and then call ax.plot( ) method to achieve real drawing.

The above is the detailed content of How to draw a rectangle in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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