如何更新 Matplotlib 上的形状?
How to update a shape on Matplotlib?
我在图像上绘制了一个矩形。我想移动那个形状,即改变它的坐标,绘制新形状并删除旧形状(背景图像保持不变)
类似于此代码。它显示了一个矩形三次,但我希望在绘制绿色形状时删除红色形状,蓝色形状也类似。当蓝色出现时去除绿色。
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
你可以试试matplotlib的动画api如下图
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
(ln,) = plt.plot([], [], "r")
x = np.array([475, 475, 675, 675, 475])
y = np.array([100, 200, 200, 100, 100])
offset = [0, 150, 300]
colors = {
0: "red",
150: "green",
300: "blue",
}
def init():
ax.set_xlim(0, 1000)
ax.set_ylim(0, 1000)
return (ln,)
def update(_offset):
ln.set_data(x, y + _offset)
ln.set_color(colors[_offset])
return (ln,)
def gety():
for i in range(1000):
yield offset[i % 3]
ani = FuncAnimation(fig, update, frames=gety(), init_func=init, blit=True)
plt.show()
我在图像上绘制了一个矩形。我想移动那个形状,即改变它的坐标,绘制新形状并删除旧形状(背景图像保持不变)
类似于此代码。它显示了一个矩形三次,但我希望在绘制绿色形状时删除红色形状,蓝色形状也类似。当蓝色出现时去除绿色。
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
你可以试试matplotlib的动画api如下图
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
(ln,) = plt.plot([], [], "r")
x = np.array([475, 475, 675, 675, 475])
y = np.array([100, 200, 200, 100, 100])
offset = [0, 150, 300]
colors = {
0: "red",
150: "green",
300: "blue",
}
def init():
ax.set_xlim(0, 1000)
ax.set_ylim(0, 1000)
return (ln,)
def update(_offset):
ln.set_data(x, y + _offset)
ln.set_color(colors[_offset])
return (ln,)
def gety():
for i in range(1000):
yield offset[i % 3]
ani = FuncAnimation(fig, update, frames=gety(), init_func=init, blit=True)
plt.show()