Unexpected behaviour with negative fraction of seconds in xdata time - gnuplot

Can anybody explain the strange behaviour of gnuplot with negative fractions of seconds when plotting with xdata time? I mean the zig-zag in the middle plot. Is this a bug or am I missing anything? (gnuplot 5.2.6)
Code:
### strange behaviour with negative fractions of seconds in xdata time
reset session
set table $Data
plot '+' u 1:($1**2) w table
unset table
set multiplot layout 3,1
plot $Data u 1:2 w lp pt 7 lc rgb "web-green"
set xdata time
set timefmt "%s"
set format x "%H:%M:%S"
plot $Data u 1:2 w lp pt 7 lc rgb "red"
set format x "%H:%M"
plot $Data u ($1*60):2 w lp pt 7 lc rgb "web-blue"
unset multiplot
### end of code
Result:

You are over-complicating this. Your data is purely in seconds. No need to invoke "set xdata time" or "set timefmt" variants because the input is just a floating point number. On output, set the xtic format depending on whether you want the x axis labeled in absolute time (wraps at midnight) or relative time (+/- interval).
### absolute vs relative time formats
set table $Data
plot '+' u 1:($1**2) w table
unset table
set multiplot layout 3,1
plot $Data u 1:2 w lp pt 7 lc rgb "web-green"
set xtics time format "%tH:%tM:%tS" # relative time
plot $Data u 1:2 w lp pt 7 lc rgb "red"
set xtics time format "%H:%M:%S" # absolute time
plot $Data u 1:2 w lp pt 7 lc rgb "blue"
unset multiplot
### end of code
But no, sorry, I cannot explain why your original plot came out with a zigzag.

Related

How can I keep previous plot in gnuplot

Need to mark with circle on graph, but it doesn't overlap
#set multiplot layout 2, 1 # engage multiplot mode
#set multiplot
set font "arial,12"
set autoscale
set datafile separator comma
#set offset 0,0,.5,.5
#set autoscale fix
set xtics out nomirror
set ytics out nomirror
unset border
set border 3
set format y "%0.04s %cV "
set format x
#set object circle at 5.2055,3430 size 25.5 lw 5 fc rgb "red"
$data <<EOD
3400,5.2055
EOD
plot [3200:4400] "shurb/foo.csv" u 1:2 w l lc rgb 'dark-green' title 'AP',\
$data using 1:2 with circle lw 1 lc rgb 'red' notitle' ,
#unset multiplot
#set output
It is only drawing second one or first one, so I have to merge them on same plot.
How can I mark with circle or merge these two plots? Why I couldn't overlap them.
Maybe I cannot fully follow what you are trying to do. How do you get two plot with a single plot command?
There is no need to use multiplot. Simply plot your datafile and your datablock $data with a single datapoint.
By the way, your commented #set object circle at 5.2055,3430 size 25.5 lw 5 fc rgb "red" has swapped x-, and y-coordinates. So, this circle object would not be visible on your plot.
Script: (works with gnuplot>=5.2.2)
### plot with circles
reset session
# create some test data
set table $Test separator comma
plot [3200:4400] '+' u 1:(sin($0/6)*cos($0/20)/2.8*0.005+5.205) w table
unset table
$data <<EOD
3400, 5.2055
EOD
set datafile separator comma
set format y "%.4f mV"
set key noautotitle
plot [3200:4400] $Test u 1:2 w l lc "dark-green", \
$data u 1:2 w circle lw 1 lc "red"
### end of script
Result:

Plot a forecast line with Gnuplot?

