what is wrong here for plotting dos in gnuplot? - gnuplot

My script:
set term postscript enhanced colour
set output 'dos.ps
set key box font 'Helvetica, 12' left center
set key at 6,15
set grid
set size noratio 1, 0.8
set style fill transparent solid 1.0 noborder
set style data filledcurves y1=0
set arrow from 0, graph 0 to 0, graph 1 nohead
set xlabel "E-E_f [eV]" font 'Helvetica, 20'
set arrow 5 head filled size screen 0.02,13 from -0.5,-17.3 to 0.4,-17.3 lc rgb "black" lw 3
set arrow 6 head filled size screen 0.02,13 from -9.05,5.5 to -9.05,8.4 lc rgb "black" lw 3
set ylabel "Density of states" font 'Helvetica, 20'
plot [-5:3] [-10:10] 'dos.dat' using ($1-12.6258):2 with filledcu lc rgb "red" title "Spin Up",'dos.dat' using ($1-12.6258):($3*-1) with filledcu lc rgb "#4169E1" title "Spin Down",0.0 lc rgb "black" title '','dos.dat' with lines lc rgb "black" title ''
set output
! ps2pdf -r300 dos.ps
! rm dos.ps
! pdftocairo -png -r 300 dos.pdf
What I get:
What I want:
Why are the curves not filled with the above script?

