I am learning how to plot pdf in gnuplot. The code is sourced from
http://gnuplot.sourceforge.net/demo/random.html
the code is
unset contour
unset parametric
load "stat.inc"
print ""
print "Simple Monte Carlo simulation"
print ""
print "The first curve is a histogram where the binned frequency of occurence"
print "of a pseudo random variable distributed according to the normal"
print "(Gaussian) law is scaled such that the histogram converges to the"
print "normal probability density function with increasing number of samples"
print "used in the Monte Carlo simulation. The second curve is the normal"
print "probability density function with unit variance and zero mean."
print ""
nsamp = 5000
binwidth = 20
xlow = -3.0
xhigh = 3.0
scale = (binwidth/(xhigh-xlow))
# Generate N random data points.
set print "random.tmp"
do for [i=1:nsamp] {
print sprintf("%8.5g %8.5g", invnorm(rand(0)), (1.0*scale/nsamp))
}
unset print
#
set samples 200
tstring(n) = sprintf("Histogram of %d random samples from a univariate\nGaussian PDF with unit variance and zero mean", n)
set title tstring(nsamp)
set key
set grid
set terminal png
set output "x.png"
set xrange [-3:3]
set yrange [0:0.45]
bin(x) = (1.0/scale)*floor(x*scale)
plot "random.tmp" using (bin($1)):2 smooth frequency with steps title "scaled bin frequency", normal(x,0,1) with lines title "Gaussian p.d.f."
I get the error
Cannot open load file 'stat.inc'
"stat.inc", line 4: util.c: No such file or directory
The version of gnuplot I am using is gnuplot 4.6 patchlevel 0.
Please help as I am unable to locate this particular file or run the code on ubuntu platform.
you find all that file in the source release of gnuplot.
You can download it from sourceforge
you'll find the stat.inc in the demo directory
Related
I have a set of files that contain data that I want to produce a set of box plots for in order to compare them. I can get the data into gnuplot, but I don't know the correct format to separate each file into its own plot.
I have tried reading all the required files into a variable, which does work, however when the plot is produced, all the boxplots are on top of each other. I need to get gnuplot to index each plot along one space for each new data file.
For example, this produces the output with overlaying plots:
FILES = system("ls -1 /path/to/files/*")
plot for [data in FILES] data using (1):($4) with boxplot notitle
I know the X position is being stated explicitly there with the (1), but I'm not sure what to replace it with to get the position to move for each plot. This isn't a problem with other chart types, since they don't have the same field locating them.
You can try the following.
You can access the file in your file list by index via word(FILES,i). Check help word and help words. The code below assumes that you have some datafiles Data0*.dat in your directory. Maybe there is a smarter/shorter way to implement the xtic labels.
Code:
### boxplots from a list of files
reset session
# get a list of files (Windows)
FILES = system('dir /B "C:\Data\Data0*.dat"')
# set tics as filenames
set xtics () # remove xtics
set yrange [-2:27]
do for [i=1:words(FILES)] {
set xtics add (word(FILES,i) i) rotate by 45 right
}
plot for [i=1:words(FILES)] word(FILES,i) u (i):2 w boxplot notitle
### end of code
Result:
I use gnuplot with epslatex option to generate figure files for plotting purposes (like here). Via this method you get 2 files corresponding to same figure, one tex file and one eps file. The figure information is in eps file and font information is in tex file. So my question is this :
Can I combine both font information and figure content to a single file like pdf / eps file ?
UPDATE : OK I forgot to mention one thing. Off course set terminal postscript eps will give me eps outputs, but it will not embed latex symbols in the plot as labels etc.
So I found a method which I got from Christoph's comment. Set terminal like set terminal epslatex 8 standalone and then finally after plotting do something like below:
set terminal epslatex color standalone
set output "file.tex"
set xrange [1:500]
set ylabel "Variance (\\AA\\textsuperscript{2})" # angstoms
set mxtics 4
plot "version1.dat" using 1:3 with linespoints pointinterval -5 pt 10 lt 1 lw 3 title 'label1' , \
"version1.dat" using 1:2 with linespoints pointinterval -5 pt 6 lt -1 lw 3 title 'label2';
unset output
# And now the important part (combine info to single file) :
set output # finish the current output file
system('latex file.tex && dvips file.dvi && ps2pdf file.ps')
system('mv file.ps file.eps')
unset terminal
reset
These steps do output tex file which is converted to dvi and ps file. And finally you rename the postscript file to eps. Now you have figure information and tex symbol information in single file. This eps file is accepted by latex files.
OK now why this works : Sorry I don't know the entire technical details. But this is working fine with me.
I've been trying to graph several files on the same gnuplot graph using sprintf to read in the filenames. I can read in one argument o.k. if I write:
filename(n) = sprintf("oi04_saxs_%05d_ave_div_sub.dat", n)
plot for [i=1:10] filename(i) u 1:2
then my graph is o.k and I get all files with that argument plotted on the same graph. However I have a string of characters that changes near the end of my filename and when I try to reflect this in
filename(n,m) = sprintf("oi04_saxs_%05d_0001_%04s_ave_div_sub.dat",n,m)
plot for [i=1:10] filename(i,m) u 1:2
I get the following error message: 'undefined variable m'. I've tried removing the loop and just running
plot for filename(m)
and this results in the same error message. Help in understanding what's going wrong and how to fix it would be much appreciated :)
This is my full script:
unset multiplot
reset
set termoption enhanced
set encoding utf8
set term pdf size 18cm,18cm font 'Arial'
set pointsize 0.25
set output 'StoppedFlowResults.pdf'
set logscale
set xlabel '{/:Italic r} / [Q]'
set ylabel '{/:Italic Intensity}'
filename(n) = sprintf("./Result_curve-%d.txt/estimate.d", n)
myColorGradient(n) = sprintf("#%02x00%02x", 256-(n-1)*8-1, (n-1)*8)
set key off
set multiplot layout 2,1
filename(n,m) = sprintf("oi04_saxs_%05d_0001_%04s_ave_div_sub.dat",n,m);
plot for [i=1:10] filename(i,m) u 1:2 not
unset multiplot
set output
based on help for, you can have nested iterations e.g.:
plot for [i=1:3] for [j=1:3] sin(x*i)+cos(x*j)
In your case you can mix this with strings (you have yet to define string possible values) with something like:
plot for [i=1:3] for [m in "A B C D"] filename(i,m) u 1:2 not
Gnuplot reads weather data from a huge file called file.dat and plots the weather data for a given date and time.
But if there is no data for the given date and time (xrange), gnuplot crashes.
How can I tell gnuplot, if there is no data for a given date and time, display a text in the output image?
("There is no data available, I am sorry")
The error, if there is no data available:
line 0: all points y2 value undefined!
The script.dem file, which is loaded by gnuplot:
reset
#SET TERMINAL
set term svg
set output 'temp-verlauf.svg'
set title "Temperaturverlauf"
#Axes label
set xlabel "Messzeitpunkt"
set ylabel "Luftfeuchte/Temperatur"
set y2label "Luftdruck"
#Axis setup
set xdata time # x-Achse wird im Datums/Zeitformat skaliert
set timefmt "%d.%m.%Y\t%H:%M:%S" # Format Zeitangaben yyyy.mm.dd_hh:mm:ss
set format x "%H:%M" # Format für die Achsenbeschriftung
#Axis ranges
set yrange [0:60] # die y-Achse geht von:bis
#Tics
set ytics nomirror
set y2tics nomirror
#OTHER
set datafile separator "\t"
set xrange ["06.11.2014 14:00:00":"07.11.2014 21:00:00"]
plot \
"file.dat" every 10 using 1:5 title "Luftfeuchte" with lines, \
"file.dat" every 10 using 1:6 title "Temperatur" with lines, \
"file.dat" every 10 using 1:7 title "Luftdruck" with lines axes x1y2, \
"file.dat" every 10 using 1:17 title "Niederschlagsintensitaet Synop (4677)" with lines
EDIT
Thanks to user "bibi".
He had the good idea to let gnuplot plot -1 to have data if there is nothing avaible in file.dat.
The script will look like that:
reset
#SET TERMINAL
set term svg
set output 'temp-verlauf.svg'
set title "Temperaturverlauf"
#Axes label
set xlabel "Messzeitpunkt"
set ylabel "Luftfeuchte/Temperatur"
set y2label "Luftdruck"
#Axis setup
set xdata time # x-Achse wird im Datums/Zeitformat skaliert
set timefmt "%d.%m.%Y\t%H:%M:%S" # Format Zeitangaben yyyy.mm.dd_hh:mm:ss
set format x "%H:%M" # Format für die Achsenbeschriftung
#Axis ranges
set yrange [0:60] # die y-Achse geht von:bis
#Tics
set ytics nomirror
set y2tics nomirror
#OTHER
set datafile separator "\t"
set xrange ["06.11.2014 14:00:00":"07.11.2014 21:00:00"]
plot \
-1 axes x1y2, \
-1 axes x1y1, \
"file.dat" every 10 using 1:5 title "Luftfeuchte" with lines, \
"file.dat" every 10 using 1:6 title "Temperatur" with lines, \
"file.dat" every 10 using 1:7 title "Luftdruck" with lines axes x1y2, \
"file.dat" every 10 using 1:17 title "Niederschlagsintensitaet Synop (4677)" with lines
The simplest solution it came to my mind is to draw an horizontal line outside the plotting region (-1 is ok since you have set yrange [0:60]):
plot \
-1, \
"file.dat" every 10 using 1:5 title "Luftfeuchte" with lines, \
"file.dat" every 10 using 1:6 title "Temperatur" with lines, \
"file.dat" every 10 using 1:7 title "Luftdruck" with lines axes x1y2, \
"file.dat" every 10 using 1:17 title "Niederschlagsintensitaet Synop (4677)" with lines
Moreover the gnuplot internal variable GPVAL_ERRNO will be non-zero if something weird happened, you might check that and print a banner on screen.
Your data in your file is double. So after plotting once it jumps back to the start and plots everything another time on top of the first plot.
Took me a couple of hours to figure out. :)
I'm trying to plot a histogram for the following data:
<text>,<percentage>
--------------------
"Statement A",50%
"Statement B",20%
"Statement C",30%
I used the set datafile separator "," to obtain the corresponding columns. The plot should have percentage on the X-axis and the statements on the Y-axis (full character string). So each histogram is horizontal.
How can I do this in gnuplot?
Or is there other tools for plotting good vector images?
The gnuplot histogram and boxes plotting styles are for vertical boxes. To get horizontal boxes, you can use boxxyerrorbars.
For the strings as y-labels, I use yticlabels and place the boxes at the y-values 0, 1 and 2 (according to the row in the data file, which is accessed with $0).
I let gnuplot treat the second column as numerical value, which strips the % off. It is added later in the formatting of the xtics:
set datafile separator ','
set format x '%g%%'
set style fill solid
plot 'data.txt' using ($2*0.5):0:($2*0.5):(0.4):yticlabels(1) with boxxyerrorbars t ''
The result with version 4.6.4 is:
#Christoph Thank you. Your answer helped me.
#Slayer Regarding your question to add labels using gnuplot v5.2 patchlevel 6 and using #Christoph's provided sample.
Sample Code:
# set the data file delimiter
set datafile separator ','
# set the x-axiz labels to show percentage
set format x '%g%%'
# set the x-axis min and max range
set xrange [ 0 : 100]
# set the style of the bars
set style fill solid
# set the textbox style with a blue line colour
set style textbox opaque border lc "blue"
# plot the data graph and place the labels on the bars
plot 'plotv.txt' using ($2*0.5):0:($2*0.5):(0.3):yticlabels(1) with boxxyerrorbars t '', \
'' using 2:0:2 with labels center boxed notitle column
Sample Data Provided:(plotv.txt)
<text>,<percentage>
--------------------
"Statement A",50%
"Statement B",20%
"Statement C",30%
Reference(s):
gnuplot 5.2 demo sample - textbox and the related sample data
gnuplot