I've this data :
Serv1;2019-10;2561.36
Serv1;2019-11;3292.65
Serv1;2019-12;3077.58
Serv1;2020-01;3369.98
Serv1;2020-02;3134.53
Serv1;2020-03;593.332
With excel, I'm able to create an graph with a forecast line on excel like that :
I'm able to create a graph with gnuplot :
With this gnuplot script :
set title "test"
set terminal png truecolor size 960,720 background rgb "#eff1f0"
set output "/xxx/xxx/xxx/xxx/xxx/test.png"
set grid
set style line 1 \
linecolor rgb '#0060ad' \
linetype 1 linewidth 2 \
pointtype 7 pointsize 1.5
set offsets 0.5,0.5,0,0.5
set datafile separator ";"
set key left
plot "test.txt" using 3:xtic(2) with linespoints linestyle 1
But I don't know how to plot a forecast line with Gnuplot...
Could you show me how to do that ?
Assuming you are looking for a linear fit and extending this linear function, you can try the following below.
Edit:
There is no gnuplot function to get the data value of a certain row and column, e.g. like a = value(row,column). You have to use a somehow strange workaround. Basically, you plot your data into a dummy table, but only the first datapoint of the first block of the first dataset (counting starts with 0). Check help every and help index.
set table $Dummy
plot $Data u (StartDate=timecolumn(1,myTimeFmt)) index 0 every ::0:0:0:0 w table
unset table
print sprintf("StartDate: %s",strftime(myTimeFmt,StartDate))
Result: StartDate: 01/03/2020
Code:
### linear fit and extrapolation
reset session
$Data <<EOD
01/03/2020,100
02/03/2020,150
03/03/2020,125
04/03/2020,150
05/03/2020,175
06/03/2020,200
07/03/2020,220
08/03/2020,150
09/03/2020,175
10/03/2020,125
11/03/2020,150
12/03/2020,200
13/03/2020,210
14/03/2020,230
EOD
set datafile separator comma
myTimeFmt = "%d/%m/%Y"
set format x "%d.%m." time
# put start date into variable StartDate
set table $Dummy
plot $Data u (StartDate=timecolumn(1,myTimeFmt)) index 0 every ::0:0:0:0 w table
unset table
EndDate = strptime("%Y-%m","30/04/2020")
f(x) = a*(x-StartDate)+ b
set fit quiet nolog
fit f(x) $Data u (timecolumn(1,myTimeFmt)):2 via a,b
set xrange[StartDate:EndDate]
set grid xtics, ytics
plot $Data u (timecolumn(1,myTimeFmt)):2 w lp pt 7 lc rgb "red" notitle, \
[StartDate:EndDate] f(x) ti "linear fit with extrapolation"
### end of code
Result:
Edit 2: (version for gnuplot 4.6)
Modified for gnuplot 4.6. Where I got problems and found out later is the parameter FIT_LIMIT = 1e-8 which you need to set for fitting timedata.
Data: (Data.dat)
Serv1;2019-10;2561.36
Serv1;2019-11;3292.65
Serv1;2019-12;3077.58
Serv1;2020-01;3369.98
Serv1;2020-02;3134.53
Serv1;2020-03;593.332
Code:
### linear fit and extrapolation, version for gnuplot 4.6
reset
FILE = "Data.dat"
set datafile separator ";"
set xdata time
set timefmt "%Y-%m"
set format x "%Y\n%m"
# put start date into variable StartDate, dummy plot
plot FILE u (StartDate=timecolumn(2)):0 index 0 every ::0:0:0:0
EndDate = strptime(myTimeFmt,"2020-09")
f(x) = a*(x-StartDate) + b
FIT_LIMIT = 1e-8
fit f(x) FILE u (timecolumn(2)):3 via a,b
set xrange[StartDate:EndDate]
set grid xtics, ytics
set yrange[0:4000]
plot FILE u (timecolumn(2)):3 w lp pt 7 lc rgb "red" notitle, \
f(x) ti "linear fit with extrapolation"
### end of code
Result:

Gnuplot - User - Error: "all points y value undefined!"

