Plotting 2 series from one data set in gnuplot - gnuplot

I have the following data file:
Time;Server;Hits
2011.05.05 12:00:01;Server1;12
2011.05.05 12:00:01;Server2;10
2011.05.05 12:00:02;Server1;2
2011.05.05 12:00:02;Server2;4
So, far I have come up with the following gnuplot script:
set datafile separator ";"
set autoscale
set xdata time
set timefmt "%Y.%m.%d %H:%M:%S"
set xtics rotate
set term png
set output "hits.png"
set style fill solid 0.5
plot "hits.log" using 1:3 title 'Hits'
But that one plots data from both servers on the same graph as one data series. How do I make gnuplot to display 2 data series: one for each server?

I found a soluton myself:
plot "hits.log" using 1:(stringcolumn(2) eq "Server1" ? $3 : 1/0) title 'Server1' with lines,\
"hits.log" using 1:(stringcolumn(2) eq "Server2" ? $3 : 1/0) title 'Server2' with lines

Related

Plotting lines with missing datapoints for multidimensional data

I'm trying to plot multiple lines representing GPU usage over time from a dataset which records data of multiple GPUs. Each row contains the timestamp, a GPU index and the usage in percent.
My dataset looks like this:
$ cat gpu.txt
#time #index # usage (%)
1,1,10
1,2,5
2,1,20
2,2,10
3,1,40
3,2,30
and this is my gnuplot script:
$ cat plot.gplot
set datafile separator ","
set autoscale # scale axes automatically
unset log # remove any log-scaling
unset label # remove any previous labels
set xtic auto # set xtics automatically
set ytic auto # set ytics automatically
set title
set term png
set title "GPU usage"
set xlabel "Time"
set ylabel "Usage"
set output "gpu.png"
plot "gpu.txt" using ($2 == 1 ? $1 : NaN):($2 == 1 ? $3 : NaN) title 'GPU1' with linespoints ls 10 linecolor rgb "blue", \
"gpu.txt" using ($2 == 2 ? $1 : NaN):($2 == 2 ? $3 : NaN) title 'GPU 2' with linespoints ls 10 linecolor rgb "red", \
Unfortunately, this only ever draws the singular datapoints, but no lines. I think this is because of "missing" datapoints - which is not the case obviously because I have the custom filters in place to plot usage data per GPU index. I tried to indicate this to gnuplot via the NaN value, but it doesn't seem to work.
Example output:
This is kind of a recurring filtering data question.
You can define linestyles and then use it in the plotting loop via ls i.
Essential if you want connecting lines is the line: set datafile missing NaN.
My minimal suggestion would be:
Code:
### filtering data
reset session
$Data <<EOD
#time #index # usage (%)
1,1,10
1,2,5
2,1,20
2,2,10
3,1,40
3,2,30
EOD
set datafile separator ","
set title "GPU usage"
set xlabel "Time"
set ylabel "Usage"
set key top left
set datafile missing NaN
myFilter(datacol,filtercol,value) = value==column(filtercol) ? column(datacol) : NaN
plot for [i=1:2] $Data u (myFilter(1,2,i)):3 w lp pt 7 title sprintf('GPU%d',i)
### end of code
Result:

System time and Gnuplot

