I'm asking this question again because I have not gathered a clear answer for my question.
I have a set of equations:
ref_energy = (K-(C/2)comp(a)) + C/2
form_E2 = relaxed_E_per_atom - ref_energy(comp_x)
I need to have a python script use these two equations using the values in the txt file to generate another x and y columns of values that will plot those values.
This is the first few lines of my txt file. the config is the name of the structure associated with four values
comp(a),form_E,comp_x,relaxed_E_per_atom
0,0,0,-8.15382173
1,0,0.33333333,-5.25358563
0.5,0.18614484,0.2,-6.33922213
0.5,-0.69658919,0.2,-6.69231575
0.5,-0.70549249,0.2,-6.69587707
Below is my script but I keep getting errors such as
Traceback (most recent call last):
File "attempt_#2.py", line 80, in <module>
x.append(float(row[0]))
ValueError: could not convert string to float: 'comp(a)'
I assume the x value isn't a numerical value in the configname, but not sure.
#!/bin/env/python
import numpy as np
import matplotlib.pyplot as plt
import csv
Columns = 'configname,comp(a),form_E,comp_x,relaxed_E_per_atom'.split(',')
testdata ='''\
comp(a),form_E,comp_x,relaxed_E_per_atom
0,0,0,-8.15382173
1,0,0.33333333,-5.25358563
0.5,0.18614484,0.2,-6.33922213
0.5,-0.69658919,0.2,-6.69231575
0.5,-0.70549249,0.2,-6.69587707
'''
reader = csv.DictReader(StringIO(testdata))
desired_cols = (tuple(row[col] for col in columns) for row in reader)
x=[]
y=[]
K=-2.69028905
C=-32.65176322
with open('values2.txt','r') as csvfile:
points = csv.reader(csvfile,delimiter=',')
for row in points:
x.append(float(row[0]))
y.append(float(row[1]))
allpoints=np.loadtxt('hmm.csv',delimiter=',')
ref_energy = (Kend-(Cend/2))*comp(a) + Cend/2
form_E = relaxed_E_per_atom-ref_energy(comp_x)
plt.scatter(x,y, label='Energy')
plt.xlabel('Composition KxC',fontsize=24)
plt.ylabel('Formation Energy per carbon (eV)',fontsize=18)
plt.title('Convex Hull Potassium Graphite')
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.tight_layout()
plt.ylim(-0.08,0)
plt.xlim(0,1.01)
plt.show()
comp(a)into floats, which obviously won't work. The simplest answer is to addnext(points) # skip headerright about thefor row in points:.DictReaderfortestdata. You can do the same thing forcsvfile. That will not just skip the headers, but actually use them, to give you a dict with nicely named columns for each row, instead of just a list. (Which is explained better in the second answer on the dup question—or just in thecsvdocs.)