Reputation: 101
I am trying to plot two rows' data, and I am having trouble doing just that. For some reason, after replacing everything that I thought was causing the problem, I am still running into the error. I have tried different methods such as .replace()
and .split()
, but they have not helped. My code is the following:
#import csv
#import numpy as np
#import string
#import pandas as pd
from matplotlib import pyplot as plt
def split_list(a_list):
half = len(a_list)/2
return a_list[:half], a_list[half:]
c = []
B1 = []
C1 = []
a = [0, 1]
with open('Lamda_HeHCL_T.txt') as fd:
for n, line in enumerate(fd):
if n in a:
c.append(line.strip())
B, C = split_list(c)
B = str(B)
C = str(C)
B = B.replace("'", "").replace("\n","")
B = B.replace(" ", ",")
B = B.replace(" ", "")
B = B.replace(",", " ")
C = C.replace("'","").replace("\n","")
C = C.replace(" ", ",")
C = C.replace("1,,,,","")
C = C.replace("2,,,","")
B = B.strip('[]')
print B
B = map(float, B)
print B
print C
#fix, ax = plt.subplots()
#ax.scatter(B, C)
Here is the data I am using:
1 2 5 10 20 30 40 50 60 70 80 90 100 150 200 300 400 500 600 700 800 900 1000 1500 2000 2500 3000
1 2 1 4.151E-12 4.553E-12 4.600E-12 4.852E-12 6.173E-12 7.756E-12 9.383E-12 1.096E-11 1.243E-11 1.379E-11 1.504E-11 1.619E-11 1.724E-11 2.139E-11 2.426E-11 2.791E-11 3.009E-11 3.152E-11 3.252E-11 3.326E-11 3.382E-11 3.426E-11 3.462E-11 3.572E-11 3.640E-11 3.698E-11 3.752E-11
I commented the plotting code out until I am able to successfully convert them to floats, so that they can be plotted.
Full traceback:
Traceback (most recent call last):
File "<ipython-input-371-b1de68ec731e>", line 1, in <module>
runfile('C:/Users/.spyder/Finding_C_New_New.py', wdir='C:/Users/.spyder')
File "C:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/.spyder/Finding_C_New_New.py", line 49, in <module>
B = map(float, B)
ValueError: could not convert string to float:
Upvotes: 1
Views: 502
Reputation: 3852
Your problem was that both B
and C
were string representations of lists. The solution is to convert B
and C
to lists via a simple list comprehension, and using the ast
module:
Original Code
#import csv
#import numpy as np
#import string
#import pandas as pd
from matplotlib import pyplot as plt
def split_list(a_list):
half = len(a_list)/2
return a_list[:half], a_list[half:]
c = []
B1 = []
C1 = []
a = [0, 1]
with open('Lamda_HeHCL_T.txt') as fd:
for n, line in enumerate(fd):
if n in a:
c.append(line.strip())
B, C = split_list(c)
B = str(B)
C = str(C)
B = B.replace("'", "").replace("\n","")
B = B.replace(" ", ",")
B = B.replace(" ", "")
B = B.replace(",", " ")
C = C.replace("'","").replace("\n","")
C = C.replace(" ", ",")
C = C.replace("1,,,,","")
C = C.replace("2,,,","")
B = B.strip('[]')
print B
At this point, we see that B
is a string:
B = "1 2 5 10 20 30 40 50 60 70 80 90 100 150 200 300 400 500 600 700 800 900 1000 1500 2000 2500 3000"
and C is a string representation of a list:
C = '[4.151E-12,4.553E-12,4.600E-12,4.852E-12,6.173E-12,7.756E-12,9.383E-12,1.096E-11,1.243E-11,1.379E-11,1.504E-11,1.619E-11,1.724E-11,2.139E-11,2.426E-11,2.791E-11,3.009E-11,3.152E-11,3.252E-11,3.326E-11,3.382E-11,3.426E-11,3.462E-11,3.572E-11,3.640E-11,3.698E-11,3.752E-11]'
You need to change these to lists by doing the following:
# For B
B = [float(i) for i in B.split()]
>>> B =
[1.0, 2.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 150.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 1500.0, 2000.0, 2500.0,3000.0]
# For C
import ast
C = ast.literal_eval(C)
> C =
[4.151e-12, 4.553e-12, 4.6e-12, 4.852e-12, 6.173e-12, 7.756e-12, 9.383e-12, 1.096e-11, 1.243e-11, 1.379e-11, 1.504e-11, 1.619e-11, 1.724e-11, 2.139e-11, 2.426e-11, 2.791e-11, 3.009e-11, 3.152e-11, 3.252e-11, 3.326e-11, 3.382e-11, 3.426e-11, 3.462e-11, 3.572e-11, 3.64e-11, 3.698e-11, 3.752e-11]
Then continue as before:
B = map(float, B)
fix, ax = plt.subplots()
ax.scatter(B, C)
Giving you:
Upvotes: 2