I am constructing a normal X/Y data plot using column data from a .txt data file. The graph plots well (see general code below).
I am wondering if it is possible to extract and use a System time in conjunction with the logged data in the .txt file?
This is in order to produce an X/Y plot with Y being the data from column [x] from the .txt file and the x-axis being an extracted system time point ??
The system time is not part of the log file.
The data string contained within the text file looks like this and is ; delimited: 221.5;65.9;-22.9;0;22.1
Code to generate the plot:
set multiplot layout 5,1
set title "Plot 1"
unset key
plot "data.txt" using 1 with linespoints ls 1
set title "Plot 2"
unset key
plot "data.txt" using 2 with linespoints ls 2
......
unset multiplot
In other words, for each data .txt file column point I would like to attach a system time giving as "Plot 1" {sys.time[x],221.5} and for "Plot 2" {sys.time[x],65.9}.
The next row of file data points delimited by ; will have another system time attached to each of them.
Thanks
I am still a bit guessing, but your image made it a bit more clear.
Maybe we can use this for further tweaking.
Some random data: data.txt
0.713;0.725;0.579;0.485;0.271
0.861;0.882;0.128;0.772;0.475
0.524;0.076;0.420;0.583;0.068
0.429;0.114;0.907;0.679;0.842
0.684;0.077;0.752;0.165;0.062
0.916;0.865;0.368;0.380;0.843
0.291;0.460;0.272;0.279;0.632
0.908;0.832;0.556;0.259;0.744
0.763;0.735;0.624;0.277;0.520
0.735;0.690;0.145;0.148;0.734
0.041;0.398;0.353;0.764;0.065
0.765;0.049;0.603;0.215;0.636
0.613;0.271;0.770;0.030;0.449
0.512;0.583;0.956;0.994;0.832
0.222;0.061;0.471;0.085;0.880
0.259;0.071;0.065;0.391;0.043
0.077;0.356;0.377;0.590;0.562
0.706;0.343;0.312;0.993;0.659
0.826;0.103;0.908;0.135;0.048
0.393;0.012;0.192;0.017;0.233
The code:
### adding time data afterwards
reset session
FILE = "data.txt"
set datafile separator ";"
set xdata time
set timefmt "%d.%m.%Y %H:%M"
set format x "%d.%m."
TimeIntervalInSeconds = 3600*24
StartTime = "01.01.2019 12:00"
set ytics 0.5
set multiplot layout 5,1
do for [i=1:5] {
set title sprintf("Plot %d",i)
plot FILE using (StartTime+$0*TimeIntervalInSeconds):i w lp lt i notitle
}
unset multiplot
### end of code
Result:

GnuPlot: stacked histogram causes hovering bars