Here is my program. I am trying to plot data whose format looks fine to me. Gnuplot refuses to plot the data, saying that "y-values are undefined". What am I doing wrong?
set output "snowData.png"
set title "Data from Weather Station Yard"
set xdata time
set timefmt "%H:%M"
set format x "%H"
set xrange [00:24]
set xtics
set xlabel "Time(hrs)"
set ylabel "Water Content(inches)" tc lt 1
set y2label "Temperature Deg F" tc lt 2
set grid
#set yrange [0:2]
#set y2range [-20:80]
#set autoscale y2
set ytics 1 nomirror tc lt 1
set y2tics 10 nomirror tc lt 2 #MUST set y2tics to view y2label
plot "logSnow.dat" using 1:2 title "Water Content(inches)" with linespoints linetype 1,
\"logSnow.dat" using 1:3 title "Temperature" with linespoints linetype 2
Here is my data:
2019:02:20:10:55 -0.01,70.14,3.90
2019:02:20:11:26 -0.01,70.59,3.90
2019:02:20:11:57 -0.01,70.36,3.90
two issues:
your data is comma separated. So, you are missing a line
set datafile separator ","
the xrange expects a time string in the format "%H:%M"
set xrange ["00:00":"24:00"]
with this, it should work.
The following code gives the graph below (gnuplot 5.2.5):
### plotting time data
reset session
$Data <<EOD
2019:02:20:10:55,-0.01,70.14,3.90
2019:02:20:11:26,-0.01,70.59,3.90
2019:02:20:11:57,-0.01,70.36,3.90
EOD
set datafile separator ","
set xdata time
set timefmt "%Y:%m:%d:%H:%M"
set format x "%H"
set xrange ["2019:02:20:00:00":"2019:02:20:24:00"]
plot $Data u 1:2 w lp lt 1 ti "Water Content(inches)" ,\
$Data u 1:3 w lp lt 2 ti "Temperature"
### end of code

Fitting a normalized histogram using gnuplot

I have a datafile containing N random numbers generated from a C-code. Now I want to normalize the histogram from this datafile and, then, fit it to a given distribution function. How can I do that?
This is my gnuplot code for the histogram plot:
width = 5000
hist(x,width)=width*floor(x/width)+width/2.0
set boxwidth width
set style fill solid 0.5
set xrange [0:500000]
set yrange [0:20]
plot "out.dat" u (hist($1,width)):(1.0) smooth freq w boxes lc rgb"green"
Since gnuplot version 5.2 there is an new smoothing type smooth fnormal which does exactly that: sum up all values with same x-value and normalize the data so that the overall sum is 1.
A simple example:
set boxwidth 0.9
set style fill solid 0.5
set yrange [0:*]
$data <<EOD
1
1
2
2
2
3
3
EOD
set style data boxes
plot $data u 1:(1) smooth freq title 'smooth frequency',\
'' u 1:(1) smooth fnormal title 'smooth fnormal'
Applied to you example you must only update the actual plotting line to
plot "out.dat" u (hist($1,width)):(1.0/(sum)) smooth fnormal w boxes lc rgb "green"

Unique key and same size of graphs using multiplots [duplicate]

