2

To use the Gnuplot (On linux Ubuntu 16.04) command directly by specifying arguments without using the its prompt, I enter this for example :

gnuplot -e "filename='DataFile.txt'" SettingsFile

Where SettingsFile looks like this :

plot filename with lines
set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","
pause -1

And the DataFile.txt looks like this :

Packets, Time(in seconds) :

13392,120
24607,240
23867,360
21764,480
20727,600
20004,720
19719,840
19758,960
19728,1080
20168,1200
19737,1320
19729,1440
20135,1560
20006,1680
21301,1800
19923,1920
20002,2040
19761,2160
20918,2280
22756,2400
22820,2520
23370,2640
22987,2760
22956,2880
24427,3000
23527,3120
24009,3240
23832,3360
23464,3480
23652,3600
11212,3654

First question :

Is there a way to set into that SettingsFile a png OutputFile ? So I can enter it as an argument to the Gnuplot command just as I did with the DataFile. (I want to use it this way, because I want to invoke it from an external code)

I want to achieve something like this :

gnuplot -e "filename='DataFile.txt'" SettingsFile OutputFile.png 

Second question :

The screen output that I get from Gnuplot shows the xtics differently than expected :

enter image description here

Notice also that the axis titles are not shown !

Now if I try to resize the window I get this :

enter image description here

The graph gets bizarrely flipped, with the titles set and the tics being updated as desired.

How should I fix these two problems, first mentioning an output file in the SettingsFile, and second the xtics not being showed properly and third this strange behavior in the screen output ?

AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84
  • And which os ? Could (?) make a difference. – kebs Jan 22 '17 at 21:50
  • I would ask the second question in a different post. Also, which version of gnuplot and which terminal (wxt, qt, x11) are you using? Notice that `set datafile separator ","` should go BEFORE the `plot` command (the same for the `xlabel`, `ylabel`, and `title` commands). For the first question, try [passing command-line arguments to gnuplot](http://stackoverflow.com/a/31815067/2174266) – vagoberto Jan 22 '17 at 21:51
  • @kebs : `linux` tag used, I updated my question with full OS info. – AymenDaoudi Jan 22 '17 at 21:56
  • @vagoberto You may be right concerning splitting the post into to questions, but... however. I use qt gnuplot, thx for the link, I'll recheck it. – AymenDaoudi Jan 22 '17 at 21:57

1 Answers1

5

Several commands can be added to gnuplot -e through semicolons, for example:

gnuplot -p -e 'filename="data.txt"; fileout="image.png"' SettingsFile

Your SettingsFile should already have a line configuring the terminal type:

set terminal png
set output fileout

set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","

plot filename using 2:1 with lines

If you want more control over your code, try with this script (gnuplot 5.0+):

filename=(ARGC>0 ? ARG1 : 'DataFile.txt' )  # By default filename=DataFile.txt
                                            # If called with one argument (ARGC=1)
                                            # then `filename=ARG1`

if(ARGC>1){
  # if called with two arguments (ARGC=2), then configure a png output
  set terminal png
  set output ARG2
}

set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","

# the plot command ALWAYS at the end of the script after the settings
plot filename using 2:1 with lines
  • If you want to plot 'DataFile.txt' (by default) interactively:

    gnuplot -p SettingsFile
    
  • If you want to plot another file, e.g. AnotherData.txt:

    gnuplot -p -c SettingsFile AnotherData.txt
    
  • If you want to plot another file and save it as PNG:

    gnuplot -p -c SettingsFile AnotherData.txt Output.png
    

The -p argument lets plot windows survive after main gnuplot program exits. Thw -c argument load script using gnuplot's "call" mechanism and pass it the remainder of the command line as arguments. See How to pass command line argument to gnuplot?

Notice that your script plots the datafile first, and THEN configure the labels, title and datafile separator. That is why you see weird tics.

Community
  • 1
  • 1
vagoberto
  • 2,372
  • 20
  • 30
  • As I mentioned in the question, I want to execute the command from an external program/code (java for example). Any way to enter the output.png argument without that script ? – AymenDaoudi Jan 22 '17 at 23:01
  • I execute similar commands from within C++ and Fortran. I don't get why it won't work in your case. Anyway, if you insist in your method, tray with something like `gnuplot -e "filename='DataFile.txt'; output='image.png'" SettingsFile` – vagoberto Jan 22 '17 at 23:07
  • I know it would work, however my solution will be deployed so I want to avoid forcing "them" to run anything else. However your solution works fine, consider adding it in the answer please. – AymenDaoudi Jan 22 '17 at 23:28