Tugiyo
Tugiyo

Reputation: 21

Matplotlib: the problem is that len(xy) or (xy) is not acceptable

It is part of long python code that I got the errors:

m = Basemap(...)
lons = ...
lats = ...
x, y = m( lons, lats )
xy = zip(x,y)
print('xy=',xy)
poly = Polygon( xy, facecolor='none', edgecolor=linecolor )

output:
..\site-packages\matplotlib\patches.py", line 1070, in set_xy
    if len(xy) and (xy[0] != xy[-1]).any():
TypeError: len() of unsized object

Matplotlib: the problem is that len(xy) or (xy) is not acceptable.

Because that codes include many variables, The short reproducible codes that I can make (by printing (x,y)):

x,y= [105, 98, 98, 105], [-5, -5, 6, 6]
xy = zip(x,y)
len(xy) 
error: TypeError: object of type 'zip' has no len()

Any suggestions to fix this problem ?

Upvotes: 0

Views: 105

Answers (1)

akseart
akseart

Reputation: 60

The zip function returns a special type of zip object, not a list. To get the list type, you need to do list(zip(x, y))

>>> x,y= [105, 98, 98, 105], [-5, -5, 6, 6]
>>> xy = zip(x,y)
>>> xy
<zip object at 0x100eaf280>
>>> xy = list(zip(x, y))
>>> xy
[(105, -5), (98, -5), (98, 6), (105, 6)]
>>> len(xy)
4

Upvotes: 2

Related Questions