@ -9,7 +9,9 @@ import numpy as np
|
||||
|
||||
|
||||
class GracePlot(object):
|
||||
def __init__(self):
|
||||
def __init__(self, fname):
|
||||
self.fname = fname
|
||||
|
||||
self.ls_map = {"None":0, "-":1, ":":2, "--":3, "-.":4 }
|
||||
|
||||
# Symbols: 0:None 1:Circle 2:Square 3:Diamond 4:Triangle 5:up 6:left 7:down 8:right, 9:PLus 10:X 11:Star
|
||||
@ -33,18 +35,16 @@ class GracePlot(object):
|
||||
tmp_fd, tmp_name = tempfile.mkstemp()
|
||||
self.tmpfiles.append(tmp_name)
|
||||
np.savetxt(tmp_name, np.array([x, y]).T)
|
||||
#tmp_fd.close()
|
||||
# read data in xmgrace
|
||||
self.cmds.append('READ NXY "%s"\n'%tmp_name)
|
||||
self.cmds.append('S%i SYMBOL SIZE 0.7\n'%(self.data_counter))
|
||||
|
||||
#self.cmds.append('S%i SYMBOL FILL COLOR %i\n'%(self.data_counter, self.data_counter))
|
||||
# read data from temporary file
|
||||
self.cmds.append('READ NXY "%s"\n'%tmp_name)
|
||||
self.cmds.append('S%i SYMBOL SIZE 0.5\n'%(self.data_counter))
|
||||
self.cmds.append('S%i SYMBOL FILL PATTERN 1\n'%(self.data_counter))
|
||||
self.cmds.append('S%i SYMBOL 1\n'%(self.data_counter)) # No line
|
||||
|
||||
|
||||
if "label" in kwds.keys():
|
||||
label = kwds["label"]
|
||||
# TODO: implement at least greek symbols and lower upper case (_ and ^)?
|
||||
label = unicode(kwds["label"]).encode('ascii', 'ignore')
|
||||
self.cmds.append('S%i LEGEND "%s"\n'%(self.data_counter, label))
|
||||
|
||||
if "ls" in kwds.keys():
|
||||
@ -84,23 +84,23 @@ class GracePlot(object):
|
||||
|
||||
def loglog(self, x,y, **kwds):
|
||||
self.cmds.append('YAXES SCALE LOGARITHMIC\n')
|
||||
self.cmds.append("YAXIS TICKLABEL FORMAT POWER\n")
|
||||
self.cmds.append("YAXIS TICKLABEL PREC 0\n")
|
||||
self.cmds.append("YAXIS TICKLABEL FORMAT POWER\n")
|
||||
self.cmds.append("YAXIS TICKLABEL PREC 0\n")
|
||||
self.cmds.append('XAXES SCALE LOGARITHMIC\n')
|
||||
self.cmds.append("XAXIS TICKLABEL FORMAT POWER\n")
|
||||
self.cmds.append("XAXIS TICKLABEL PREC 0\n")
|
||||
self.cmds.append("XAXIS TICKLABEL FORMAT POWER\n")
|
||||
self.cmds.append("XAXIS TICKLABEL PREC 0\n")
|
||||
self.plot(x, y, **kwds)
|
||||
|
||||
def semilogx(self, x, y, **kwds):
|
||||
self.cmds.append('XAXES SCALE LOGARITHMIC\n')
|
||||
self.cmds.append("xAXIS TICKLABEL FORMAT POWER\n")
|
||||
self.cmds.append("xAXIS TICKLABEL PREC 0\n")
|
||||
self.cmds.append("xAXIS TICKLABEL FORMAT POWER\n")
|
||||
self.cmds.append("xAXIS TICKLABEL PREC 0\n")
|
||||
self.plot(x, y, **kwds)
|
||||
|
||||
def semilogy(self, x, y, **kwds):
|
||||
self.cmds.append('YAXES SCALE LOGARITHMIC\n')
|
||||
self.cmds.append("YAXIS TICKLABEL FORMAT POWER\n")
|
||||
self.cmds.append("YAXIS TICKLABEL PREC 0\n")
|
||||
self.cmds.append("YAXIS TICKLABEL FORMAT POWER\n")
|
||||
self.cmds.append("YAXIS TICKLABEL PREC 0\n")
|
||||
self.plot(x, y, **kwds)
|
||||
|
||||
|
||||
@ -121,26 +121,26 @@ class GracePlot(object):
|
||||
self.cmds.append('LEGEND OFF\n')
|
||||
|
||||
|
||||
def save(self, fname):
|
||||
def save(self):
|
||||
self.cmds.append('AUTOSCALE\n')
|
||||
self.cmds.append('SAVEALL "%s"\n'%fname)
|
||||
self.cmds.append('SAVEALL "%s"\n'%self.fname)
|
||||
# write cmds to tmpfile
|
||||
tmp_fd, tmp_name = tempfile.mkstemp()
|
||||
self.tmpfiles.append(tmp_name)
|
||||
tmp_file = open(tmp_name, 'w')
|
||||
tmp_file.writelines(self.cmds)
|
||||
tmp_file.close()
|
||||
# excecute tempraray xmgrace file to create final agr
|
||||
os.system("xmgrace -batch %s -hardcopy -nosafe -printfile tmp.tmp"%tmp_name)
|
||||
# excecute temporary xmgrace file to create final agr
|
||||
os.system("xmgrace -batch %s -hardcopy -nosafe -printfile tmp.tmp" % tmp_name)
|
||||
os.remove("tmp.tmp")
|
||||
# prepend color map to the new file
|
||||
with file(fname, 'r') as original_agr: data = original_agr.readlines()
|
||||
with file(self.fname, 'r') as original_agr: data = original_agr.readlines()
|
||||
# get the last "@map color ..." line
|
||||
last_color_lineno = 0
|
||||
for i,line in enumerate(data):
|
||||
if line.lower().startswith("@map color"):
|
||||
last_color_lineno = i+1
|
||||
with file(fname, 'w') as new_agr:
|
||||
with file(self.fname, 'w') as new_agr:
|
||||
new_agr.writelines(data[:last_color_lineno])
|
||||
for color in self.color_map:
|
||||
new_agr.write(self.color_map[color][1])
|
||||
@ -164,5 +164,5 @@ if __name__ == "__main__":
|
||||
gr.save("test.agr")
|
||||
print "created test.agr"
|
||||
os.system("xmgrace test.agr")
|
||||
print "deleting test.agr"
|
||||
#print "deleting test.agr"
|
||||
#os.remove("test.agr")
|
Reference in New Issue
Block a user