Set with lines in gnuplot - gnuplot

I can connect the dots of a data file by including with lines in the plot command. Is there a way to set this fixed (I was hoping for set with lines) so I don't have to explicitly include it in every call of plot?

Use
set style data line
to achieve this. Similar options are also available for functions
set style function boxes

Related

How to plot to the same file overwriting it?

I am fiddling with gnuplot. I am not able to output results to the terminal, but that's a different question.
I can plot into a png file. But each time I plot something I need to call set output "plot.png".
Is there a setting that allows to overwrite the existing file?

.Plot data from many files in different output files using do loop in gnuplot

I am using gnuplot 4.6.
I have many data files named data_1.dat, data_2.dat,...,data_100.dat. Now I want to plot data from file 'data_i.dat' to file 'figure_i.eps'. Of course I can do this sequentially for every file data_i.dat like this:
set term postscript eps enhanced color
set out 'figure_i.eps'
pl 'data_i.dat'
set out
set term wxt
If the number of files is too large, this is not a good method. I know that it is possible to use 'do' command in gnuplot 4.6. Hence something like following could perhaps be done:
set term postscript eps enhanced color
do for [i=1:100] {
set out 'figure_i.eps'
pl 'data_i.dat'
set out
}
set term wxt
However, I don't know how do really specify input and output files inside the loop such that correct numbers will be picked up automatically. Any advice is highly appreciated. Thanks in advance.
You can use sprintf inside your loop:
...
outFile=sprintf("figure_%d.eps", i)
dataFile=sprintf("data_%d.dat", i)
set output outFile
plot dataFile ...
...

gnuplot - multiple plot files using the same style settings, but different number of plot lines?

This is my problem: I have 4 different data files; and I need to create various plots on png, using data in these files.
I would like to have all in a function that I call in a script, so I would like to put together as many common statement as I can.
The plot have different file names, but they use mostly the same settings: the legend position, the title, the axis label, the range, border line style.
What change is the data, coming from different data files, the number of lines on the same plot (some has for example 1 set of data, others has 4-5 per plot), and the color to differentiate them.
Is there a clean way to group what is similar, so I don't end up writing the same things for each of my plot? I've check the doc and I was not able to find a solution to this; since the style is set for each dataset, so I can't really do anything to group it.
Found some questions here that look similar, but the problem is totally different...I don't need to merge data from different dataset, but I need to create different plot files, which only share most of the common settings. To make a generic example, I need a way to do something like a CSS style file, so the style stay the same, but the content of the plot (and the name of the file) changes.
I am using shell script for the code; so I wrapped a gnuplot command in a shell function.
Thanks
You can put all common settings in one file (lets say settings.gp) and load them from your main files with load 'settings.gp'. That works as if you would write the actual commands in place of the load command. Therefore you can define some variables before loading the settings file to change the behavior.
File settings.gp:
set terminal pngcairo
set output outfile
set style increment user
if (plotNum == 2) {
set style line 1 lt 5
set style line 2 lt 6
} else {
set for [i=1:5] style line i lt i+2
}
(Note, that this kind of if statement requires gnuplot version 4.6 and newer).
File main.gp
outfile = 'first.png'
plotNum = 2
load 'settings.gp'
plot x, x**2
The command set style increment user automatically iterates over line styles instead of line types in the plot command.
That is of course only an example, basically you can include any kind of tests and conditions in you settings.gp. An other possiblity is using the call command.

titles next to lines in gnuplot

The feature list for the new version of gnuplot states that "plot title can be placed next to the plot line in the graph proper." This is something I would like to be able to do automatically, but I can't find mention of how to do this in the documentation (probably because it hasn't been written yet). Does anyone know how to do this?
I found the answer in the command line documentation, I assume the pdf hasn't been updated yet. Here's the text:
If you want the title of a plotted line to be placed immediately before or
after that line in the graph itself, use `at {beginning|end}`. This option
may be useful when plotting `with lines` but makes little sense for some
other plot styles.

how to draw objects (rect) from a file

In gnuplot i can draw a rectangle via
set object rect from x0,y0 to x1,y1
How to read the coordinates x0,x1,y0,y1 from a file?
One way would be to put the line of code that sets the rectangle into a separate file and call that file from within the gnuplot script. So you have a file called "coord.txt" that contains the one line
set object rect from 2,2 to 4,40
and you have a gnuplot script called "rect.gp" that says
set title "call rectangle coordinates"
load "coord.txt"
plot x**2
If you now from within gnuplot type load "rect.gp" you get your graph with the rectangle.
That may not be exactly what you are looking for but maybe a first step.
You probably need to store the data from the file into variables, then use these variables to define the locations of objects. The way to do this is not straight as far as I know. Please refer to here. Good luck!

Resources