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.
Related
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"
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:
If I set a specific yrange and plot in a pdf terminal with this plot command:
plot "data.dat" u 1:4:5:6 w yerrorbars pt 6 ps 0.5 t "R_t"
errorbars that belong to data points outside the yrange, but end inside the yrange are not shown.
How do I force gnuplot to draw those. I already tried "set clip one/two"
The only workaround I found is to plot the data 3 times, once for the central point and once for each side of the error bar.
Use "-" as symbol for the errorbars and use their own "errorbars" to draw a line to the central point.
You could use multiplot to achieve this.
Set your plot to have zero margins, so the axes are on the border of the canvas, and switch of all tics and borders for the first plot.
Switch on the axes, tics etc. again, and do an empty plot that you set at the correct position using set size and set origin. You'll have to do some math to calculate the exact position.
#MaVo159, you can reduce it to plotting only twice by using with yerrorbars and with vectors (check help vectors). You need to set the proper arrow style, check help arrowstyle.
However, this works only for gnuplot>=5.2.3, for earlier versions there seems to be a bug which plots the arrowhead at the wrong side for some of the vectors extending the graph.
You nevertheless have to plot once with yerrorbars in order to get the proper legend.
Script: (works for gnuplot>=5.2.3, May 2018)
### plot errorbars from points outside the range
reset
$Data <<EOD
1 9 5.11 8.32
2 8 6.20 9.22
3 6 5.31 6.31
4 5 4.41 5.51
5 4 3.31 4.71
6 2.9 2.81 3.71
7 2 1.11 3.41
EOD
set yrange[3:7]
set offsets 1,1,0,0
set style arrow 1 heads size 0.05,90 lw 2 lc 1
set multiplot layout 2,1
plot $Data u 1:2:3:4 w yerrorbars pt 6 ps 2 lw 2
plot $Data u 1:2:3:4 w yerrorbars pt 6 ps 2 lw 2, \
'' u 1:3:(0):($4-$3) w vec as 1 notitle
unset multiplot
### end of script
Result:
You could modify your data file: Because the central value of the data point is outside the plot range you could set it equal to the errorbar's end point that would be still visible in your plot.
Example:
plot range: set yrange[-2:2]
data point: 1, -3, -1, -4 (x, y, ylow, yhigh)
set data point to: 1, -1, -1, -4
Attention: Since you have to edit your data file you should
Make a copy of the original data file
Be very careful when editing the file
Keep in mind, that when changing the plot range such that the central
value of the data point becomes visible you have to use the original data point. Otherwise you will see the correct error bar but there will be no central value plotted. (this is equivalent to setting 'point type' to 0)
I'd like to draw an impulse graph from a text file that looks like this:
II 5 0 0 288.40 1.3033e+14
II 6 0 0 289.60 1.5621e+14
II 1 4 0 302.70 3.0084e+13
II 2 4 0 303.40 4.0230e+13
II 1 5 1 304.40 3.4089e+13
The plot conceptually should be plot "datafile.dat" using 5:6 w impulses ls $2.
Basically, given a previously defined set of line styles, I'd like to input the line style number from column 2 for every couple of plotted points from column 5 and 6.
Also I'd like to create a text box, for every plotted point, taking strings from the first four columns.
Does somebody know if that's possible?
To use the data from column two as line style use set style increment user and linecolor variable:
set style increment user
plot "datafile.dat" using 5:6:2 with impulses lc var
In order to place a label, use the labels plotting style:
plot "datafile.dat" using 5:6:1 with labels offset 0,1
Putting everything together, you have:
set style increment user
set for [i=1:6] style line i lt i
set yrange [0:*]
set offsets 0,0,graph 0.1,0
plot "datafile.dat" using 5:6:2 with impulses lc var, "" using 5:6:1 with labels offset 0,1
The result with 4.6.3 is:
Thanks for the helpful answer above. It almost solved my problem
I'm actually trying to use a column from my data file to specify a linestyle (dot, squares,triangles, whatever as long as it's user-defined), and not a linecolor. Is there any way to do that?
This line works : I get points with different colors (specified in column 4), but the point style is the same.
plot "$file" u 1:2:4 w p notitle lc var, "" using 1:2:3 with labels offset 0,1 notitle
Replacing lc with ls after defining my own styles doesn't work (ls can't have variable as an option)
I can live without different linestyles, but it would be much prettier.
You only have to replace the lineset for [i=1:6] style line i lt i for set for [i=1:6] style line i lt i pt %, Where % can be any type of point you want
I want to draw two pointsline in a histogram by gnuplot, The result chart is not exactly what I wanted. Actually, I want points in two pointlines(the pink one and the lightgreen one) align the center of two types of pillars, so the lightgreen one keeps still, while the pink one move a little to the right, just like the black one I have drawn.
the 'test.dat' is as follow:
1 10 15 8 22
2 11 19 7 21
3 9 14 7 19
4 10 21 8 23
5 9 17 9 21
and the 'plt' file:
set style data histogram
unset key
set yrange[0:12]
set y2range[0:25]
plot "test.dat" using 2:xticlabel(1) axis x1y1,\
"test.dat" using 3:xticlabel(1) axis x1y2 with linespoints,\
"test.dat" using 4:xticlabel(1) axis x1y1,\
"test.dat" using 5:xticlabel(1) axis x1y2 with linespoints
My answer is based on this contribution which uses boxes instead of a histogram. The benefit is that, you know exactly where those boxes are placed, which you can exploit for the line plot.
Here's the code:
dx=1.
n=2.
total_box_width_relative=0.25
gap_width_relative=0.1
d_width=(gap_width_relative+total_box_width_relative)*dx/n
set boxwidth total_box_width_relative/n relative
set style fill transparent solid 0.5 noborder
plot "test.dat" u ($1):2 w boxes lc rgb"green" notitle,\
"test.dat" u ($1+d_width):4 w boxes lc rgb"red" notitle,\
"test.dat" u ($1):3 w linespoints notitle,\
"test.dat" u ($1+d_width):5 w linespoints notitle
set yrange [0:15]
replot
Some explanations to the code:
The dx should be selected according to your data file, otherwise the spacing of the boxes will be off.
The number of data-sets for the boxes is given by n
The total_box_width_relative and gap_width_relative control the axial with and spacing of the boxes
the two set ... commands control the appearance of your boxes
within the plot command you now have to separate: You call one set of boxes and lines with the original 1st column data: ($1), however, for the second set of boxes and the corresponding line, you choose your defined axial offset: ($1+d_width) - this will ensure that the data points from the line plot will align with the boxes
it may be necessary to include a set yrange command
The plot will look like this:
Note
I changed the data for the line plots with regard to the data you provided. This was done only to bring the points closer to the boxes and illustrate the effect.