【发布时间】:2022-08-20 19:05:27
【问题描述】:
我正在尝试为学校项目模拟城市中的隔离过程。我已经设法在初始化时和隔离后绘制了城市,但我没有设法创建显示城市居民移动以显示演变的动画。 我的 Ville 课程中有两种方法(我正在用法语编码),它们应该一起制作动画。
def afficher(self, inclure_satisfaction=False, inclure_carte_categories=False, size=5):
carte = self.carte_categories(inclure_satisfaction=inclure_satisfaction)
if inclure_carte_categories:
print(\"Voici la carte des catégories (à titre de vérification)\")
print(carte)
mat_rs = masked_array(carte, carte!=1.5)
mat_ri = masked_array(carte, carte!=1)
mat_bs = masked_array(carte, carte!=2.5)
mat_bi = masked_array(carte, carte!=2)
plt.figure(figsize=(size, size))
affichage_rs = plt.imshow(mat_rs, cmap=cmap_rs)
affichage_ri = plt.imshow(mat_ri, cmap=cmap_ri)
affichage_bs = plt.imshow(mat_bs, cmap=cmap_bs)
affichage_bi = plt.imshow(mat_bi, cmap=cmap_bi)
return plt.figure()
(此函数通过首先从方法中获取一个数组来绘制地图carte_categories根据每个居民的类别,然后为每个要绘制的值获取一个数组)
def resoudre2(self):
fig = plt.figure(figsize=(5,5))
list_of_artists = []
while self.habitants_insatisfaits != []:
self.demenagement_insatisfait_aleatoire()
list_of_artists.append([self.afficher(inclure_satisfaction=True)])
ani = ArtistAnimation(fig, list_of_artists, interval=200, blit=True)
return ani
(habitants_insatisfaits 是一个包含“不满意的居民”的列表:他们周围有两个属于他们的类别的人,所以他们想搬到其他地方;所以 resoudre 意味着解决,这个函数循环直到所有居民都满意他们是(这样社会就被机械地隔离了)
初始化的城市看起来像这样initialized city(不满意的居民使用深色),隔离的城市看起来像segregated city。
但是当我进入
a = ville1.resoudre2(compter=True)
我没有得到动画,但只有这个错误消息:
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:211: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:206: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
Traceback (most recent call last):
File \"/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/__init__.py\", line 196, in process
func(*args, **kwargs)
File \"/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py\", line 951, in _start
self._init_draw()
File \"/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py\", line 1533, in _init_draw
fig.canvas.draw_idle()
AttributeError: \'NoneType\' object has no attribute \'canvas\'
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:452: UserWarning: Warning: converting a masked element to nan.
dv = np.float64(self.norm.vmax) - np.float64(self.norm.vmin)
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:459: UserWarning: Warning: converting a masked element to nan.
a_min = np.float64(newmin)
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:464: UserWarning: Warning: converting a masked element to nan.
a_max = np.float64(newmax)
<string>:6: UserWarning: Warning: converting a masked element to nan.
/usr/local/lib/python3.7/dist-packages/matplotlib/colors.py:993: UserWarning: Warning: converting a masked element to nan.
data = np.asarray(value)
(第一个问题)然后绘制每张地图(对应于隔离城市的每一步)(第二个问题;参见here)。当我尝试输入
print(a)
from IPython.display import HTML
HTML(a.to_html5_video())
绘制动画,我只得到
<matplotlib.animation.ArtistAnimation object at 0x7f4cd376bfd0>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-20-d7ca1fcdadb6> in <module>()
1 print(a)
2 from IPython.display import HTML
----> 3 HTML(a.to_html5_video())
2 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/animation.py in _init_draw(self)
1531 # Flush the needed figures
1532 for fig in figs:
-> 1533 fig.canvas.draw_idle()
1534
1535 def _pre_draw(self, framedata, blit):
AttributeError: \'NoneType\' object has no attribute \'canvas\'
所以我不明白为什么我得到这个错误而不仅仅是我的动画...... 感谢您的帮助,这是我第一次在这里提问,所以如果您需要有关我的代码的更多详细信息,请不要犹豫! :)
弥敦道
标签: matplotlib animation canvas figure nonetype