2

Hello All you Lovely People on stackoverflow,

I am trying to plot data using gnuplot. I start by reading through a table and pulling out the data I want. I write this data to a .dat file. As of now, I'm just trying to plot it through a command line but will add the necessary code to plot it from the python script after it's working.

My code which creates the .dat file-

#!/usr/bin/python

file = open("test_m.rdb")
table = open('table.dat', 'w+')

trash = file.readline()

trash = file.readline()

data = file.readline()
i = data.split()
flux = i[2]
observed = i[4]
table.write(flux + " " + observed,)

while 1:
    line = file.readline()
    i = line.split()
    try:
        flux = i[2]
        observed = i[4]
    except IndexError:
        break
    table.write("\n" + flux + " " + observed)
    table.close()

The command I'm attempting to use in cygwin and the error-

gnuplot plot table.dat

0.058 2
^
"table.dat", line 1: invalid command

Thank you in advance. I appreciate any suggestions you can offer.

marie
  • 417
  • 3
  • 5
  • 15

1 Answers1

4

you probably want:

gnuplot --persist -e 'plot "table.dat" u 1:2'

With your command, gnuplot is looking for commands to run in a file called 'plot' and then in a file called 'table.dat'. 'table.dat' doesn't have commands to run, it has data to be plotted. using '-e' is the same thing as putting the stuff in singe quotes into a temporary file (call it temp.gp) and then doing gnuplot temp.gp. The --persist makes it so the plot stays on your screen (which you'll want since I doubt you're saving it to a file). To learn about how to save it to a file, inside gnuplot do: help set term and help set output and set term.

EDIT

I don't know much about cygwin, so I don't know what the default terminal is (or what terminals will be enabled).

A few things to try:

gnuplot -e 'plot "table.dat" u 1:2; pause -1'  #this should leave your plot open until you hit return

Put the commands in a file

#tmp.gp
set term postscript enh color
set output "tmp.ps"
plot "table.dat" u 1:2

Now run it:

gnuplot tmp.gp

and then open the postscript using whatever tool you have for viewing postscripts -- I often use gv, but I don't know what there is on cygwin.

gv tmp.ps &
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Thanks, cygwin gives me this error when I try that command- 0 [main] gnuplot 7176 exception::handle: Exception: STATUS_ACCESS_VIOLATION 4903 [main] gnuplot 7176 open_stackdumpfile: Dumping stack trace to gnuplot.exe.stackdump gnuplot: unable to open display '' gnuplot: X11 aborted. – marie Jun 07 '12 at 18:35
  • Thank you again for helping. Both options are giving me the same error "unable to open display". Is it possible that something doesn't have the permissions it needs to open up a display? – marie Jun 07 '12 at 18:58
  • With the second option, did you get a file named 'tmp.ps' in the current directory? (`ls` will tell you if it's there or not). If you have `gv`, it is reasonable that it would be complaining the same as gnuplot if the file exists if your cygwin environment isn't configured properly to use X11 (You'll need to google around for how to fix that one though -- I have no idea). – mgilson Jun 07 '12 at 19:01
  • Oh, wow, yep, that file is there. And it's the graph I wanted! Thank you so much! – marie Jun 07 '12 at 19:12