This question is related to Loop structure inside gnuplot? and the answer there by DarioP.
gnuplot 4.6 introduced the do command. How can I use this to loop over an array of for example files and colors? What is the correct syntax?
colors = "red green #0000FF"
files = "file1 file2 file3"
do for [i=1:3] {
plot files(i).".dat" lc colors(i)
}
If you want to have all files in a single plot, you need to use plot for[... (supported since version 4.4). Looping over several plot commands with do for (supported only since version 4.6) works only in multiplot mode.
The following two solutions both plot all data in one graph, but differ a bit in the iterations.
The first solution uses word to extract a word from a string directly when plotting.
colors = "red green #0000FF"
files = "file1 file2 file3"
plot for [i=1:words(files)] word(files, i).'.dat' lc rgb word(colors, i)
The second solution changes the linetype and then iterates directly over the word list instead of using an index.
colors = "red green #0000FF"
files = "file1 file2 file3"
set for [i=1:words(colors)] linetype i lc rgb word(colors, i)
plot for [file in files] file.'.dat'
Related
I am trying to use gnuplot to make a 'live' plot embedded in a C application, but I have encountered something that I want to change.
I am using gnuplot_i to 'interface' with gnuplot as if I were using gnuplot in the terminal.
Whenever I plot (two lines) I save the data to a file to then tell gnuplot to load the file and plot.
I would like to not have to save a data file and load the data directly into gnuplot. I do not know much of gnuplot so there may be a simple solution for what I want. What I would like is to have a command like this:
plot [1,2,3,4], [1,1,1,1], [1,2,3,4], [2,2,2,2]
|_____line 1_______| |______line 2______|
Is there a way to do this in gnuplot?
gnuplot> help datablock
There are two mechanisms for embedding data into a stream of gnuplot commands.
If the special filename '-' appears in a plot command, then the lines
immediately following the plot command are interpreted as inline data.
See `special-filenames`. Data provided in this way can only be used once, by
the plot command it follows.
The second mechanism defines a named data block as a here-document. The named
data is persistent and may be referred to by more than one plot command.
Example:
$Mydata << EOD
11 22 33 first line of data
44 55 66 second line of data
# comments work just as in a data file
77 88 99
EOD
stats $Mydata using 1:3
plot $Mydata using 1:3 with points, $Mydata using 1:2 with impulses
Data block names must begin with a $ character, which distinguishes them from
other types of persistent variables. The end-of-data delimiter (EOD in the
example) may be any sequence of alphanumeric characters.
I am using Tesseract for OCR character recognition using the Charles Weld C# wrapper. I am pre-processing the images with Open CV.
My issue is that I need to pre-process the image differently if it came from a dot matrix printout. Is there a way, using OpenCV, to tell that the image was scanned from a dot matrix printout?
I have tried blurring the image once and counting the differences using AbsDiff which is the technique I use to detect if an image needs to be despeckled but there is no consitent result that indicates dot matrix.
I had a few thoughts and decided to put them down in ImageMagick but you can equally do this sort of thing with OpenCV and its findContours().
I used this as an input image:
If you erode the black areas a little using morphology (or alternatively dilate the white, it comes to the same thing) each of the dots will become separated from adjacent ones. If you then do a "Connected Component Analysis" you will see that the image has an abnormally large number of very small dots which are roughly the same height as width - characteristic of circles or dots.
Here is the code I used in Terminal to run ImageMagick:
magick dotmatrix.png -threshold 50% -morphology dilate disk:1 \
-define connected-components:verbose=true \
-connected-components 8 -auto-level result.png
The output is this image wherein each detected blob gets a successively brighter shade of white:
More interesting though is the verbose output, which has one line of output per blob detected in the image. It shows lots of small 2x2, 3x2 and similarly sized dots with an area around 7 pixels, circled in red. I would use this as a base for exploring some more...
I'm very new to matlab, or coding for that matter.
I'm running a simulation which outputs thousands of files. These files are .vtk and are read correctly by dlmread.
I tried reading one of them, defining it as a matrix and extracting column vectors out of this matrix. This works fine. What i need now is to not only read one of them, but all. The filenames vary by a number, for example cover1000.vtk, cover2000.vtk, ...., cover1200000.vtk.
I want all of them to be read with dlmread and stored as a different matrix. How do i do that? Here is what i have right now, working with one file at a time:
A_1000 = dlmread ('cover1000.vtk') %matrix a containing values from vtk file_in_loadpath
fx_1000 = A(1:20,1) %extracting vector with specific values
fx_ave_1000 = sum(fx_1000)/length(fx_1000) % average of the values in extracted vector
I'm thinking of a loop, but how do i create a loop with varying file names?
Also I've read that a loop is not the best idea, cell arrays would be better. But i have absolutely no idea how to implement any of this.
Thanks for your help!
cheers
You can use the function dir to list all the vtk files in your directory then loop over those files.
filename = dir('*.vtk'); %list all the vtk files in your current directory.
for ii = 1:length(filename)
A = dlmread (filename(ii).name) %matrix a containing values from vtk file_in_loadpath
fx{ii} = A(1:20,1) %extracting vector with specific value
fx_ave{ii} = sum(fx{ii})/length(fx{ii}) % average of the values in extracted vector
end
The results are now stored in two cells: fx and fx_ave.
I have a large number of ASCII files named in order of the form data.xxxx.tab, where "xxxx" is a number between 0000 and 9999. Each file contains 5 columns, where the first is for X-coordinate, second is for Y-coordinate and the remaining three are for variables which I wish to plot against X-coordinate. I need to know how to write a loop in gnuplot 4.6, that could plot consecutive graphs of one of the variables against X-coordinate.
I already tried the instructions given in the following posts:
Plotting with gnuplot from several files
and
gnuplot : plotting data from multiple input files in a single graph
but these created a single graph containing all the curves from all the data files together, whereas what I need are consecutive graphs that are plotted one after another, thus showing the evolution in time of the variable graph.
The following should work:
# fix axes for proper comparison between graphs
set xrange [0:10]
set yrange [0:10]
# if you want an animated gif
set term gif animate
set output 'output.gif'
# then plot your data
do for [n=0:9999]{
plot sprintf("data.%04d.tab", n) using 1:2 title 'case '.n
}
The %04d string inside the sprintf command prints the number n with until four zeros before the minimum field width of n, i.e. n=2 is printed as 0002, and n=9999 is printed as 9999.
I would suggest using a shell script that calls a gnuplot file
file plot.gp:
set term png
set out fname.".png"
set title fname
plot fname w l
and then in the shell:
for fname in data.????.tab; do gnuplot -e fname=\"$i\" plot.gp; done
you'll get a file named data.xxxx.tab.png file for each data.xxxx.tab.
I am writing a program for event driven simulation of elastic collision of spherical balls(in 2D). As a output of the program I am collecting the collision points into a log file(say, data.temp) and plotting them using gnuplot.
I am following the instructions discussed here in this link. My output log file looks like this.
DATA.TEMP(All points are not shown)
20.000000 0.000000
3.535534 0.000000
3.535534 0.000000
45.000000 33.508349
-20.000000 -2.500000
-3.535534 -2.500000
-3.535534 -2.500000
-47.500000 -38.028654
-47.500000 -38.028654
The command used for gnuplot
char* commandForGnuplot[] = {"set title \"Path Plot\"","plot 'data.temp' with linespoints"};
I am able to get a plot like this
I want the paths for different balls to be colored differently. The color should be an input from the user. How can I do this?
Check out gnuplot's docs: http://gnuplot.sourceforge.net/docs_4.2/node62.html
There are some examples in there for defining plot styles. You will have to do some changes in your gnuplot command string -you will probably have to define a style and change your plot command into something like this (just stick your own color in there):
set style line 1 lt rgb "#FF00FF" lw 3 pt 6
plot 'data.temp' with linespoints ls 1