Gnuplot point interval from point plot - gnuplot

I know that I can control point interval by "pointinterval" or "pi" command. However, this command only works with "linespoint" or "lp" plotting style.
Can I manage point interval with "points" or "p" plotting style?
For example:
plot data u 1:2 w p pt 7 ps 1 pi 2000 lc rgb "red" title "density"
Here, I tried "pi 2000" for point interval with points plotting style but it is not working

In the points plotting style, you can control which data points to plot in regular intervals with the 'every' keyword. It goes right after the data file name. If you really wanted to only plot every 2000 data points:
plot "dataFile" every 2000 u 1:2 w p pt 7 ps 1 lc rgb "red" title "density"
"every" is quite sophisticated. Type 'help every' at a gnuplot prompt and you can get more details.

Related

Gnuplot XRD graph, connecting points

I have a XRD data and when I plot it I want to have this kind of graph. Anyway, excel has a problem to plot too large data and I want to plot it with Gnuplot and here is my code
set title "GNUPLOT RESULT"
set xlabel "Wavelength 2Theta"
set ylabel "Intensity"
set xrange [20:90]
set key right center
set terminal pngcairo size 1600, 1000 enhanced font "Arial,16"
set output "Allt-XRD.png"
plot "AllW" using 1:2 w p pt 7 ps 2 lc rgb "orange" title "point", "AllW" using 1:2 smooth acspline lw 3 lc rgb 'blue' title 'spline'
But what it produces, it does not connect all dots/points and I do not know but somehow it has a preferences (is it a weight point?) to connecting them.
Question
How can I connect all the dots as seen at excel graph with Gnuplot
Thanks in advance
P.S: I tried all bunch of smooth version acscpline' cspline' bezier etc. it did not work
Edit 1: The line plot who wonders why I do not try it
Edit 2: The worked answer of user8153 : Use decimal data point not an integer. Both spline and points option plot perfectly the data as it seen below
How XRD data looks like, it is too long so I pasted only a few of them
Wavelength = 1.54059 Å (Cu)
Angle Intensity
20.00243 1467
20.02869 1533
20.05495 1482
20.08121 1468
20.10747 1376
20.13374 1421
20.16000 1433
20.18626 1380
20.21252 1431
20.23878 1405
20.26504 1357
20.29130 1374
20.31756 1413
Your with points plot shows that your data contains only integer values of the wavelength, but each value has multiple intensities associated with it. Is that really what the data should look like, or was there some mistake that chopped off the values of the wavelengths after the decimal point? Maybe your data file uses a symbol for the decimal point that gnuplot doesn't recognize? If so, use set decimalsign so gnuplot realizes that you are feeding it floating point numbers.
As it is, gnuplot does precisely what you tell it to do: it plots all these points at the same x coordinate, and connects them with lines if you use with lines, which are then by construction vertical.
You told it to plot "with points pointtype 7 pointsize 2" (shorthand "w p pt 7 ps 2"). So it did.
If you want it to plot with lines then say "with lines".
plot "AllW" using 1:2 with lines lc rgb "orange" title "lines"

How to remove line between "jumping" values, in gnuplot?

