The end product I am trying to create is a program that takes input from a csv (attached, called OutputC.csv) and allows the user to graph a scatter plot from columns that they select. I've decided to first create up to the graphing of preselected columns, and then play with rawinput and variables later. So, the issue I'm having getting there is with the way the data is being read by python into pyplot. The data is taken from the input csv and written into it's own file(Qout14.csv), which I tried to plot from. Unfortunately, I am getting an IndexError even though the x and y variable lists are the same length. Upon doing some reading, I was wondering if the issue might be that the y data does not have a comma separating the data in an instance so it may then just be reading all of y as a string, so it does not have anything else in the list. The confusing portion is that the same line of code works for the other side of the variables, for x. Can anyone point out where my mistake may be?
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')
appears to be adding extra "blank" row at the end, so below would work.
x = [row.split(',')[0] for row in data[1:-1]]
y = [row.split(',')[1] for row in data[1:-1]]