user3262537
user3262537

Reputation: 137

Invoking Gnuplot from Python

I am trying divide the data based on line numbers using a python script and invoking Gnuplot to plot a histogram. However, for some reason, I don't get any output after running my script. Can somebody help me understand what am I doing wrong? Thanks!

My script:

#!/usr/bin/env python
import os
import subprocess
with open("gpu Latency sample 1.txt", "r") as f:
        for lineno, line in enumerate(f, start=1):
                if ( lineno % 2 == 0) :
                        #odd_file = open("odd_file.txt", 'a')
                        with open("even.txt", "a") as even_file:
                            even_file.write(line)
                else :
                        with open("odd.txt", "a") as odd_file:
                            odd_file.write(line)
p = subprocess.Popen(['gnuplot'], shell = True,stdin=subprocess.PIPE )
filename1= open("even.txt","r")
filename2= open("odd.txt","r")
p.stdin.write("set xtics nomirror rotate by -45")
p.stdin.write("set key noenhanced")
p.stdin.write("set style data linespoints")
p.stdin.write("binwidth=1000")
p.stdin.write("bin(x, width)=width*floor(x/width) + binwidth/2.0")
p.stdin.write("plot \"%s\" using (bin((\$1+\$2),binwidth)): (1.0) smooth freq with lines, \"%s\" using (bin((\$1+\$2),binwidth)): (1.0) smooth freq with lines" %(filename1, filename2))
p.stdin.write("pause -1")

Upvotes: 2

Views: 718

Answers (1)

jsbueno
jsbueno

Reputation: 110756

You are writing to gnuplot's stdin, but you are not sending any newline (\n) characters to it: all your gnuplot commands are sgetting quashed in a single line without even a whitespace between then.

Just try adding a "\n" character at the end of each of your write calls above.

Upvotes: 1

Related Questions