python错误“ValueError:顶点数组无效”

时间:2015-04-15 23:00:01

标签: python scatter-plot

我正在运行以下代码并获得以下错误以制作绘图。当我打印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.

2 个答案:

答案 0 :(得分:0)

需要将数字转换为matplotlib可以使用的类型;现在它只是创建一个字符串列表,因为split()返回一个字符串列表。您可以通过调用int()(或float(),如果数字不是所有整数)来修复此问题,以便在读取文件时转换字符串,这已在分配给{{{ 1}}:

ch

答案 1 :(得分:0)

如果数字周围有引号,则意味着它们会以字符串形式出现,这可能会导致问题。

在你的代码中

         pos.append(line.split()[1])
         cov.append(line.split()[2])

这会将数字作为字符串附加到列表pos和cov中,因此您应该执行上面所做的操作并将它们转换为数字。

         pos.append(int(line.split()[1]))