Plotting on Gnuplot - skipping lines - gnuplot

I have some problems with reading my file in Gnuplot.
For example, I have a file like this:
___________________________________________
'#current'
month followed retweeted mentioned replied
Jan 395 29 35 28
Feb 380 28 32 31
'#previous'
month followed retweeted mentioned replied
Jan 381 30 38 32
Feb 378 25 42 30
Mar 374 28 46 40
______________________________________________________
I need to read only the second block, which starts with tag "#previous". How can I do it? I tried this command:
plot "data.txt" index 'previous' using 3:xticlabel(1) axes x1y1 with lines linecolor rgbcolor "red",\
but it doesn't work. Any ideas?

Check out the answer to this question
Gnuplot: Plotting several datasets with titles from one file
I think you need to add a 1 after index like so
plot "data.txt" index 1 using 3:xticlabel(1) axes x1y1 with lines linecolor rgbcolor "red"
edit:
Datasets are 0-indexed so the first block is indexed 0 and the second block (previous) has an index one. The error you mention regarding the bad line indicates a problem with our data file format.
Read about the data format at these links
http://www.gnuplotting.org/plotting-data/
http://lowrank.net/gnuplot/datafile2-e.html

Let us put everything together:
Following this link you can learn how to filter the file (so you can get everything after certain line)
So in our case:
sed -e '1,/previous/d' data.txt > gnuplot some_gnuplot_options
I write this from my windows devel machine so can't validate but this should give you some idea how can you do this.
I would also recommend defining gnuplot config file you feed to gnuplot. Just create settings.pg and put there something like this (that's my example from some work I have done for myself so it does not apply to your data format):
set terminal png size 1024, 360
set output "load.png"
set datafile separator "\t"
set timefmt "%Y-%m-%d %H:%M:%S"
set xdata time
set format x "%m/%d\n%H:%M"
set xrange["2012-04-29 11:00:00":"2012-05-01 11:58:00"] noreverse nowriteback
set autoscale y
set grid
set key left box
plot "chart.txt" using 1:2 t 'column_1' with lines, "chart.txt" using 1:3 t 'column_2' with lines
So then your gnuplot call would look something like this:
sed -e '1,/previous/d' data.txt > gnuplot your_pg_file.pg
You would also want to check time formating from gnuplot manuals here.
edit:
If that's your university homework you should not post your question here :-) I don't want to be boring or something but is it not the goal of homework that you find your solution following documentation study and trying different things eh? :-)

Related

Using nested for loop in gnuplot

I am trying to plot a set of graphs to compare the simulation results with the experimental data. The simulation files are in ordered arrangement on 7X7X7 for various parameters. I need to plot all of those files using a nested for loop for each iXjXk file. The files are named thus : fibrilAll_i_j_k.dat
I have already tried some alternatives like using multiple for loop in the same line. But it doesn't seem to work.
set terminal eps size 1200,800
set output "all.eps"
set title "{/*2 Alternative rates}"
set ylabel "{/*2 fibril mass fraction}" offset 1.5,0,0
set xlabel "{/*2 Time(h)}"
set key left top
plot 'experiment.txt' using 1:6 ps 2 pt 5 title "EXP",\
for [i=1:7] for [j=1:7] for [k=1:7] 'fibrilAll'._i_j_k.'.dat' using 1:2 with lines title 'i,j,k'
replot
I get the following error message:
internal error : STRING operator applied to undefined or non-STRING variable
I see a few possible problems.
1) I take it you do not want to plot the same file fibrilAll_i_j_k.dat 343 times.
If the data files are named e.g. fibrilAll_1_5_3.dat then you can construct that name by saying plot ... sprintf("fibrilAll_%d_%d_%d.dat",i,j,k)
2) Probably you want something similar for the titles
3) The replot does not accomplish anything. Did you leave out something?

Simple boxes graph using Gnuplot

I have a data file called for example dat.txt
0 3111 1755 2577 1895 3224 1725 2163 1641 2525 331
it's got a single line of data and for some reason i wanna plot them as vertical boxes for each value, rather than a line connecting all of the data points.
How can i do that with Gnuplot please, I think the version im stuck with for gnuplot is 4.2.6
What about help boxes or searching for boxes in the documentation? So, for boxes, use the boxes plotting style.
In any case, you must have your data organized in columns rather than as single row. On *nix you can do that on-the-fly with tr:
set style fill solid
set boxwidth 0.9
plot '< tr " " "\n" < dat.txt' with boxes
The output, with version 4.2.6 is:

Gnuplot nested do loop and plotting on the same graph

