【发布时间】:2021-05-24 15:50:08
【问题描述】:
我正在尝试用 Python 专门使用 matplotlib 编写一个小型视频游戏。这个想法是让一个小坐标轴对象上有一个角色,该坐标轴对象在更大的地图上移动。我设法更新了轴的位置,但该方法似乎重绘了完整的图形而不是小轴对象(通过在源代码中调用 plt.draw() ),因此角色后面的重地图也是如此,使得处理速度极慢。
有没有办法在 matplotlib 中重绘特定的轴对象?
我在终端的 IPython shell 中工作。
这是一个模拟我的问题的测试代码,我得到的东西比理想情况慢 10 倍:
import matplotlib.pylab as plt
import time
import numpy as np
dtframe = 0.02 #time between plot updating in seconds
frames = 30
plt.subplots(5,5) #to simulate an heavy background matplotlib figure
ax = plt.axes([0.1,0.5,0.03,0.03])
plt.show(block=False)
begin = time.time()
for j in np.linspace(0.1,0.9,frames):
plt.pause(dtframe)
ax.set_position([j,0.5,0.03,0.03])
#ax.set_position([j,0.5,0.03,0.03], redraw_only_axes=True) ???
print('Current time [s] :',time.time() - begin)
print('Expected time [s] :',dtframe*frames)
#Current time [s] : 5.70146918296814
#Expected time [s] : 0.6
【问题讨论】:
标签: python matplotlib draw axes