There are a few things in your script which I don't understand:
why are you creating a postscript file which you later convert to PDF and then to PNG. Why not directly using terminal pngcairo?
set key at 6,15 is outside the graph
set size noratio 1, 0.8 What's the purpose of this?
set style data filledcurves y1=0 (haven't seen/used this so far)
arrows 5 and 6 are outside the graph
... 0.0 lc rgb "black" title '' Plot the x-axis again?
...'dos.dat' with lines lc rgb "black" ... Plot a black outline again?
Check the following minimized script as starting point. You posted only data as image which doesn't help too much. So, I used some data.
Data: SO74355087.dat
7.0 1.0 1.0
7.5 0.1 1.0
8.0 3.0 4.0
8.5 4.0 3.0
9.0 0.5 2.0
9.5 7.0 5.0
10.0 4.0 7.0
10.5 1.0 1.0
11.0 0.0 2.0
11.5 9.0 3.0
12.0 0.0 0.0
12.5 3.0 0.0
13.0 2.5 0.0
13.5 2.0 0.0
14.0 1.0 5.0
14.5 1.0 3.0
15.0 0.5 1.0
15.5 0.0 0.5
Script:
### plot with filledcurves
reset session
FILE = "SO74355087.dat"
set key box font 'Helvetica, 12' top right noautotitle
set grid
set style fill transparent solid 1.0 noborder
set xlabel "E-E_f [eV]" font 'Helvetica, 20'
set ylabel "Density of states" font 'Helvetica, 20'
set xzeroaxis
set xrange[-5:3]
set yrange[-10:10]
set term pngcairo size 640,384
set output "SO74355087.png"
plot FILE u ($1-12.6258):2 w filledcurves y=0 lc rgb "red" ti "Spin Up", \
'' u ($1-12.6258):(-$3) w filledcurves y=0 lc rgb "#4169E1" ti "Spin Down"
set output
### end of script
Result:
Addition:
Ok, you want to smooth the curve. Check help smooth. There are different options: e.g. bezier, splines, kdensity. Depending on what data you have and what you want to see (how smooth) you need to play with these options.
For the script above, if you use the same data but change the plotting lines to:
plot $Data u ($1-12.6258):2 smooth cspline w filledcurves y=0 lc rgb "red" ti "Spin Up", \
'' u ($1-12.6258):(-$3) smooth kdensity bandwidth 0.2 w filledcurves y=0 lc rgb "#4169E1" ti "Spin Down"
You will get the following. Please read help smooth for getting a deeper understanding.
Addition: smoothing options
Please read help smooth and play with the numbers set sample 500 and if you use smooth kdensity bandwidth 0.02.
Script:
### plot with filledcurves
reset session
FILE = "SO74355087.dat"
FILE = "dos.dat"
set key box font 'Helvetica, 12' top right noautotitle
set grid
set style fill transparent solid 1.0 noborder
set style data filledcurves y1=0
set xlabel "E-E_f [eV]" font 'Helvetica, 20'
set ylabel "Density of states" font 'Helvetica, 20'
set xzeroaxis
set xrange[-5:3]
set yrange[*:*]
set term pngcairo size 640,900
set output "SO74355087.png"
set samples 500
set multiplot layout 3,1
plot FILE u ($1-12.6258):2 w filledcurves y=0 lc rgb "red" ti "Spin Up", \
'' u ($1-12.6258):(-$3) w filledcurves y=0 lc rgb "#4169E1" ti "Spin Down"
plot FILE u ($1-12.6258):2 smooth bezier w filledcurves y=0 lc rgb "red" ti "Spin Up", \
'' u ($1-12.6258):(-$3) smooth bezier w filledcurves y=0 lc rgb "#4169E1" ti "Spin Down"
plot FILE u ($1-12.6258):2 smooth kdensity bandwidth 0.02 w filledcurves y=0 lc rgb "red" ti "Spin Up", \
'' u ($1-12.6258):(-$3) smooth kdensity bandwidth 0.02 w filledcurves y=0 lc rgb "#4169E1" ti "Spin Down"
unset multiplot
set output
### end of script
Result:
Top graph: original data as is
Middle graph: smooth bezier
Bottom graph: smooth kdensity bandwidth 0.02
You might want to scale the y-values... or remove ytics to have arbitraty units...

Related

How to use gnuplot to plot history graph with labels

I have two small datafiles
one
11 365.4
12 659.2
and one
11 432.1
12 882.4
I try to plot those as a histogram with labels
gnuplot <<EOF
set output 'house-energy.png'
set terminal png size 800,400 font "Arial,10"
set boxwidth 0.7 relative
set grid ytics linestyle 0
set style fill solid 0.20 border
set style data histogram
#set style histogram columnstacked
#set style histogram rowstacked
set title "Energy"
set xlabel "Month"
set ylabel "kWh"
set yrange [0:2000]
plot 'file1.dat' u 2: xtic(1) with histogram lc rgb "#0045FF" title "Energy house total", \
'' using 1:(\$2):(\$2) with labels notitle font ",10" , \
'file2.dat' u 2: xtic(1) with histogram lc rgb "#004500" title "Labb", \
'' using 1:(\$2):(\$2) with labels notitle font ",10"
EOF
But the labels are way off
Image showing plot
// GH
The histogram plotting style is implicitly using the pseudocolumn 0 (check help pseudocolumns) as x-coordinate. So, you have to place your label at column(0) or ($0) with some x-offset, e.g. ($0-0.15) in the one and other direction. And some offset in y-direction, e.g. via offset 0,0.7.
Script:
### histogram with labels
reset session
$Data1 <<EOD
11 365.4
12 659.2
EOD
$Data2 <<EOD
11 432.1
12 882.4
EOD
set boxwidth 0.7 relative
set grid ytics linestyle 0
set style fill solid 0.20 border
set style data histogram
set title "Energy"
set xlabel "Month"
set ylabel "kWh"
set yrange [0:2000]
set key noautotitle
plot $Data1 u 2:xtic(1) w histogram lc rgb "#0045FF" title "Energy house total", \
'' u ($0-0.15):2:2 w labels font ",10" offset 0,0.7, \
$Data2 u 2 w histogram lc rgb "#004500" title "Labb", \
'' u ($0+0.15):2:2 w labels font ",10" offset 0,0.7
### end of script
Result:

How to plot partial density of states in gnuplot?

I tried to plot using this script but it doesnt work well.
set term postscript enhanced colour
set output 'pdos.ps'
set key box font 'Helvetica, 12' top right noautotitle
set grid
set xlabel "E-E_f [eV]" font 'Helvetica, 20'
set ylabel "PDos(states/ev)" font 'Helvetica, 20'
set xzeroaxis
set xrange[-6:4]
set yrange[*:*]
plot 'Co_d.dat' u 1:2 w l lc "green", 'Mn_d.dat' u 1:2 w l lc "red', 'Sb_p.dat' u 1:2 w l lc "#4169E1"
set output
I expect this like this plot as in this image I mentioned.
I got image like this.
Plot your data first without setting a range, default is autorange or set xrange[*:*]. Then you will see the region of interest.
And in your case this is not set xrange[-6:4] as you set, but maybe set xrange[6:16].
Probably, you are looking for a script like this:
reset session
set key box font 'Helvetica, 12' top right noautotitle
set xlabel "E-E_f [eV]" font 'Helvetica, 20'
set xrange[-6:4]
set mxtics 2
set xzeroaxis lt 1 lc "black"
set ylabel "PDos(states/ev)" font 'Helvetica, 20'
set yrange[-10:10]
set yzeroaxis lt 1 dt 2 lw 1.5 lc "black"
set grid x,y
set label at first 0.25, first 5 "Spin-Up" font "Helvetica, 12"
set label at first 0.25, first -5 "Spin-Down" font "Helvetica, 12"
set label at first -0.5, first 8 "Fermi-Level" font "Helvetica, 12" right
set arrow from first 0.75, 5.5 to 0.75, 7.5
set arrow from first 0.75, -5.5 to 0.75, -7.5
set arrow from first -0.4, 8 to 0.0, 8
plot 'Co_d.dat' u ($1-12.6258):2 w l lc "green" ti "Co-4d", \
'' u ($1-12.6258):(-$3) w l lc "green", \
'Mn_d.dat' u ($1-12.6258):2 w l lc "red" ti "Mn-4d", \
'' u ($1-12.6258):(-$3) w l lc "red", \
'Sb_p.dat' u ($1-12.6258):2 w l lc "#4169E1" ti "Sb-2p", \
'' u ($1-12.6258):(-$3) w l lc "#4169E1"

Plot a error bar as shaded region in GNUPLOT

I have plotted a graph (X-top axis, Y-bottom axis) with fsteps function in Gnuplot. Next, I tried to add an error bar as a shaded region(transparent) to the graph, but unable to plot it on the graph. Below is the code so far I have tried and also attached the graph.
#!/usr/bin/gnuplot
reset
set border lw 30
set term pngcairo size 10000,10000 font "arial-Bold,130"
set output 'out.png'
unset key
set size ratio 1.2
set style data lines
set xtics format ""
set x2tics nomirror
set ytics out nomirror
set ytics 0,20
set x2label "Vs (km/s)" offset -1.0
set ylabel 'Depth (km)' offset 1.5
set xrange [2.5:4.8]
set yrange [314:0]
set label 3 at 2,120
set key samplen 1.7 at 3.0,135
#
set label 1 '(a)' font "arial-Bold,130" at 0.8,15 right
set label 3 "C3 (MNAI)" center font "arial-Bold,130"
set style fill transparent solid 0.25
set style fill noborder
plot 'MAN.inmd' lc rgb 'blue' lw 35 title "Initial model" with fsteps,\
'MAN.outmd' using 1:2 lc rgb 'red' lw 35 dt"-" title "Inverted model" with fsteps ,\
'MAN.outmd' using 1:($2-$3):($2+$3) with filledcurve lc "blue" notitle,
Example Data for file MAN.outmd X Y Z(Error)
0 3 0
0.4475 3.1 0
0.4475 3.5 0
2.6738 3.6 0.0552
2.6738 5 0.0552
3.8441 5.1 0.0592
3.8441 8 0.0592
3.6302 8.1 0.0395
3.6302 15.935 0.0395
4.5176 15.1 0.041
4.5176 113.296 0.041
4.2443 113.3 0.1024
4.2443 214 0.1024
4.4584 214.1 0.1077
4.4584 314 0.1077
I want output should be as given below (example)
gnuplot can easily fill the area between two "horizontal" curves (i.e. unique x-values), but as far as I know, not between two vertical curves. However, gnuplot can fill some enclosed areas. So, the workaround is to create datapoints which surround the area to be shaded. For this, you "plot" the data into a datablock, once "forward" with x-dx and once "backwards" with x+dx. This can be done easiest if you have the data already in a datablock, because then you can easily loop the data forward and backwards. In case you have your data in a file, see here: gnuplot: load datafile 1:1 into datablock
Code:
### fill between vertical curves
reset session
$Data <<EOD
0 3 0
0.4475 3.1 0
0.4475 3.5 0
2.6738 3.6 0.0552
2.6738 5 0.0552
3.8441 5.1 0.0592
3.8441 8 0.0592
3.6302 8.1 0.0395
3.6302 15.935 0.0395
4.5176 15.1 0.041
4.5176 113.296 0.041
4.2443 113.3 0.1024
4.2443 214 0.1024
4.4584 214.1 0.1077
4.4584 314 0.1077
EOD
# create datablock with circumference of shaded area
set print $XErrorFill
do for [i=1:|$Data|] {
print real(word($Data[i],1))-real(word($Data[i],3)), real(word($Data[i],2))
}
do for [i=|$Data|:1:-1] {
print real(word($Data[i],1))+real(word($Data[i],3)), real(word($Data[i],2))
}
set print
set yrange [:] reverse
set style fill noborder
plot $XErrorFill u 1:2 w filledcurves lc "light-grey" notitle, \
$Data u 1:2 w l lw 1.5 lc rgb "red" notitle
### end of code
Result:

shading plot downward with different colors

I try to plot several graphs using multiplot option in gnuplot. I use the script as shown below.
#!/usr/bin/env gnuplot
#OUTPUT
#PNG
set terminal pngcairo size 600,550 enhanced dash #font "Arial-Bold,13" #fontscale 1.20
set output "Fill-Multi-plot-LDP-lyoSystemLast50ns.png"
#############################################################################
set style line 4 lt 1 lw 2.5 lc rgb "red"
set style line 5 lt 3 lw 2.5 lc rgb "forest-green"
set style line 6 lt 5 lw 2.5 lc rgb "blue"
#############################################################################
set macro
labelFONT="font 'Arial,18'"
scaleFONT="font 'Arial-Bold,14'"
scaleFONtt="font 'Helvetica,10'"
keyFONT="font 'Arial,10'"
#############################################################################
xsize = 0.80 # change this for expand in x direction
ysize = 0.22
xorigin = 0.022
yorigin = 0.02
#############################################################################
set xrange [-25.2:25.2] noreverse nowriteback
set yrange [0:2.5] noreverse nowriteback
set xtic auto #scaleFONT # set xtics automatically
set ytic '' #0,0.2,0.4 #scaleFONT # set ytics automatically
unset key
set size 1.0,1.0
set multiplot
#############################################################################
# plot A
set ylabel ""
set label "Distance in Angstrom" at -30.0,-0.22 #labelFONT
set label "Number Density" at -58,0.70 rotate by 90 left #labelFONT
set label "(e)" at 0,0.60 #scaleFONT
set origin xorigin,yorigin
set size xsize,(ysize+0.015)
plot "bcm25perRS-251-300ns_head_tail_wat2.dat" u 1:2 w filledcurves y1=0 fs transparent solid 0.35 ls 4, \
'' u 1:3 w filledcurves fs transparent solid 0.4 ls 5,\
'' u 1:4 w filledcurves fs transparent solid 0.5 ls 6
#################################################################################
# plot B
set xrange [-25.2:25.2]
set ylabel ""
unset label
set label "(d)" at 0,0.60 #scaleFONT
set origin xorigin,(yorigin+0.19)
set size xsize,ysize+0.02
plot "bcm25perS-251-300ns_head_tail_wat2.dat" u 1:2 w filledcurves y1=0 fs transparent solid 0.35 ls 4, \
'' u 1:3 w filledcurves fs transparent solid 0.4 ls 5,\
'' u 1:4 w filledcurves fs transparent solid 0.5 ls 6
#################################################################################
## plot C
set xrange [-25.2:25.2]
set ylabel ""
unset label
set label "(c)" at 0,0.60 #scaleFONT
set origin xorigin,(yorigin+0.385)
set size xsize,ysize+0.02
plot "bcm25perR-251-300ns_head_tail_wat2.dat" u 1:2 w filledcurves y1=0 fs transparent solid 0.35 ls 4, \
'' u 1:3 w filledcurves fs transparent solid 0.4 ls 5,\
'' u 1:4 w filledcurves fs transparent solid 0.5 ls 6
#################################################################################
# plot D
set xrange [-27.3:27.3]
set xtics auto
set ylabel ""
unset label
set label "(b)" at 0,0.60 #scaleFONT
set origin xorigin,(yorigin+0.58)
set size xsize,ysize+0.02
plot "malto23per-251-300ns_head_tail_wat2.dat" u 1:2 w filledcurves y1=0 fs transparent solid 0.35 ls 4, \
'' u 1:3 w filledcurves fs transparent solid 0.4 ls 5,\
'' u 1:4 w filledcurves fs transparent solid 0.5 ls 6
#################################################################################
# plot E
set xrange [-20.0:20.0]
set xtics auto
set ylabel ""
unset label
set label "(a)" at 0,0.60 #scaleFONT
set origin xorigin,(yorigin+0.77)
set size xsize,ysize+0.02
plot "malto12per-251-300ns_head_tail_wat2.dat" u 1:2 w filledcurves y1=0 fs transparent solid 0.35 ls 4, \
'' u 1:3 w filledcurves fs transparent solid 0.4 ls 5,\
'' u 1:4 w filledcurves fs transparent solid 0.5 ls 6
#################################################################################
## plot F
set size 0.3,0.5
set origin 0.76,0.55
set bmargin at screen 0
set key center center
set border 0
unset xlabel
unset ylabel
unset label
unset tics
set format x ""
set format y ""
set yrange [0:1]
plot 2 ls 4 t 'Head', \
2 ls 5 t 'Chain', \
2 ls 6 t 'Water'
#, \
# 2 ls 10 t '151-200ns', \
# 2 ls 13 t '201-250ns', \
# 2 ls 16 t '251-300ns'
unset multiplot
Using this code I get a plot as shown here. .
The problem that I am facing here is the plot labeled (a) in the figure. The blue color shade must be downward. But it shades upward. The other figures (b), (c) and etc are correct.
The corresponding code for the figure label (a) is given in section #(PLOT E)# in the code. Especially the line for the blue shade is as follows: (u 1:4 w filledcurves fs transparent solid 0.5 ls 6).
I cannot figure out what is the mistake or error I made here.
(note: The order for the code and corresponding figures are upside down).
I appreciate any help for correcting this code. Many thanks in advance.
You want to fill the area between the curve and the lower x-axis. This is done with the option x1:
plot 'file.dat' with filledcurves x1 fs transparent solid 0.5 ls 6
I think you can set x1 for all of your areas.

gnuplot - calculate distance between lines

Can gnuplot calculate the distance between two lines or maybe two points?
I'm having a plot where two (main) lines are plotted. For the moment let's assume that the first line is always above the second one. Is there a way to calculate the distance from line 2 to line 1 at a given x-value?
here is a picture of what my plot looks like and which distance I want to calculate:
The vertical lines are just for style and have nothing to do with the actual plot, their data is stored in test.dat and test2.dat.
My data-files of the lines look like this:
line1
0 118.1
2.754 117.77
4.054 117.64
6.131 116.17
7.7 116.04
8.391 115.36
10.535 115.25
11.433 116.03
12.591 116.22
19.519 118.59
line2
19.4 118.51
15.2 116.56
10.9 115.94
10.35 114.93
9.05 114.92
8.3 115.9
5.9 116.19
4.2 116.62
2.2 117.66
-0.3 118.06
My plotting-code looks like this:
set term wxt enhanced
cd 'working directory'
unset key
set size 0.9,0.9
set origin 0.1,0.1
set title 'TITLE'
unset border
set label 21 " rotate by 45" at -3.0,0.0 rotate by 45 point ps 2
set xrange [0:19.519]
set yrange [110:119]
set xtics nomirror(0, 2.745, 4.054, 6.131, 7.7, 8.391, 10.535, 11.433, 12.591, 19.519) rotate by 90 offset 0,-0.1 right
set ytics " ", 30000
plot "line1.dat" using ($1):($2):2 with labels offset 1, 1.8 rotate by 90, "line1.dat" using 1:2 with lines lt 1 lw 1 lc rgb '#000000', +112 lt 1 lw 1 lc rgb '#000000' , 'test.dat' with lines lt 1 lw 1 lc rgb '#000000', +110 lt 1 lw 1 lc rgb '#000000', 'line2.dat' with lines lt 0.5 lw 1 lc rgb '#000000', 'test2.dat' with lines lt 0.5 lw 1 lc rgb '#000000'
You can measure the distance manually. Move the mouse to the first point and type 'r'. Then as you move the mouse around, the x and y offsets, distance and angle are displayed. Type '5' to draw a line segment and to toggle between degrees and tangent display. Zooming in beforehand increases accuracy.
By the way, typing 'h' in the plot window will display a list of keybindings to the console.
An answer to this "rather old" question still might be of interest to OP, if not, maybe to others.
Yes, you can calculate and plot the difference of two lines. It requires some linear interpolation. Simply assign the desired x-value to the variable myX.
Data:
SO17717287_1.dat
0 118.1
2.754 117.77
4.054 117.64
6.131 116.17
7.7 116.04
8.391 115.36
10.535 115.25
11.433 116.03
12.591 116.22
19.519 118.59
SO17717287_2.dat
19.4 118.51
15.2 116.56
10.9 115.94
10.35 114.93
9.05 114.92
8.3 115.9
5.9 116.19
4.2 116.62
2.2 117.66
-0.3 118.06
Script: (works for gnuplot>=4.6.0)
### calculating and plotting a difference between two curves
reset
FILE1 = "SO17717287_1.dat"
FILE2 = "SO17717287_2.dat"
set border 1
unset key
set origin 0.05,0.05
set size 0.9,0.8
set xrange [0:19.519]
set xtics nomirror rotate by 90 offset 0,-0.1 right
set yrange [110:119]
unset ytics
myX = 15.2
getYa(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? ya=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? ya=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
getYb(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? yb=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? yb=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
set samples 2 # set to minimal possible value for plotting '+'
plot x1=y1=NaN FILE1 u 1:2:2:xtic(1) w labels offset 0,0.5 left rotate by 90, \
'' u 1:(getYa(myX),$2) w l lc rgb 'black', \
'' u 1:2:(0):(110-$2) w vec lt 0 nohead, \
+112 w l lc rgb 'black', \
x1=y1=NaN FILE2 u 1:(getYb(myX),$2) w l lt 0 lc rgb 'black', \
'+' u (myX):(ya):(0):(yb-ya) w vec heads lc rgb "red", \
'+' u (myX):(ya):(sprintf("%.3f",yb-ya)):xtic(sprintf("%g",myX)) w labels tc rgb "red" offset 0,1, \
'+' u (myX):(ya):(0):(110-ya) w vec nohead lt 0 lc rgb "red"
### end of script
Result: (created with gnuplot 4.6.0)

Resources