Skip to content

Instantly share code, notes, and snippets.

@becxer
Last active August 29, 2015 14:22
Show Gist options
  • Save becxer/9263c310801a3e2a05fd to your computer and use it in GitHub Desktop.
Save becxer/9263c310801a3e2a05fd to your computer and use it in GitHub Desktop.
gnuplot wrapper
#!/usr/bin/python
#
# used to make dem for gnuplot script more easier
#
# @author becxer
# @email [email protected]
#
import sys
import os
import subprocess
#default options
chart_name = "test.chart"
outimg = "./out-img.svg"
imgtype = "svg"
title = "report"
lineid = 0
px = 1
py = 2
lx = 'X'
ly = 'Y'
xtic = None
#[0,1,9]
ytic = None
#[0,1,9]
if len(sys.argv) < 3 or len(sys.argv) % 2 != 0:
print "usage : plotoss.py chart.report (-o out-img.svg) (-i id) (-t title) (-p x:y) (-xtic 0:1:9) (-ytic 0:5:90)"
exit(1)
#set options
chart_name = sys.argv[1]
def setOutput(filename):
global outimg
global imgtype
if ".svg" in filename or ".png" in filename:
outimg = filename
sout = outimg.split('.')
imgtype = sout[len(sout)-1]
def setTitle(title_):
global title
title = title_
def setPoint(param):
global px
global py
pms = param.split(":")
if len(pms) == 2:
px = pms[0]
py = pms[1]
def setxTic(param):
global xtic
pms = param.split(":")
if len(pms) == 3:
xtic = pms
def setyTic(param):
global ytic
pms = param.split(":")
if len(pms) == 3:
ytic = pms
def setId(id_):
global lineid
lineid = id_
optionMap = {
"-o" : setOutput,
"-t" : setTitle,
"-p" : setPoint,
"-i" : setId,
"-xtic" : setxTic,
"-ytic" : setyTic
}
if len(sys.argv) >= 3 :
for i in range(2,len(sys.argv)):
if i % 2 == 0 :
opt = sys.argv[i]
param = sys.argv[i+1]
optionMap.get(opt)(param)
ch = open(chart_name,'r')
label = []
chart = []
for line in ch:
if "@title" in line:
title = line[8:].strip()
continue
if "@label" in line:
label = map(str.strip,line[6:].split("|"))
continue
if "#" not in line:
gem_temp = '''
set title "%s"
set terminal %s
set output "%s"
set ylabel "%s"
set xlabel "%s"
set grid
set style line 1 lw 3
set style line 2 lw 3
set style line 3 lw 3
set style line 4 lw 3
set style line 5 lw 3
set style line 6 lw 3
set style line 7 lw 3
set style line 8 lw 3
'''%(title,imgtype,outimg,label[py],label[px])
if xtic != None :
gem_temp += "set xrange [%s:%s]\nset xtics %s, %s, %s\n"%(xtic[0],xtic[2],xtic[0],xtic[1],xtic[2])
if ytic != None :
gem_temp += "set yrange [%s:%s]\nset ytics %s, %s, %s\n"%(ytic[0],ytic[2],ytic[0],ytic[1],ytic[2])
if not os.path.exists('./tmp'):
os.makedirs('./tmp')
gem_temp += 'plot'
count = 0
for row in chart :
count+=1
tmp_out = open('./tmp/'+row[lineid]+".dat",'w')
tmp_out.write(row[px]+' '+ row[py])
tmp_out.close()
plot_temp = ' "%s" using 1:2 t "%s" with linespoints linestyle %d' % ('./tmp/'+row[lineid]+'.dat', row[lineid], count)
gem_temp += plot_temp
if len(chart) > count:
gem_temp += ','
print gem_temp
outdem = open(outimg+".dem",'w')
outdem.write(gem_temp)
outdem.close()
cmd = "gnuplot "+outimg+".dem"
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print output
print "------------------------------------------"
print " chart : " + chart_name
print " out-img : " + outimg
print " title : " + title
print " axis : (" + str(px) + "," + str(py) + ")"
print " line_id : " + str(lineid)
print " xtic : " + str(xtic)
print " ytic : " + str(ytic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment