首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的plt动画不工作:"'NoneType‘对象没有属性’画布‘“

我的plt动画不工作:"'NoneType‘对象没有属性’画布‘“
EN

Stack Overflow用户
提问于 2022-05-12 21:02:29
回答 1查看 334关注 0票数 0

我试着在一个学校项目中模拟一个城市的隔离过程。在初始化和隔离之后,我成功地绘制了这座城市,但我没有创建动画来显示城市居民的变化。在我的Ville类中,我有两种方法(我用法语编写代码),它们应该一起制作动画。

代码语言:javascript
运行
复制
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中获得一个数组,然后为每个值绘制一个数组来绘制地图。)

代码语言:javascript
运行
复制
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意味着解决,这个函数循环,直到所有的居民都满意他们所在的地方(这样的方式,社会是机械隔离的)。

初始化的城市看起来像这个初始化城市 (不满意的居民的深色),而隔离的城市看起来像那个隔离城市

但当我进入

代码语言:javascript
运行
复制
a = ville1.resoudre2(compter=True)

我没有得到动画,但只有以下错误消息:

代码语言:javascript
运行
复制
/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)

(第一个问题),然后绘制每个地图(对应于隔离城市的每一步)(第二个问题,参见这里)。当我试着打字

代码语言:javascript
运行
复制
print(a)
from IPython.display import HTML
HTML(a.to_html5_video())

为了制作动画,我只得到

代码语言:javascript
运行
复制
<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'

所以我不明白为什么我会犯这个错误而不仅仅是我的动画.谢谢你的帮助,这是我第一次在这里问问题,所以如果你需要我的代码的更多细节,请不要犹豫!:)

内森

EN

回答 1

Stack Overflow用户

发布于 2022-08-20 11:05:27

有同样的问题,降级Matplotlib为我解决了问题。

pip install matplotlib==3.5.1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72222025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档