<分区> 分区>
我正在尝试使用 matplotlib 制作动态网格。然而,在仅运行大约 10 次交互之后,时间步长似乎变得越来越长,即使我已将其设置为固定数字(代码中为 0.1 秒)。在大约 70 次迭代后,它变得非常慢(每次更改大约 3 秒)。下面是代码示例:
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
map=[[0,0,0],[0,0,0],[0,0,0]]
plt.ion()
fig, ax = plt.subplots(1, 1, tight_layout=True)
color = ['white', 'black', 'red']
my_cmap = colors.ListedColormap(color)
# draw the grid
for x in range(len(map)):
ax.axhline(x, lw=2, color='k')
ax.axvline(x, lw=2, color='k')
ax.axis('off')
plt.draw()
for i in range(0,100):
x = np.random.randint(0,3)
y = np.random.randint(0,3)
map[x][y]+=1
map[x][y]*=-1
# draw the boxes
ax.imshow(map, interpolation='none', cmap=my_cmap, extent=[0, len(map), 0, len(map)])
# turn off the axis labels
plt.pause(0.1)
我使用的是python3.7。我绘制图表的方式有问题吗?或者有没有更好的方法来绘制它,以便在一些迭代后变化不会减慢?非常感谢您的帮助!
