Методы рисования ограничивающих рамок с использованием Matplotlib в Python

Чтобы нарисовать ограничивающие рамки с помощью Matplotlib, вы можете использовать следующие методы:

Метод 1: использование matplotlib.patches.Rectangle

import matplotlib.pyplot as plt
import matplotlib.patches as patches
# Create a figure and axis
fig, ax = plt.subplots()
# Draw a rectangle
rect = patches.Rectangle((50, 100), 200, 150, linewidth=1, edgecolor='r', facecolor='none')
# Add the rectangle to the axis
ax.add_patch(rect)
# Display the plot
plt.show()

Метод 2: использование matplotlib.pyplot.rectangle

import matplotlib.pyplot as plt
# Create a figure and axis
fig, ax = plt.subplots()
# Draw a rectangle
rect = plt.Rectangle((50, 100), 200, 150, linewidth=1, edgecolor='r', facecolor='none')
# Add the rectangle to the axis
ax.add_patch(rect)
# Display the plot
plt.show()

Метод 3: использование matplotlib.patches.Polygon

import matplotlib.pyplot as plt
import matplotlib.patches as patches
# Create a figure and axis
fig, ax = plt.subplots()
# Define the coordinates of the bounding box vertices
x = [50, 250, 250, 50]
y = [100, 100, 250, 250]
# Create a polygon patch
polygon = patches.Polygon(xy=list(zip(x, y)), linewidth=1, edgecolor='r', facecolor='none')
# Add the polygon to the axis
ax.add_patch(polygon)
# Display the plot
plt.show()

Метод 4: использование matplotlib.pyplot.plot

import matplotlib.pyplot as plt
# Define the coordinates of the bounding box vertices
x = [50, 250, 250, 50, 50]
y = [100, 100, 250, 250, 100]
# Plot the bounding box
plt.plot(x, y, linewidth=1, color='r')
# Display the plot
plt.show()