Hi I'm using gnuplot to create a large number of graphs. To save myself work I've written a script that tries to plot all possible graphs.
#!/usr/bin/gnuplot
reset
is_missing(x)=system("./ismissing.sh ".x)
set terminal post eps enhanced color
set key inside right top vertical Right noreverse enhanced autotitles box linetype -1 linewidth 1.000
set grid
models="Cebeci-Smith Baldwin-Lomax Spalart-Allmaras Launder-Sharma Chien"
files="./CebeciOut/ ./BaldwinOut/ ./SpalartOut/ ./LaunderOut/ ./ChienOut/"
xgrid="35000 70000 140000 280000 560000"
# Plot grid convergence Spalart
do for [f=1:words(files)]{
set output sprintf('%s%s',word(files,f),'ZPGGridConvergence.eps')
set title sprintf("%s%s",word(models,f), " ZPG Grid Convergence")
set xlabel "N_y"
set ylabel "C_f Final"
print f
do for [i=1:words(xgrid)]{
filename=sprintf("%s%s%s%s",word(files,f),'yconv_',word(xgrid,i),"x")
if(is_missing(filename)==0){
plot filename using 1:2 title sprintf("%s%s",word(xgrid,i), " X Gridpoints") with linespoints
}
}
}
Unfortunately I have a problem, after one full iteration the script fails, ie f is never made 2 despite the first plots completing successfully. The error given is
cannot open file; output not changed
line 25: util.c: No such file or directory
which I haven't managed to decipher. Any help would be much appreciated. Also is there a way to use replot in this loop given I haven't already created a plot.
Thanks
"files" are actually dirs in your code.
If you want to place the output in a subdir, then make sure that it exists and is writable.
gnuplot cannot create subdirs automatically. You can add system("mkdir -p ".word(files,f)) ( on linux/unix) in your code to create the directory if needed.

Is it possible to set the "linetype" in gnuplot while using "linecolor variable"?

I have a tab-separated data file containing a number of (double-blank-line-separated) datasets and I want to plot a line for each of them. I want to be able to set the linetype (by this I'm referring to solid/dashed/dotted). I want each line to be a different color.
I can plot them all different colors using this:
plot 'example.dat' using 1:2:(column(-2)) with lines linecolor variable
And I can set the linetype but plot them all the same color using this:
plot 'example.dat' using 1:2:(column(-2)) with lines linetype 5
But when I combine them, the linetype is not what I set it to (in this case I just get a solid line).
plot 'example.dat' using 1:2:(column(-2)) with lines \
linetype 5 linecolor variable
Is there a way to achieve this?
I'm using gnuplot 4.6, tried with the x11 and postscript terminals.
This looks like a bug to me. Unfortunately, I don't think too many gnuplot devs hang out on StackOverflow, so we might not ever find out. (I would encourage you to submit a bug report though and keep me updated on any progress that might be made)...
If you're really using column(-2) to pick out the colors, the problem becomes a lot easier and you should be able to do that using plot iteration (as long as you know an upper limit on the number of datasets).
NDSET=3 #This will issue a warning if NDSET is too big, but will still work.
plot for [IDX=0:NDSET] 'example.dat' index IDX using 1:2 with lines linetype 5 linecolor IDX+1
The indexing starts from 0 and corresponds to column(-2). Linecolor 0 isn't defined (I don't know why gnuplot uses two different conventions here -- I suppose because in theory the colors corresponding to any particular linestyle are terminal dependent, so it doesn't really matter too much anyway...)

Plotting block data in 3d using different colors and smoothing the lines in Gnuplot or Octave

I am trying to plot (with Gnuplot) some basic 3d data from one file which is pretty much like that:
N M t1 t2 t3 t4
1000 1000 0.05268 0.04711 0.003947 0.003348
1000 2000 0.05743 0.04214 0.007577 0.006486
1000 3000 0.08465 0.04193 0.011329 0.009654
2000 1000 0.10726 0.08845 0.013593 0.012397
2000 2000 0.21065 0.10817 0.026525 0.024390
2000 3000 0.31528 0.16960 0.039772 0.036405
3000 1000 0.25415 0.14845 0.031082 0.026364
3000 2000 0.47345 0.25227 0.060887 0.051840
3000 3000 0.70612 0.36866 0.091311 0.077432
The idea is to plot it in some way I could see t1,t2,t3 and t4 for each N and M. It may be a lot of data to plot in only one graph, I know that. First of all, I have started with t1 plotting this way:
splot 'aux' u 1:2:3 w lp
and I get something like that
I would like to smooth a bit those lines and give them a different color. Is there any way to do something for it? Also any ideas to improve it in any other way would be very nice.
You're right, that is a lot of data. Changing the color of a particular dataset is pretty easy:
splot "dataset" using 1:2:3 w lines linecolor rgb "green"
Note that you can easily overlay multiple plots as follows:
splot "dataset" using 1:2:3 w lines lc rgb "green",\
"" using 1:2:4 w lines lc rgb "red"
A backslash at the end of a line is the gnuplot line-continuation character. Note that it must be the last character on the line. I've also used the pseudo-file "" which is just shorthand for the last file that gnuplot read. Finally, in this second version I used lc instead of linecolor. The gnuplot parser provides a lot of shorthand, although I suspect you already know this since you plotted w lp. (Also, please don't actually choose red and green for your plots. I think one of the biggest flaws in gnuplot is the default first 2 colors are red and green -- 1 in 20 people is red-green colorblind)
Smoothing is another story (unfortunately). plot does support a smooth option
plot "dataset" using 1:2 smooth beizer with lines #see help smooth for a list of options
Unfortunately, it appears that this option doesn't work for splot. At this point, I would write a small utility script to smooth the data in your favorite language using your favorite smoothing algorithm (plot "<utility.script mydata" u 1:2:3 ...). There are other (gnuplot-only) options, but they'd be ugly. What version of gnuplot do you have by the way? The gnuplot version is important to decide the best way to do the smoothing all in gnuplot if you decide that's still necessary.

Resources