I would like to draw a line with plots that contain "jumping" values.
Here is an example: when we have plots of sin(x) for several cycles and plot it, unrealistic line will appear that go across from right to left (as shown in following figure).
One idea to avoid this might be using with linespoints (link), but I want to draw it without revising the original data file.
Do we have simple and robust solution for this problem?
Assuming that you are plotting a function, that is, for each x value there exists one and only one corresponding y value, the easiest way to achieve what you want is to use the smooth unique option. This smoothing routine will make the data monotonic in x, then plot it. When several y values exist for the same x value, the average will be used.
Example:
Data file:
0.5 0.5
1.0 1.5
1.5 0.5
0.5 0.5
Plotting without smoothing:
set xrange [0:2]
set yrange [0:2]
plot "data" w l
With smoothing:
plot "data" smooth unique
Edit: points are lost if this solution is used, so I suggest to improve my answer.
Here can be applied "conditional plotting". Suppose we have a file like this:
1 2
2 5
3 3
1 2
2 5
3 3
i.e. there is a backline between 3rd and 4th point.
plot "tmp.dat" u 1:2
Find minimum x value:
stats "tmp.dat" u 1:2
prev=STATS_min_x
Or find first x value:
prev=system("awk 'FNR == 1 {print $1}' tmp.dat")
Plot the line if current x value is greater than previous, or don't plot if it's less:
plot "tmp.dat" u ($0==0? prev:($1>prev? $1:1/0), prev=$1):2 w l
OK, it's not impossible, but the following is a ghastly hack. I really advise you add an empty line in your dataset at the breaks.
$dat << EOD
1 1
2 2
3 3
1 5
2 6
3 7
1 8
2 9
3 10
EOD
plot for [i=0:3] $dat us \
($0==0?j=0:j=j,llx=lx,lx=$1,llx>lx?j=j+1:j=j,i==j?$1:NaN):2 w lp notit
This plots your dataset three times (acually four, there is a small error in there. I guess i have to initialise all variables), counts how often the abscissa values "jump", and only plots datapoints if this counter j is equal to the plot counter i.
Check the help on the serial evaluation operator "a, b" and the ternary operator "a?b:c"
If you have data in a repetitive x-range where the corresponding y-values do not change, then #Miguel's smooth unique solution is certainly the easiest.
In a more general case, what if the x-range is repetitive but y-values are changing, e.g. like a noisy sin(x)?
Then compare two consecutive x-values x0 and x1, if x0>x1 then you have a "jump" and make the linecolor fully transparent, i.e. invisible, e.g. 0xff123456 (scheme 0xaarrggbb, check help colorspec). The same "trick" can be used when you want to interrupt a dataline which has a certain forward "jump" (see https://stackoverflow.com/a/72535613/7295599).
Minimal solution:
plot x1=NaN $Data u 1:2:(x0=x1,x1=$1,x0>x1?0xff123456:0x0000ff) w l lc rgb var
Script:
### plot "folded" data without connecting lines
reset session
# create some test data
set table $Data
plot [0:2*pi] for [i=1:4] '+' u 1:(sin(x)+rand(0)*0.5) w table
unset table
set xrange[0:2*pi]
set key noautotitle
set multiplot layout 1,2
plot $Data u 1:2 w l lc "red" ti "data as is"
plot x1=NaN $Data u 1:2:(x0=x1,x1=$1,x0>x1?0xff123456:0x0000ff) \
w l lc rgb var ti "\n\n\"Jumps\" removed\nwithout changing\ninput data"
unset multiplot
### end of script
Result:

Gnuplot: Aligning several graphs

I have two time-domain signals. Now I want to plot the range 10..20 seconds from signal 1 and the range 12...22 seconds from signal 2 in a single plot. However, I tried it this way, which fails:
plot [10:20] 'signal1.txt' using 1:2 with line lt -1 lw 1 fc rgb "black" title 'Signal 1',\
[12:22] 'signal2.txt' using 1:2 with line lt -1 lw 1 fc rgb "black" title 'Signal 2'
gnuplot says "invalid expression" for to the second range definition ([12:22]).
Any ideas?
If one tries
plot [3:6] sin(x), [-2:8] cos(x)
it's easy to realize that the only interval that counts is the first one.
To achieve his/her intent the OP may recur to an external program or use something similar to
set xrange [-10:10]
plot x<3?1/0:x<=6?sin(x):1/0, x<-2?1/0:x<=8?cos(x):1/0
The external program could simply be a shell script that leverages on awk to filter not interesting abscissae.
It's relatively easy to map my proposal to OP requirements
plot 's1' using 1:($1<10?1/0:$1<=20?$2:1/0) w l, 's2' using 1:($1<14?1/0:$1<=22?$2:1/0) w

Displaying markers on specific values in Gnuplot's line plot

I have data for a CDF in a file which looks like the following:
0.033 0.0010718113612
0.034 0.0016077170418
0.038 0.0021436227224
... ...
... ...
0.847 0.999464094319
0.862 1.0
First column is the X-axis value and the second column is the CDF value on Y-axis. I set the line style as follows:
set style line 1 lc rgb 'blue' lt 1 lw 2 pt 7 ps 0.75 # --- blue
and subsequently plot the line with the following:
plot file1 using 1:2 title 'Test Line CDF' with linespoints ls 1
This all works fine, the problem seems to be that my CDF file is pretty big (about 250 rows) and Gnuplot would plot the marker/point (a circle in this case) for every data point. This results in a very "dense" line because of the over-concentration of markers such that the underlying line is almost not visible as I show in an example image below:
How can I selectively draw the markers so that instead of having them on all data points, I plot them after every 50 data points, without having to decrease the number of data points (which I believe is what "every n" in the plot command would do) in my data file or decrease the marker size?
There is no need to use two plots commands, just use the pointinterval option:
plot 'data' pointinterval 5 with linespoints
That plots every line segment, but only every fifth point symbol.
The big advantage is, that you can control the behaviour with set style line:
set style line 1 lc rgb 'blue' lt 1 lw 2 pt 7 ps 0.75 pi 5
plot 'data' w lp ls 1
You can plot the same function twice, once with lines only, and then with points every n points. This will draw less points without decreasing the amount of segments. I think this is what you want to achieve. For this example I have done set table "data" ; plot sin(x) to generate numerical sampling of the sin(x) function.
What you have at the moment is:
plot "data" with linespoints pt 7
which gives
Now you can do the following:
plot "data" with lines, "data" every 10 with points pt 7 lc 1
which gives what you want:
You can change the styling to meet your needs.
Although #Miguel beat me to it, but I'm also posting my solution below:
The idea is to once draw the line and then draw the points with the "every n" specifier. I changed my own Gnuplot script in the following manner. A kind of hack but works:
set style line 1 lc rgb 'blue' lt 1 lw 2 pt 7 ps 0 # --- blue
plot file1 using 1:2 title '' with linespoints ls 1, "" using 1:2 every 20 title 'Test Line CDF' with points ls 1 ps 0.75
This retains the nice curve, without quantizing it too coarsely while also keeping the points much better spaced.

How to plot a point on a function in gnuplot

I need to plot a point on a graph of a function. I've seen gnuplot> plot “< echo ‘x y’” as an example, however, that only works on the terminal and I need it to be on a .gp file. I've also seen doing set parametric plot 4,3 but then I can't plot the point over the graph of the function.
Is there a way to do this?
Try something like:
set terminal postscript enhanced color
set output 'plot.eps'
set xr[-10:10]
set yr[-2:2]
plot sin(x), \
"<echo '3.141592 0'" pt 7 ps 2 notitle
The comma allows you to plot multiple things in the same plot. This way you have to specify the range and the point position manually, but it works for me in a plot script in gnuplot 4.6.
another option is to use 'inline data'
plot sin(x) w lines, '-' w p pt 7 ps 5
3.14 0
e

Resources