plotting fitted linear graphs at two different horizontal range values - gnuplot

I am plotting two graphs using gnuplot. First plot is actual data and the second one is the fitting of the data.
The script I used for plotting this is shown here below:
#!/usr/bin/gnuplot
reset
set terminal png enhanced
set terminal pngcairo enhanced color dashed
set output 'msd-maltoLyo12per-225ns.png'
##########################################
set macros
labelSIZE="font 'Arial,24'"
ticFONT="font 'Arial,16"
set key font 'Arial,14'
set key spacing 1.5 samplen 5
##########################################
set xrange [0:225]
set yrange [0:11000]
set xtic #ticFONT
set ytic #ticFONT
set xtics out nomirror
set ytics out nomirror
##############################
set style line 1 lt 1 lc rgb "red" lw 2.0
set style line 2 lt 2 lc rgb "blue" lw 2.0
set style line 3 lt 3 lc rgb "coral" lw 2.0
set style line 4 lt 4 lc rgb "green" lw 2.0
set style line 5 lt 5 lc rgb "black" lw 2.0
##############################
f(x)=a+b*x
fit [120:225] f(x) 'diff-xy-maltoLyo12per.dat' via a,b
plot 'diff-xy-maltoLyo12per.dat' using 1:2 with lines linestyle 1 title "{/Symbol b}Mal-C_{12}", f(x) lw 3.0 lc rgb 'black'
Here I plot the fitting graph from 1 to 120 as shown . Also I want to plot the same graph from range 120 to 225 as in the picture .
Now I want a single plot which contain the two black lines and the red line.
How can I achieve this?
Thanks in advance.

Working with the script you already have, you can use two functions to fit in the different ranges separately, and then use a conditional plot that selects one if x < 120 and the other one if x > 120:
f1(x)=a1+b1*x
fit [0:120] f1(x) 'diff-xy-maltoLyo12per.dat' via a1,b1
f2(x)=a2+b2*x
fit [120:225] f2(x) 'diff-xy-maltoLyo12per.dat' via a2,b2
f(x) = x < 120 ? f1(x) : f2(x)
plot 'diff-xy-maltoLyo12per.dat' using 1:2 with lines linestyle 1 title "{/Symbol b}Mal-C_{12}", f(x) lw 3.0 lc rgb 'black'
Now, the way I would go about this, would be to generate a special fitting function, whose parameters would give me the point at which the slope changes as a result of the fitting itself. Say you call that point x0 (for which the value of the function is y0), the slope at the left of it is m1 and the slope at the right m2. Then the function at the left has the form m1*(x-x0)+y0 and the function at the right has the form m2*(x-x0)+y0. The overall function can be defined in gnuplot as:
f(x) = x < x0 ? m1*(x-x0)+y0 : m2*(x-x0)+y0
and you can fit f(x) "data" via x0, m1, m2, y0. You can also generate this function without the condition using a step function:
f(x) = m1*(x-x0)*(sgn(x0-x)+1)/2 + m2*(x-x0)*(sgn(x-x0)+1)/2 + y0
After you fit, for which you might need to provide some initial values, you can print x0 and you'll get the best value (that should be close to 120 in your case, as you know) for the position of the change in slope.

Related

Bubble graphs - how to use?

