我有一个矩形的形状是在一个图像上绘制。我想移动的形状,即改变其坐标,绘制新的形状,并删除旧的(背景图像保持不变)
类似于这个代码的东西。它显示了一个矩形三次,但我想当绿色形状被绘制时,红色的要被删除,同样的蓝色。当蓝色出现时,删除绿色。
import matplotlib.pyplot as plt
import numpy as np
# Get an example image
image_file = r"image_splash.jpg"
img = plt.imread(image_file)
# Make some example data
x = np.array([475, 475, 675, 675, 475])
y = np.array([100, 200, 200, 100, 100])
# Create a figure
_dpi = 72
fig = plt.figure(1, figsize=(1000/_dpi, 1000/_dpi), dpi=_dpi)
ax = fig.add_subplot(1, 1, 1)
ax.set_aspect('equal')
# Show the image
ax.imshow(img)
# Now, loop through the offset array and update the shape
# (This obviously doesnt work, it plots all three shapes)
rgb = ['r', 'g', 'b']
offset = [0, 150, 300]
plt.plot(x, y + offset[0], c=rgb[0]) # draw the red rect
plt.plot(x, y + offset[1], c=rgb[1]) # should remove the red and draw the green
plt.plot(x, y + offset[2], c=rgb[2]) # should remove the green and draw the blue
1条答案
按热度按时间cwdobuhd1#
您可以尝试matplotlib的动画API,如下所示