Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 填充多边形,列表超出范围_Python_Matplotlib - Fatal编程技术网

Python 填充多边形,列表超出范围

Python 填充多边形,列表超出范围,python,matplotlib,Python,Matplotlib,我正在尝试从csv文件填充多边形,我正在使用以下代码,第y.append(str(row[1]))行中出现问题。 索引器:列表索引超出范围“ 代码: import matplotlib.pyplot as plt import csv x = [] y = [] # coord = [[1,1], [2,1], [2,2], [1,2],] with open('screen.txt','r') as csvfile: coord

我正在尝试从csv文件填充多边形,我正在使用以下代码,第
y.append(str(row[1]))行中出现问题。

索引器:列表索引超出范围“

代码:

    import matplotlib.pyplot as plt
    import csv

    x = []
    y = []
    # coord = [[1,1], [2,1], [2,2], [1,2],]
    with open('screen.txt','r') as csvfile:
        coord = csv.reader(csvfile, delimiter=',')
        for row in coord:
            x.append(str(row[0]))
            y.append(str(row[1]))

    coord.append(coord[0]) #repeat the first point to create a 'closed loop'

    xs, ys = zip(*coord) #create lists of x and y values

    plt.figure()
    plt.plot(xs,ys)
    plt.fill(xs, ys, color= "r")
    plt.show()

问题中的代码假设文件“screen.txt”如下所示:

1,1
2,1
2,2
1,2
然后,创建一个变量
coords
,它是一个
\u csv.reader对象
,而后续代码的行为就像
coords
是一个简单的列表。reader对象是一个读取文件的特殊对象,允许对coords中的行进行
构造。但它只在需要时读取部分文件(以防止在只需要几行的情况下读取数百万行)。关闭文件后(即在打开(…
)的
之后),reader对象将不可用

作为简单的解决方法,如果文件不是很长,您可以将coords转换为一个列表:
coords=list(coords)
。从那时起,它可以用作普通列表

顺便说一下,如果文件如上所述,则会为行
coord.append(coord[0])
抛出错误
列表索引超出范围

另一个可能的原因可能是您的输入文件末尾包含空行。如果最后一行只包含几个空格,则在
x.append(str(行[0]))
y.append(str(行[1]))
处会出现第一个错误。但是删除空行后,在
coord.append(coord[0])处仍然会出现错误

修改后的代码(省略列表x和y,因为它们在本版本中未真正使用):

它显示了一个漂亮的正方形: PS:上面的版本将坐标存储为字符串,而直接使用数字可能更有效

更好、更可读的版本(由@ImportanceOfBeingErnest建议):


问题可能出在您的csv文件中。您确定这些值用逗号分隔吗?是的,它用逗号分隔。csv文件的内容是什么?谢谢,问题出在文件上。。
import csv
from matplotlib import pyplot as plt

# coord = [[1,1], [2,1], [2,2], [1,2],]
with open('screen.txt', 'r') as csvfile:
    coord = csv.reader(csvfile, delimiter=',')
    coord = list(coord)  # we can step out of the 'with', because the file isn't needed
                         # anymore once we have the real list

coord.append(coord[0])  # repeat the first point to create a 'closed loop'
xs, ys = zip(*coord)    # create lists of x and y values

plt.figure()
plt.plot(xs, ys)
plt.fill(xs, ys, color="r")
plt.gca().set_aspect('equal') # needed so squares look square, x and y axis get the same pixels per unit
plt.show()
import csv
from matplotlib import pyplot as plt

x = []
y = []
# coord = [[1,1], [2,1], [2,2], [1,2],]
with open('screen.txt', 'r') as csvfile:
    coord = csv.reader(csvfile, delimiter=',')
    for row in coord:
        x.append(float(row[0]))
        y.append(float(row[1]))
x.append(x[0])
y.append(y[0])

plt.figure()
plt.plot(x, y)
plt.fill(x, y, color="orchid")
plt.show()