Pedro Azambuja
Pedro Azambuja

Reputation: 3

TypeError:"object of type 'int' has no len()", even though I don't use the function "len()"

I've been working on a project to predict scallops population variation, but it seems my code has a problem which i cannot identify. If anyone could help I'd be thankfull:

import matplotlib.pyplot as plt
S=[4000]
a1 = 0.08
t = 50
for e in range(t-1):
    S.append(S[e]*a1+ S[e])
time = range(0,t)
plt.plot(time, S)
plt.axis(0, t, 0, 18000)
plt.xlabel("Time")
plt.ylabel("Scallops")
plt.title(r'Scallops Population')
plt.show()

This script returns the proper graph, but also returns the following error message: "object of type 'int' has no len()" Full Traceback:

Traceback (most recent call last):    
  File "<ipython-input-79-1e9eb32fa3a3>", line 1, in <module>
    runfile('C:/arquivos python/Entrega 4.py', wdir='C:/arquivos python')
  File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "C:/arquivos python/Entrega 4.py", line 16, in <module>
    plt.axis(0, t, 0, 18000)
  File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 1537, in axis
    return gca().axis(*v, **kwargs)
  File "C:\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 1633, in axis
    if len(v) != 4:
TypeError: object of type 'int' has no len()

Upvotes: 0

Views: 1429

Answers (1)

trincot
trincot

Reputation: 350107

The axis method has this signature:

matplotlib.pyplot.axis(*v, **kwargs)

[...]

>>> axis(v)

sets the min and max of the x and y axes, with v = [xmin, xmax, ymin, ymax].

So you need to pass a list with 4 values as first argument, instead of 4 arguments:

plt.axis([0, t, 0, 18000])

You can see in the stack trace how the library is checking that the first argument is a list with 4 values:

if len(v) != 4:

But in your call, v is 0, while it should have been [0, t, 0, 18000].

Upvotes: 1

Related Questions