尝试将 y 变量读入 pyplot.scatter 时出现 IndexError
IndexError when trying to read y variable into pyplot.scatter
我试图创建的最终产品是一个程序,它从 csv(已附加,称为 OutputC.csv)获取输入,并允许用户从他们 select 的列中绘制散点图.我决定先创建预 select 列的图形,然后再使用 rawinput 和变量。所以,我遇到的问题是 python 将数据读入 pyplot 的方式。数据取自输入 csv 并写入它自己的文件 (Qout14.csv),我试图从中绘制。不幸的是,即使 x 和 y 变量列表的长度相同,我也会收到 IndexError。在做了一些阅读后,我想知道问题是否可能是 y 数据没有逗号分隔实例中的数据,所以它可能只是将所有 y 作为字符串读取,所以它没有任何其他内容名单。令人困惑的部分是同一行代码适用于变量的另一端,即 x。谁能指出我的错误在哪里?
Example of Inputs(Columns 1 and 7 of OutputC.csv):
pcp Qout14
2.3 7.20E-03
3 1.34E-02
3.3 1.50E-02
2.3 8.25E-03
3 1.32E-02
2.5 9.47E-03
3 1.28E-02
3.6 1.81E-02
2.5 1.02E-02
2.5 9.44E-03
2 6.00E-03
2.8 1.17E-02
2.8 1.12E-02
2.8 1.16E-02
Traceback (most recent call last): File
"C:\Users\jmiyama\Desktop\Python Files\OutputCtoScatterplot.py", line
48, in
main() File "C:\Users\jmiyama\Desktop\Python Files\OutputCtoScatterplot.py", line 35, in main
y = [row.split(',')[1] for row in data[1:]] IndexError: list index out of range
import csv
import numpy as np
import matplotlib.pyplot as plt
import pandas
import collections
def getColumn(filename, column):
results = csv.reader(open(filename), dialect='excel')
return [result[column] for result in results]
def dictcolumn():
pcp = getColumn("C:\Users\jmiyama\Desktop\Python Files\OutputC.csv",1)
Qout14 = getColumn("C:\Users\jmiyama\Desktop\Python Files\OutputC.csv",7)
scatter={}
scatter["pcp"]= pcp
scatter["Qout14"]= Qout14
return scatter
def main():
scatter = dictcolumn()
file = 'Qout14.csv'
with open(file, 'w+') as csv_file:
writer = csv.writer(csv_file, lineterminator='\n')
writer.writerows(zip(*scatter.values()))
with open("Qout14.csv") as f:
data = f.read()
data = data.split('\n')
# trying to split the zipped values into columns
x = [row.split(',')[0] for row in data[1:]]
#THE CODE WORKS UP TO HERE
y = [row.split(',')[1] for row in data[1:]]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid()
plt.scatter(x,y)
plt.xlabel('pcp')
plt.ylabel('Qout14')
plt.title('Qout14 vs pcp')
plt.show()
if __name__ == '__main__':
main()
data = data.split('\n')
似乎在末尾添加了额外的 "blank" 行,因此下面会起作用。
x = [row.split(',')[0] for row in data[1:-1]]
y = [row.split(',')[1] for row in data[1:-1]]
我试图创建的最终产品是一个程序,它从 csv(已附加,称为 OutputC.csv)获取输入,并允许用户从他们 select 的列中绘制散点图.我决定先创建预 select 列的图形,然后再使用 rawinput 和变量。所以,我遇到的问题是 python 将数据读入 pyplot 的方式。数据取自输入 csv 并写入它自己的文件 (Qout14.csv),我试图从中绘制。不幸的是,即使 x 和 y 变量列表的长度相同,我也会收到 IndexError。在做了一些阅读后,我想知道问题是否可能是 y 数据没有逗号分隔实例中的数据,所以它可能只是将所有 y 作为字符串读取,所以它没有任何其他内容名单。令人困惑的部分是同一行代码适用于变量的另一端,即 x。谁能指出我的错误在哪里?
Example of Inputs(Columns 1 and 7 of OutputC.csv):
pcp Qout14
2.3 7.20E-03
3 1.34E-02
3.3 1.50E-02
2.3 8.25E-03
3 1.32E-02
2.5 9.47E-03
3 1.28E-02
3.6 1.81E-02
2.5 1.02E-02
2.5 9.44E-03
2 6.00E-03
2.8 1.17E-02
2.8 1.12E-02
2.8 1.16E-02
Traceback (most recent call last): File "C:\Users\jmiyama\Desktop\Python Files\OutputCtoScatterplot.py", line 48, in main() File "C:\Users\jmiyama\Desktop\Python Files\OutputCtoScatterplot.py", line 35, in main y = [row.split(',')[1] for row in data[1:]] IndexError: list index out of range
import csv
import numpy as np
import matplotlib.pyplot as plt
import pandas
import collections
def getColumn(filename, column):
results = csv.reader(open(filename), dialect='excel')
return [result[column] for result in results]
def dictcolumn():
pcp = getColumn("C:\Users\jmiyama\Desktop\Python Files\OutputC.csv",1)
Qout14 = getColumn("C:\Users\jmiyama\Desktop\Python Files\OutputC.csv",7)
scatter={}
scatter["pcp"]= pcp
scatter["Qout14"]= Qout14
return scatter
def main():
scatter = dictcolumn()
file = 'Qout14.csv'
with open(file, 'w+') as csv_file:
writer = csv.writer(csv_file, lineterminator='\n')
writer.writerows(zip(*scatter.values()))
with open("Qout14.csv") as f:
data = f.read()
data = data.split('\n')
# trying to split the zipped values into columns
x = [row.split(',')[0] for row in data[1:]]
#THE CODE WORKS UP TO HERE
y = [row.split(',')[1] for row in data[1:]]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid()
plt.scatter(x,y)
plt.xlabel('pcp')
plt.ylabel('Qout14')
plt.title('Qout14 vs pcp')
plt.show()
if __name__ == '__main__':
main()
data = data.split('\n')
似乎在末尾添加了额外的 "blank" 行,因此下面会起作用。
x = [row.split(',')[0] for row in data[1:-1]]
y = [row.split(',')[1] for row in data[1:-1]]