since two days I am trying to solve this problem. The bars of this stacked histogram are not printed above each other. They are floating freely around.
Secondly, I only want to print any 5th xtic-label. I am using GnuPlot v 4.6 patchlevel 6.hovering bars in stacked bargraph
Here are the first data rows (generated with libreoffice):
05.06,-,-,1
06.06,3,-,0
07.06,12,-,3
08.06,0,5,4
09.06,7,2,0
10.06,86,2,1
11.06,31,4,1
12.06,17,1,0
01.07,1,7,1
Here comes the command set:
gnuplot> set datafile separator ','
gnuplot> set style data histogram
gnuplot> set style histogram rowstacked
gnuplot> set style fill solid border -1
gnuplot> set xlabel "Zeit"
gnuplot> set ylabel "Anzahl"
gnuplot> set yrange [0:250]
gnuplot> plot 'test.csv' using 2:xtic(1) title "Menge A",''
gnuplot> using 3:xtic(1) title "Menge B",''
gnuplot> using 4:xtic(1) title "Menge C"
Gnuplot seems to get confused with - as only column content. Also a set datafile missing '-' doesn't help. You need a datafile with really empty fields, like
05.06,,,1
06.06,3,,0
07.06,12,,3
If you cannot get LibreOffice to save the data file properly you can use e.g. sed to process the file on-the-fly:
plot "< sed 's/-//g' test.csv" using 2:xtic(1), '' ...
(This works properly if you don't have negative values, which I suppose is the case).
To the second part: Instead of xtic(1) you can also put any expression which evaluates to a string inside of xtic, like
xtic(int($0)%5 == 0 ? strcol(1) : '')
This uses the string in the first column as xticlabel if the row number is a multiple of 5, otherwise an empty string:
set datafile separator ','
set style data histogram
set style histogram rowstacked
set style fill solid border -1
set xlabel "Zeit"
set ylabel "Anzahl"
set yrange [0:*]
plot '< sed "s/-//g" test.csv' using 2:xtic(int($0)%5 == 1 ? strcol(1) : '') title "Menge A",\
'' using 3 title "Menge B",\
'' using 4 title "Menge C"
As Christoph has already explained, the problem is caused by the - in your input data.
Therefore, the best and cleanest solution is to make LibreOffice display missing data differently.
However, everything worked fine for me when I mask the using COLUMNNUMBER part by using $COLUMNNUMBER. Hence, I changed the last line of your code to
plot 'test.csv' u ($2):xtic(1) t "Menge A", '' u ($3) t "Menge B", \
'' u ($4) t "Menge C"
As you see, you can shorten using to u and title to t. Moreover, you should use :xtic(1) only for the first data set.
Here is my outoput

Unexpected output GNUPlot

I am drawing a simple graph using GNUPlot but output is not what I expected order.
Here is my script :
set title 'cost function vs clusters'
set xlabel '#clusters'
set ylabel 'cost function'
set terminal postscript
set output '| ps2pdf - output.pdf'
plot filename using 1:2 title "x" with linesp
Data on which I am plotting the data is :
13 0.004945370902817711
8 0.06739505462909719
2 0.28378378378378377
17 0.004657849338700402
5 0.015181138585393904
20 0.0018401380103507763
And here is my ouput :
I want points to be joined in sequential order of x.
How I can achieve this?
For the data you showed, you can use smooth unique. This sorts the data and replaces the same x-values with a single point having the averaged y value. If you can be sure, that you'll never have two equal x-values, then you can use this:
set title 'cost function vs clusters'
set xlabel '#clusters'
set ylabel 'cost function'
set terminal pdfcairo
set output 'output.pdf'
plot filename using 1:2 smooth unique title "x" with lp
And call it with gnuplot -e 'filename="aboveFile"' plot.gpi.
The other variant using sort also works fine:
plot '< sort -n '.filename using 1:2 title "x" with lp

Gnuplot, setting yrange days back

I have put up a raspberry pi to measure temperature and call a Gnuplot script to place a graph on a webpage.
Now i want to make a couple of graphs that display 1hour backwards in time/1day backwards in time.
Does anyone know how i specify the X-range to start at "current time - 1 day" or "current time - 1 hour"?
Thanx!
This won't work everywhere, but if your gnuplot supports pipes and your system has the date command ...
TIMEFMT = "%Y:%m:%d:%H:%M:%S"
#now = "`date +%Y:%m:%d:%H:%M:%S`" #Use this line in production
now = '2013:01:24:20:49:30' #Hard-code this for the sake of the example ...
now_secs = strptime(TIMEFMT,now)
one_hour_past = now_secs - 3600.0
set xdata time
#set timefmt TIMEFMT #This doesn't parse correctly ... Not sure why...
eval(sprintf('set timefmt "%s"',TIMEFMT))
print strftime(TIMEFMT,one_hour_past)
#set xrange [strftime(TIMEFMT,one_hour_past):] #This doesn't seem to work
#set xrange ["2013:01:24:20:49:30":] #This works, but is declared statically -- Yuck.
eval(sprintf('set xrange ["%s":]',strftime(TIMEFMT,one_hour_past)))
plot '-' u 1:2 w l
2013:01:24:10:00:00 2.5
2013:01:24:21:00:00 2
2013:01:24:22:00:00 3
e
THe code i have used is as follows. Im running it on a Raspberry Pi with Rasbian OS. Any sudgestions? Thanx!
#!/usr/bin/gnuplot
reset
set terminal png size 1250,700
set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#E6E6FA" behind
set output '/var/www/bild.png'
set multiplot
set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"
set format x "%H:%M\n%d/%m"
set xlabel "Timme/datum"
set ylabel "Inomhustemperatur"
set yrange [15:28]
set y2label "Utomhustemperatur"
set y2range [-20:10]
set y2tics nomirror
set y2tics
set title "Temperatur"
set key reverse Left outside
set grid
set style data lines
plot "logg.txt" using 1:3 axes x1y1 lw 3 title "inomhus", "" using 1:4 axes x1y2 lw 3 title "utomhus"#

Resources