移动补丁而不是删除补丁 [英] Move patch rather than remove it
问题描述
我有一个逐渐显示的图形.由于这应该在一个巨大的数据集和多个子图上同时发生,我计划移动补丁
而不是删除它并从头开始绘制它以加速代码
I have a graph that is gradually revealed. Since this should happen with a huge dataset and on several subplots simultaneously, I was planning to move the patch
rather than remove it and draw it from scratch in order to accelerate the code.
我的问题类似于这个问题一>.但是,我无法解决.
My problem is similar to this question. However, I could not resolve it.
这是一个最小工作示例:
import numpy as np
import matplotlib.pyplot as plt
nmax = 10
xdata = range(nmax)
ydata = np.random.random(nmax)
fig, ax = plt.subplots()
ax.plot(xdata, ydata, 'o-')
ax.xaxis.set_ticks(xdata)
plt.ion()
i = 0
while i <= nmax:
# ------------ Here I would like to move it rather than remove and redraw.
if i > 0:
rect.remove()
rect = plt.Rectangle((i, 0), nmax - i, 1, zorder = 10)
ax.add_patch(rect)
# ------------
plt.pause(0.1)
i += 1
plt.pause(3)
推荐答案
也许这对您有用.无需删除补丁,只需更新其位置和宽度(使用 rect.set_x(left_x)
和 rect.set_width(width)
),然后重新绘制画布.尝试用这个替换你的循环(注意 Rectangle
在循环之前创建一次):
Maybe this works for you. Instead of removing the patch, you just update its position and its width (with rect.set_x(left_x)
and rect.set_width(width)
), and then redraw the canvas. Try replacing your loop with this (note that the Rectangle
is created once, before the loop):
rect = plt.Rectangle((0, 0), nmax, 1, zorder=10)
ax.add_patch(rect)
for i in range(nmax):
rect.set_x(i)
rect.set_width(nmax - i)
fig.canvas.draw()
plt.pause(0.1)
这篇关于移动补丁而不是删除补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!