Separate file plots from different data files gnuplot - gnuplot

I have 3000 .dat files which I want to plot using gnuplot.
They are all named as "iteration_1", ...", iteration_93", ..."iteration_1247",... (not in the format "iteration_XXXX", if this information is helpful).
Each of those files is to be plotted in an .eps file - my final intention is to do a video (an evolution of those plots), which I can easily make if I have the .eps files.
Is there any way to quickly command gnuplot to do this? All the questions I have found remotely similar to my situation were all actually regarding putting data from different files into one plot in a single file.
Again, I do not want to put all the plots into a single .eps file. I want 3000 .eps files.
Thanks in advance!

Simply put your plotting routine in a do for loop. By the way, gnuplot can also do animated GIFs. Check help gif.
### create output files in a loop
reset session
set terminal epscairo
do for [i=1:3000] {
FILE = sprintf("iteration_%d",i)
set output FILE.".eps"
plot FILE.".dat" u 1:2 w l # or change your extension and plot command accordingly
}
set output
### end of code

Related

how to save two plots simultaneously in gnuplot

Hiii...
I havebeen trying to plot two curves simultaneously in a single plot to compare them. ie, by the command:
plot "1.txt" w l, "2.txt" w l
now I want to save it, but the usual command for saving is:
set out "1.txt"
but in this case how can I save them together in a same plot?
For saving a plot as a file on your disk, you need to setup two things: a terminal and an output. Suppose you have configured your plot interactively in a command-line gnuplot session, this is an example:
set terminal png
set output "myplot.png"
replot
unset output
where replot does what its command name suggests, it repeats the last plot command - and if that last plot command showed both curves as you wanted it, they will identically show up in the output file. For info on supported terminal types on your system, read help terminal.

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?

Creating an animation from multiple data files

I have some C++ code that generates data I want to create an animated gif (or equivalent) from. The data is output into .txt files with the names 1, 2, 3, 4 ..., N with 2 columns (x y data points). For simplicity say we use 100 files.
There seems to be 1 of 2 ways to do this, either create 100 png images from the 100 files then use GIMP to create a gif or create a gif automatically via GNUplot. The first I should be able to do with a loop, say;
set term png
for [i=1:100] {set output "data".i."png"; plot 'filepath/'.i.'.txt' with lines title ""; set output}
Which gives me the error: 'invalid complex constant'. This I suspect is just be being bad with GNUplot syntax.
As for the second, the examples I can find make it unclear how to use data to generate the plots.
Any help is much appreciated.
For gnuplot versions older than 4.6 you can use reread to do such kind of looping.
Consider the file looper.gp:
set output 'data'.i.'.png'
plot 'filepath/'.i.'.txt' with lines notitle
i = i + 1
if (i <= 100) reread
Call this with
i = 1
set terminal png
load 'looper.gp'
set output

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.

Can I convert a contour plot in matplotlib to a format that can be imported and edited in AutoCAD?

I have written a script in Python 3.3 which reads position (x, y coordinate) and risk values from a text file and saves the resulting contour plot using matplotlib. My company needs to be able to edit the contour in AutoCAD. Unfortunately, my knowledge about AutoCAD is severely limited, and the people at my company who know AutoCAD know very little about generating contour plots.
How can I create a contour plot that can be imported in AutoCAD? My current thinking is that I should save the plot as an svg file and convert it to something AutoCAD can open, or install a plugin for AutoCAD that will allow it to open one of the formats that matplotlib can save. I have seen this question but that does not quite suit my needs.
*Edit*
I have tried saving the plot as an SVG file, opening it in Inkscape, and saving it as DXF, but it does not save the contour color information, and the task needs to be automated anyway. The contour color information is important to preserve because the color indicates the order of magnitude of risk.
If you can generate a postscript file (matplotlib can create pdf's right?) you might be able to use pstoedit from the command line to convert it to a dxf.
Alternatively, you can use Illustrator(not free) or Inkscape(free) to convert the svg to a dxf. There are some general rumblings on the internet that Inkscape will sometime turn bezier curves into straight lines, but I haven't check to see if this is still true or not.
I ended up having my plotting program create a very basic Autocad script. I referred to this question about extracting x,y data from a contour plot to write the Autocad script. Here is the relevant function:
def make_autocad_script(outfile_name, contour):
'''
Creates an Autocad script which contains polylines for each contour.
Args
outfile_name: the name of the Autocad script file.
contour: the contour plot that needs to be exported to Autocad.
'''
with open(outfile_name, 'w', newline='') as outfile:
writer = csv.writer(outfile, delimiter=',', )
# each collection is associated with a contour level
for collection in contour.collections:
# If the contour level is never reached, then the collection will be an empty list.
if collection:
# Set color for contour level
outfile.write('COLOR {}\n'.format(random.randint(1,100)))
# Each continuous contour line in a collection is a path.
for path in collection.get_paths():
vertices = path.vertices
# pline is an autocad command for polyline. It interprets
# the next (x,y) pairs as coordinates of a line until
# it sees a blank line.
outfile.write('pline\n')
writer.writerows(vertices)
outfile.write('\n')
I send make_autocad_script the outfile and contour plot that I need, and in Autocad I import the script. This plots each contour as a random color, but that could be replaced with whatever color you wanted.

Resources