【问题标题】:Python: TypeError: draw() takes 0 positional arguments but 1 was givenPython:TypeError:draw()采用0个位置参数,但给出了1个
【发布时间】:2015-12-13 23:31:47
【问题描述】:

我正在制作人生游戏,我想要一个窗口(在我的代码中称为 Grid),它可以刷新第二个 Grid(称为 SecGrid)信息

import numpy as np
import pylab as pl
from random import randint


i = 0
o = 0


#Chose Grid size
m = int(input("Enter the width and length of the Grid: ",))
n = m
print("Your Grid will be",m,"x",n)
Grid = np.zeros((m+2,n+2))
SecGrid = np.zeros((m+2,n+2))

#Random /Grid filling in
while(i <= n/3*m):
    i = i+1
    Grid[randint(1,m), randint(1,n)] = 1
img = pl.imshow(Grid, cmap = 'PuRd', interpolation = 'none')

#Main algo
while (o < 2):
    print(o)
    v = input("continue: ")
    o = o + 1
    for i in range(1,m):
        for j in range(1,n):
            #Nbr of entities
            nbr = 0
            for k in range(-1,1):
                for l in range(-1,1):
                    nbr = nbr + Grid[i+k,j+l]
            #cells that are alive
            if Grid[i,j] == 1:
                if nbr > 1 and nbr < 4:
                    SecGrid[i,j] = 1
                else:
                    SecGrid[i,j] = 0
                #cells that are dead
            else:
                    if nbr == 3:
                        SecGrid[i,j] = 1
                    else: 
                        SecGrid[i,j] = 0

如何用新信息刷新我的网格?

我试过了

Grid = SecGrid
pl.draw(img)
img.set_data(Grid)

但我是 python 新手,所以我不知道 ._。请帮助我:3

【问题讨论】:

  • 能否请您添加完整的回溯?
  • 您的代码中的pl 是什么?

标签: python


【解决方案1】:

pylab draw 方法不带任何参数。看看从命令行运行python的draw文档。

For example:
$ python
>>> import pylab as pl
>>> help(pl.draw)

关于您的问题“如何使用新信息刷新我的网格?”,因为您正在更新“随机/网格填充”评论下方的网格中的值,我假设您询问如何更新该数据在您的 img 对象中。如果是这样,请参阅以下内容:

# Note: entered in your code line by line into python at command line then
>>> help(img.set_data)

Help on method set_data in module matplotlib.image:

set_data(self, A) method of matplotlib.image.AxesImage instance
    Set the image array

    ACCEPTS: numpy/PIL Image A

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多