python error "ValueError: Invalid vertices array"
python error "ValueError: Invalid vertices array"
我是运行下面的代码,在制作绘图时出现以下错误。当我打印 cov 变量时,其中的数字用单引号括起来。我假设这是问题所在?当我尝试绘制 x 和 y 时,它工作得很好。我是新 python 用户。有什么建议么?谢谢!
#!/bin/python
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from sys import argv
#from numpy import array
#array = numpy.float64(array)
script, filename = argv
file = open(filename)
pos = []
cov = []
for line in file:
if (line.split()[0])=='1':
ch = int(line.split()[0])
#adjpos = int(line.split()[1])
pos.append(line.split()[1])
cov.append(line.split()[2])
file.close()
print cov
x = [0, 1, 2, 3]
y = [5, 10, 5, 5]
print x
print y
fig = plt.figure(figsize=(16,3),edgecolor='w')
plt.scatter(pos,cov,marker='.',s=1)
plt.show()
Traceback (most recent call last):
File "/Users/eflannery/Dropbox/plotSamFlags.py", line 37, in <module>
plt.scatter(pos,cov,marker='.',s=1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2557, in scatter
ret = ax.scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, faceted, verts, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 5876, in scatter
self.add_collection(collection)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 1445, in add_collection
self.update_datalim(collection.get_datalim(self.transData))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/collections.py", line 160, in get_datalim
offsets = transOffset.transform_non_affine(offsets)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 1925, in transform_non_affine
self._a.transform(points))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 1415, in transform
return affine_transform(points, mtx)
ValueError: Invalid vertices array.
数字需要转换为matplotlib可以使用的类型;现在它只是制作一个字符串列表,如 split()
returns 一个字符串列表。您可以通过在读取文件时调用 int()
(或 float()
,如果数字不全是整数)来转换字符串来解决此问题,您已经在分配给 ch
:
for line in file:
if (line.split()[0])=='1':
ch = int(line.split()[0])
pos.append(int(line.split()[1]))
cov.append(int(line.split()[2]))
如果数字周围有引号,则表示它们出现在字符串中,这很可能会导致问题。
在你的代码中
pos.append(line.split()[1])
cov.append(line.split()[2])
这会将数字作为字符串附加到列表 pos 和 cov,因此您应该执行上述操作并将它们转换为数字。
pos.append(int(line.split()[1]))
我是运行下面的代码,在制作绘图时出现以下错误。当我打印 cov 变量时,其中的数字用单引号括起来。我假设这是问题所在?当我尝试绘制 x 和 y 时,它工作得很好。我是新 python 用户。有什么建议么?谢谢!
#!/bin/python
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from sys import argv
#from numpy import array
#array = numpy.float64(array)
script, filename = argv
file = open(filename)
pos = []
cov = []
for line in file:
if (line.split()[0])=='1':
ch = int(line.split()[0])
#adjpos = int(line.split()[1])
pos.append(line.split()[1])
cov.append(line.split()[2])
file.close()
print cov
x = [0, 1, 2, 3]
y = [5, 10, 5, 5]
print x
print y
fig = plt.figure(figsize=(16,3),edgecolor='w')
plt.scatter(pos,cov,marker='.',s=1)
plt.show()
Traceback (most recent call last):
File "/Users/eflannery/Dropbox/plotSamFlags.py", line 37, in <module>
plt.scatter(pos,cov,marker='.',s=1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2557, in scatter
ret = ax.scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, faceted, verts, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 5876, in scatter
self.add_collection(collection)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 1445, in add_collection
self.update_datalim(collection.get_datalim(self.transData))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/collections.py", line 160, in get_datalim
offsets = transOffset.transform_non_affine(offsets)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 1925, in transform_non_affine
self._a.transform(points))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 1415, in transform
return affine_transform(points, mtx)
ValueError: Invalid vertices array.
数字需要转换为matplotlib可以使用的类型;现在它只是制作一个字符串列表,如 split()
returns 一个字符串列表。您可以通过在读取文件时调用 int()
(或 float()
,如果数字不全是整数)来转换字符串来解决此问题,您已经在分配给 ch
:
for line in file:
if (line.split()[0])=='1':
ch = int(line.split()[0])
pos.append(int(line.split()[1]))
cov.append(int(line.split()[2]))
如果数字周围有引号,则表示它们出现在字符串中,这很可能会导致问题。
在你的代码中
pos.append(line.split()[1])
cov.append(line.split()[2])
这会将数字作为字符串附加到列表 pos 和 cov,因此您应该执行上述操作并将它们转换为数字。
pos.append(int(line.split()[1]))