This question already has answers here:
Place key below multiplot graph in gnuplot
(2 answers)
Closed 8 years ago.
I want to put this two graphs together and I would like to use only one key. If I set one with notitle I could get them with only key, however the shape of the graphs will change.
set term postscript eps
set output "temp.eps"
set multiplot layout 1,2
set xtics ("32" 0, "128" 2, "512" 4, "2048" 6, "8192" 8)
#set grid ytics
set xrange [0:8]
set yrange [0:100]
p "8" u ($0):($6) w lp ps 0.75 notitle, "10" u ($0):($6) w lp lc rgb "#228B22" ps 0.75 notitle, "12" u ($0):($6)w lp lc rgb "black" ps 0.75 notitle , "14" u ($0):($6)w lp lc rgb "blue" ps 0.75 notitle, "16" u ($0):($6) w lp lc rgb "#D2691E" ps 0.75 notitle, "18" u ($0):($6)w lp lc rgb "#8A2BE2" ps 0.75 notitle, "20" u ($0):($6) w lp lc rgb "#20B2AA" ps 0.75 notitle
set xtics ("32" 0, "128" 2, "512" 4, "2048" 6, "8192" 8)
#set grid ytics
set xrange [0:8]
set yrange [0:100]
set xlabel "nel"
#set key location
set key invert
set key reverse
set key center right
set key outside
p "8" u ($0):($6) w lp ps 0.75 title "8", "10" u ($0):($6) w lp lc rgb "#228B22" ps 0.75 title "10", "12" u ($0):($6)w lp lc rgb "black" ps 0.75 title "12", "14" u ($0):($6)w lp lc rgb "blue" ps 0.75 title "14", "16" u ($0):($6) w lp lc rgb "#D2691E" ps 0.75 title "16", "18" u ($0):($6)w lp lc rgb "#8A2BE2" ps 0.75 title "18", "20" u ($0):($6) w lp lc rgb "#20B2AA" ps 0.75 title "20"
unset multiplot
This would produce something like:
As it can be seen, the shape of one graph would change due to the legend on its right. I looking for a way of using only legend and both figures using the shape of the graph on the left.
This question has two parts which I will address separately.
1) Can I use the same key for multiple plots?
No, unless the key would happen to be the same for the data in both plots anyway. For example, two plots with two lines each where the two lines would have the titles 'one' and 'two' in both plots could share the same key; two plots with one line each with the titles 'one' and 'two' in the separate plots could not share the same key.
2) How do I get my multiplots to be the same size when I have a key?
There is no easy way to do this, either you manually adjust the sizes of the plots, or you set up functions like in this answer.
In your case, it could look something like:
#!/usr/bin/env gnuplot
### n: change this parameter to equal the number of data sets to be plotted
n = 2
# l: left margin in pixels
l = 75.0
# k: key height in pixels (right margin)
k = 150.0
# m: margin between plots
m = 40.0
# p: plot width
p = 300.0
# w: width of output in pixels
w = p*n + m*(n-1) + l + k
### functions to help set top/bottom margins
lft(i,n,w,l,k) = (l+(w-l-k)*(i-1)/n)/w
rgt(i,n,w,l,k) = (l+(w-l-k)*i/n - m)/w
### first set up some basic plot parameters
set term pngcairo enhanced size w,600
set output 'multikey.png'
set ylabel 'Y Axis'
set xlabel 'X Axis'
set multiplot layout 1,(n+1) title 'Main title'
### First plot
# change only plot command here
currentplot = 1
set lmargin at screen lft(currentplot,n,w,l,k)
set rmargin at screen rgt(currentplot,n,w,l,k)
unset key
plot sin(1*x) notitle, \
sin(2*x) notitle
unset ylabel
### Middle data plot (commented out for this example)
# copy and paste this code to make more middle plots
#currentplot = currentplot + 1
#set lmargin at screen lft(currentplot,n,w,l,k)
#set rmargin at screen rgt(currentplot,n,w,l,k)
#unset title
#replot
### Last data plot
# change only plot command here
currentplot = currentplot + 1
set lmargin at screen lft(currentplot,n,w,l,k)
set rmargin at screen rgt(currentplot,n,w,l,k)
set xtics
replot
### Key plot
set lmargin at screen rgt(n,n,w,l,k)
set rmargin at screen 1
set key center center
set border 0
unset tics
unset xlabel
unset ylabel
set yrange [0:1]
plot 2 t 'Line 1', \
2 t 'Line 2'
unset multiplot
The result looks like this:
Note that I just provided example code. You might need to change it to get a proper plot.
You can set the size of the individual plots in multiplot using the following example: (It is an example , you might have to work with to get a correct size for your plots.)
set size 0.8,1.2
Try setting it for each of the individual plots.
You can set the overall size by
set terminal postscript enhanced eps 24 color size 10cm,15cm

Resources