Is it possible to combinate "normal points" and "bubble points"? I have two sets of data - file1.txt and file2.txt and for one of them (file2.txt) I would like to use points like http://gnuplot-tricks.blogspot.com/2009/06/
How to compose this into code please? When I use the code from that webpage how to plot another data. I tried this:
f(x) = A*exp(-x*x/B/B)
rx=0.107071; ry=0.057876; A = 1; B = 0.2; C=0.5*rx; D=-0.4*ry
g(u,v) = (2*cos(u)*v*rx+C)*(2*cos(u)*v*rx+C)+(3.5*sin(u)*v*ry+D)*(3.5*sin(u)*v*ry+D)
unset key; unset colorbox; set view map
set xrange [-0.15:5.2]; set yrange [-0.7:0.95]
set parametric; set urange [0:2*pi]; set vrange [0:1]
set isosamples 20, 20; set samples 30
set palette model HSV functions 1, 1-f(gray), 1+2*f(gray)
splot cos(u)*rx*v-0.13335347,sin(u)*ry*v+2.7730389, g(u,v) w pm3d, \
cos(u)*rx*v-0.11625481,sin(u)*ry*v+3.5312312, g(u,v) w pm3d, \
cos(u)*rx*v-0.14454222,sin(u)*ry*v+3.6412394, g(u,v) w pm3d, \
cos(u)*rx*v-0.070272446,sin(u)*ry*v+3.8070912, g(u,v) w pm3d, \
cos(u)*rx*v-0.10077238,sin(u)*ry*v+4.090774, g(u,v) w pm3d, \
'file1.txt' using 2:1:(0.0):2 with points pt 7 ps 1 palette
but file2 is not splot. Thank you very much
Here is a variant of the "trick" shown in your second link. I have extracted the customized point styles into a pre-defined set of linetypes. This makes the plot command itself simpler, and it is easier to reuse the definitions in multiple plots.
set linetype 101 ps 3.0 pt 7 lc rgb "#ff0000"
set linetype 102 ps 2.6 pt 7 lc rgb "#ff2222"
set linetype 103 ps 2.2 pt 7 lc rgb "#ff4444"
set linetype 104 ps 1.8 pt 7 lc rgb "#ff6666"
set linetype 105 ps 1.4 pt 7 lc rgb "#ff8888"
set linetype 106 ps 1.0 pt 7 lc rgb "#ffaaaa"
set linetype 107 ps 0.6 pt 7 lc rgb "#ffcccc"
set linetype 108 ps 0.2 pt 7 lc rgb "#ffeeee"
set border back
plot for [LT=101:108] 'silver.dat' using 1:2 with point lt LT notitle
Please clarify what exactly what properties your plot needs to have.
(1) The term "bubble plot" usually refers to a plot in which each point is drawn as a circle with additional properties encoded by changing the size, color, or other property of the circle. Gnuplot can do that quite well. A good example can be found in the online demo collection: Hypertext bubble chart In this case the size of the circle is used to indicate relative population and additional information is encoded as hypertext (pop-up text box) attached to that point. Variable color could easily be added as well. The png version below does not include the hypertext.
(2) The example you link in the query does not appear to encode any additional information into the shape or color of the point but it does use a fancy glyph for each point rather than a simple dot or cross. Gnuplot can do that also. It depends on exactly what set of symbols or glyphs you want to use. If you can find a font providing appropriate glyphs then one way is shown here:
shape(i) = (i%4 == 0) ? "⊕" : (i%4 == 1) ? "⊙" : (i%4 == 2) ? "⊚" : "⦾"
set grid x y z vertical
splot 'silver.dat' using 1:2:3:(shape(int(column(0))) with labels textcolor "forest-green"
More complicated options are also possible but may depend on exactly what you need and what output format (gnuplot "terminal type") is acceptable.

gnuplot curve from file and parametric sphere

I am trying to plot in a 3d space a curve coming from a file and a sphere made with parametric entries.
The idea is to plot the planet Earth and the orbit of a satellite.
The orbit is defined in a file x y z and gnuplot commands are simply
splot 'file.txt' u 1:2:3 title 'Orbit element 1' with lines
Orbit satellite :
I found a script to plot the Earth
#color definitions
set border lw 1.5
set style line 1 lc rgb '#000000' lt 1 lw 2
set style line 2 lc rgb '#c0c0c0' lt 2 lw 1
unset key; unset border
set tics scale 0
set lmargin screen 0
set bmargin screen 0
set rmargin screen 1
set tmargin screen 1
set format ''
set mapping spherical
set angles degrees
set xyplane at -1
set view 56,81
set parametric
set isosamples 25
set urange[0:360]
set vrange[-90:90]
r = 0.99
splot r*cos(v)*cos(u),r*cos(v)*sin(u),r*sin(v) with lines linestyle 2,'world.dat' with lines linestyle 1
unset parametric
Unfortunately, I am not able to mix splot wiht the data file and the splot with the parametric.
Any suggestions more than welcome!
Thanks
In order to generate the plot below, I used the data linked in this blog post. Now, if we want to combine several data sources into one plot, we will need to convert one or the other into a common system of coordinates. If the satellite data is in Cartesian x,y,z coordinates, perhaps the easiest solution would be to convert the world map into Cartesian system as well.
This could be done as shown below. The parameter R denotes the radius of the sphere on the surface of which Gnuplot draws the world map. It should be slightly larger than r so that hidden3d works. The columns in the world_110m.txt file have the meaning of longitude (first column) and latitude (second column), therefore the conversion is given as (R*cos($1)*cos($2)):(R*sin($1)*cos($2)):(R*sin($2)). In the file input.pnts.dat, I just generated coordinates of points on an ellipse with a=1.6 and b=1.2 rotated around the x axis by 45 degrees (counterclockwise). For real satellite data, one would need to rescale the coordinates by dividing by the radius of Earth, i.e., use ($1/Re):($2/Re):($3/Re) instead of 1:2:3, where Re denotes the radius in whichever units your data is (probably meters, judging by the first plot in your question).
set terminal pngcairo
set output 'fig.png'
set xr [-2:2]
set yr [-2:2]
set zr [-2:2]
#color definitions
set border lw 1.5
set style line 1 lc rgb '#000000' lt 1 lw 2
set style line 2 lc rgb '#c0c0c0' lt 2 lw 1
unset key; unset border; set tics scale 0
set format ''
set angles degrees
set xyplane at -1
set view 56,81
set lmargin screen 0
set bmargin screen 0
set rmargin screen 1
set tmargin screen 1
set parametric
set isosamples 25
set urange[0:360]
set vrange[-90:90]
r = 0.99
R = 1.00
set hidden3d
#since we are using Cartesian coordinates, we don't want this
#set mapping spherical
splot \
r*cos(v)*cos(u),r*cos(v)*sin(u),r*sin(v) with lines linestyle 2, \
'world_110m.txt' u (R*cos($1)*cos($2)):(R*sin($1)*cos($2)):(R*sin($2)) w l lw 2 lc rgb 'black', \
'input.pnts.dat' u 1:2:3 w l lw 2 lc rgb 'red'
This then gives:

Gnuplot columnstacked histogram with errorbars

Suppose I have the following data file, so-qn.dat:
Type on on-err off off-err
good 75 5 55 4
bad 15 2 30 3
#other 10 1 15 2
which contains values on columns 2 and 4 and corresponding error deltas on columns 3 and 5.
I can produce a columnstacked histogram:
#!/usr/bin/gnuplot
set terminal png
set output 'so-qn.png'
set linetype 1 lc rgb "blue" lw 2 pt 0
set linetype 2 lc rgb "dark-red" lw 2 pt 0
set style data histograms
set style histogram columnstacked
set style fill solid
set ylabel "% of all items"
set yrange [0:100]
set boxwidth 0.75
set xtics scale 0
set xlabel "Option"
plot 'so-qn.dat' using 2 ti col, \
'' using 4:key(1) ti col
But I can’t figure out how to add errorbars to this. The closest I got so far is with
plot 'so-qn.dat' using 2 ti col, '' using 2:3 with yerrorbars lc rgb 'black' ti col, \
'' using 4:key(1) ti col, '' using 4:5:key(1) with yerrorbars lc rgb 'black' ti col
which produces
but only one of the error bars is in the right spot (I actually have no idea where the bottom left one gets its y from), one is completely invisible (hidden behind the right stack?), and I’d like the error bars to not show up in the key.
Is it possible to combine column-stacked histograms and error bars?
You can add errorbars to column-stacked histograms by manually adding plot-commands for the errorbars. To do so, you need, however, to keep track of the y-positions.
Therefore, let's introduce two variables which store the y-position for each of the two columns' errorbars.
y1 = -2
y2 = -4
You need to initialize these variables with -(number of column)
Next, let us define two functions that update the variables y1, y2.
f1(x) = (y1 = y1+x)
f2(x) = (y2 = y2+x)
Now, generate the desired plot via
plot 'so-qn.dat' using 2 ti col, \
'' using 4:key(1) ti col, \
'' using (0):(f1($2)):3 w yerr t "", \
'' using (1):(f2($4)):5 w yerr t ""
As you can see, you can supress the errorbars in the key by assigning an empty title (t ""). This approach even gives you more flexibility in customizing the appearance of the errorbars (e.g., assign different linestyles etc.).
This being said, I personally think this visualization is rather confusing. You might want to consider another visualization:
set bars fullwidth 0
set style data histograms
set style fill solid 1 border lt -1
set style histogram errorbars gap 2 lw 2
plot 'so-qn.dat' using 2:3:xtic(1) ti columnhead(2), \
'' using 4:5:xtic(1) ti columnhead(4)

GNUplot - plot data file (simple X and Y columns) - setting suitable color and scale on a figure

I have a simple file with two columns:
1 0.005467
2 0.005333
3 0.005467
4 0.005467
5 0.005600
6 0.005600
7 0.005467
8 0.005467
In the first column I have the x-axis values, while on the second column I have y-axis values. I would like to plot a figure of this data. I wrote a gnuplot script for this:
#!/usr/bin/gnuplot
set xlabel "test"
set ylabel "value"
set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
set autoscale
set terminal postscript portrait enhanced mono dashed lw 1 'Helvetica' 14
set style line 1 lt 1 lw 3 pt 3 linecolor rgb "red"
set output 'out.eps'
plot 'data.txt' using 2:1 w points title "tests"
And, the output:
But of course, as a newbie in gnuplot, I have some troubles:
How to change the crosses on the fingure into dots?
How to change the color of the dots, to let's say, red? ( my command in my gnuplotscript seems not to work at all ...)
For the first test the adequate, accurate, exact value is 0.005467 but on my figure it doesnt look like so... I would like to place the dot on my figure for the first, second, third, (so on) test on the exact place, where is appropriate value.
How to add a grid to my figure? - SOLVED
How to get rid of the ugly text: 'data.txt' using 1:2 and replace it with a legend? - SOLVED
EDIT (SOLVED ISSUE NO 5)
plot 'data.txt' using 1:2 w points title "tests"
EDIT (SOLVED ISSUE NO 4)
set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
You should read a bit in the documentation about all your commands!
Several remarks:
If you want colored points, you shouldn't use the mono (i.e. the monochrome) option, but rather color.
Your definition of the line style is correct, but in order to use it you must use linestyle 1 when plotting. Otherwise the linetype 1 is used. Compare:
set style line 1 lt 1 lw 3 pt 3 linecolor rgb "red"
plot x, 2*x linestyle 1
In order to see all the dots of a terminal, use the test command:
set terminal postscript eps enhanced color dashed lw 1 'Helvetica' 14
set output 'test.eps'
test
set output
You see, that for filled dots you must use pt 7.
I'm sure, that the points are shown at the correct values. Use
set ytics add (0.005467)
to see this.

Add a single point at an existing plot

I am using the following script to fit a function on a plot. In the output plot I would like to add a single value with etiquette on the fitting curve lets say the point f(3.25). I have read that for gnuplot is very tricky to add one single point on a plot particularly when this plot is a fitting function plot.
Has someone has an idea how to add this single point on the existing plot?
set xlabel "1000/T (K^-^1)" font "Helvetica,20"
#set ylabel "-log(tau_c)" font "Helvetica,20"
set ylabel "-log{/Symbol t}_c (ns)" font "Helvetica,20"
set title "$system $type $method" font "Helvetica,24"
set xtics font "Helvetica Bold, 18"
set ytics font "Helvetica Bold, 18"
#set xrange[0:4]
set border linewidth 3
set xtic auto # set xtics automatically
set ytic auto # set ytics automatically
#set key on bottom box lw 3 width 8 height .5 spacing 4 font "Helvetica, 24"
set key box lw 3 width 4 height .5 spacing 4 font "Helvetica, 24"
set yrange[-5:]
set xrange[1.5:8]
f(x)=A+B*x/(1000-C*x)
A=1 ;B=-227 ; C=245
fit f(x) "$plot1" u (1000/\$1):(-log10(\$2)) via A,B,C
plot [1.5:8] f(x) ti "VFT" lw 4, "$plot1" u (1000/\$1):(-log10(\$2)) ti "$system $type" lw 10
#set key on bottom box lw 3 width 8 height .5 spacing 4 font "Helvetica, 24"
set terminal postscript eps color dl 2 lw 1 enhanced # font "Helvetica,20"
set output "KWW.eps"
replot
There are several possiblities to set a point/dot:
1. set object
If you have simple points, like a circle, circle wedge or a square, you can use set object, which must be define before the respective plot command:
set object circle at first -5,5 radius char 0.5 \
fillstyle empty border lc rgb '#aa1100' lw 2
set object circle at graph 0.5,0.9 radius char 1 arc [0:-90] \
fillcolor rgb 'red' fillstyle solid noborder
set object rectangle at screen 0.6, 0.2 size char 1, char 0.6 \
fillcolor rgb 'blue' fillstyle solid border lt 2 lw 2
plot x
To add a label, you need to use set label.
This may be cumbersome, but has the advantage that you can use different line and fill colors, and you can use different coordinate systems (first, graph, screen etc).
The result with 4.6.4 is:
2. Set an empty label with point option
The set label command has a point option, which can be used to set a point using the existing point types at a certain coordinate:
set label at xPos, yPos, zPos "" point pointtype 7 pointsize 2
3. plot with '+'
The last possibility is to use the special filename +, which generates a set of coordinates, which are then filtered, and plotted using the labels plotting style (or points if no label is requested:
f(x) = x**2
x1 = 2
set xrange[-5:5]
set style line 1 pointtype 7 linecolor rgb '#22aa22' pointsize 2
plot f(x), \
'+' using ($0 == 0 ? x1 : NaN):(f(x1)):(sprintf('f(%.1f)', x1)) \
with labels offset char 1,-0.2 left textcolor rgb 'blue' \
point linestyle 1 notitle
$0, or equivalently column(0), is the coordinate index. In the using statement only the first one is taken as valid, all other ones are skipped (using NaN).
Note, that using + requires setting a fixed xrange.
This has the advantages (or disadvantages?):
You can use the usual pointtype.
You can only use the axis values as coordinates (like first or second for the objects above).
It may become more difficult to place different point types.
It is more involved using different border and fill colors.
The result is:
Adding to Christoph's excellent answers :
4. use stdin to pipe in the one point
replot "-" using 1:(f($1))
2.0
e
and use the method in 3rd answer to label it.
5. bake a named datablock
(version > 5.0) that contains the one point, then you can replot without resupplying it every time:
$point << EOD
2.0
EOD
replot $point using 1:(f($1)):(sprintf("%.2f",f($1))) with labels
6. A solution using a dummy array of length one:
array point[1]
pl [-5:5] x**2, point us (2):(3) pt 7 lc 3
7. Or through a shell command (see help piped-data):
pl [-5:5] x**2, "<echo e" us (2):(3) pt 7 lc 3
pl [-5:5] x**2, "<echo 2 3" pt 7 lc 3
8. Special filename '+'
pl [-5:5] x**2, "+" us (2):(3) pt 7 lc 3
It seems to be the shortest solution. But note that while it looks like a single point, these are like 500 points (see show samples) plotted on the same position.
To have only one point the sampling needs to be temporarily adjusted (see help plot sampling)
pl [-5:5] x**2, [0:0:1] "+" us (2):(3) pt 7 lc 3
9. Function with zero sampling range length
Shortest to type, but plotting as many points on top of each other as many specified with samples
pl [-5:5] x**2, [2:2] 3 w p pt 7 lc 3

Resources