I have a dataset with timestamps and values. The timestamp has 6 digit milli seconds. I tried to use this set format x "%H:%M:%.6S" with gnuplot. However, gnuplot cannot recognize this and just groups the values together for each second in the plot:
set format x "%H:%M:%.6S"
The dataset looks like this:
16:28:11.690076 1
16:28:11.690198 12
16:28:11.710519 14
16:28:11.730769 53
16:28:11.770280 18
16:28:11.791748 12
16:28:11.893583 15
The first column is the timestamp and the second column is the value. I want to plot the time on the x axis and the value on the y axis.
Now, gnuplot only gives me a plot up to seconds, but not millisecond. Is there something I should have set?
To read in time data you must use set timefmt ... to specify the format to use. Here, you must not specify the number of digits to use for the seconds, using %S reads the milliseconds as you specified them.
For the output you can set the number of digits:
set timefmt '%H:%M:%S'
set xdata time
set format x '%H:%M:%.3S'
plot 'test.txt' using 1:2 w lp pt 7 notitle
Related
If I want to calculate the correlation coefficient for the two variables "Duration" and "% played". Am I allowed to use df.groupby("Duration").agg({"played": "mean"}) and then calculate the correlation coefficient? Or do I have to take the entire dataset as is without groupby?
Duration
% played
6
50%
12
48.5%
6
42.5%
8
41.5%
12
48.5%
I am having hard time applying conditional plotting to data with timefmt abscissa using gnuplot 5.0 patchlevel 6.
I am trying to plot the content of an ASCII file consisting in two columns:
2016-12-01 12
2017-01-01 1
2017-02-01 2
2017-03-01 3
2017-04-01 4
2017-05-01 5
2017-06-01 6
so I just issue:
set timefmt "%Y-%m-%d"
set xdata time
p 'file.dat' u 1:2 w l, '' u 1:($1>strptime("%Y-%m-%d","2017-03-01")?$2:10) w p
I expect the plot to look like a line following the second column and a series of dots, following the line for the last three abscissas or marking the value 10 at the previous ones.
Actually, all the points are at 10. Do you have any clue about what is happening? Many thanks in advance.
Use timecolumn.
p 'file.dat' u 1:2 w l, '' u 1:(timecolumn(1, "%Y-%m-%d")>strptime("%Y-%m-%d", "2017-03-01") ? column(2) : (10)) w p
i have a simple dat file with 36 values in a range from 0 to 100.
Is it possible to use this 36 values as y-lables instead of the automatically generated labels? This 36 y-values are sorted from 0 to 100, but have a irregular gap.
Use the yticlabel function:
plot 'file.txt' using 1:2:yticlabel(2)
I'm trying to plot a graph that has two-line labels on its xticks. I cannot reduce the font anymore because it becomes unreadable (the plot is part of a 2x2 multiplot).
The problem is that the separation between the two lines in a label is too big, so that they become too close to the labels of the next columns.
I would like to reduce the interline spacing so that the lines of a label become closer between them, but further to the lines of the other labels.
I've thought about placing the labels manually (using a function to compute the position of each line), but before doing it I would like to know if someone has a simpler solution.
Thanks a lot!
I'm using "gnuplot 4.6 patchlevel 4" on Ubuntu 14. The folowing is a MWE:
# Requires gnuplot >= 4.6
set terminal pdf color solid font "Helvetica, 10" enhanced size 4, 3.72
set output 'mwe.pdf'
set border 3 lc rgb "#000000"
set bmargin 9
set ylabel offset -1
TicksFont = ", 10"
Title2Font = ", 14"
set yrange[0:180 < * ]
set format y "%.0f%%"
set xtics out scale 0, 0 nomirror rotate by 90 right offset 0,0 font TicksFont
set ytics out nomirror font TicksFont
unset key
set datafile separator ";"
set style fill transparent solid 1.0 border -1
set style data boxes
set boxwidth 1.0
#Bottom-left plot
set title "C) Third (sub)plot" font Title2Font
plot '-' using ($0):2:(0xFF8080):xtic(1) notitle lc rgb variable
# Label; Value
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. Ccccc: Ccccccc(Ccc),\nCccccccc(CCC); 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
;NaN
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. A single-liner; 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
;NaN
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. A single-liner; 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
05. Eeee: EEEE,\nEEEEEEEEEEEE(EE); 50
end;
From gnuplot you cannot directly control the line height used for the labels. But there is a quite dirty workaround for your problem:
You can split your label at the new line character, change the font size of each line with the enhanced label syntax, but set a different font size for the new line character. So a label
set label at 0,0 "first line\nsecond line" font ",10"
is changed to
set label at 0,0 "{/=10 first line}\n{/=10 second line} font ",8"
And your script changes to:
# Requires gnuplot >= 4.6
set terminal pdf color solid font "Helvetica, 10" enhanced size 4, 3.72
set output 'mwe.pdf'
set border 3 lc rgb "#000000"
set bmargin 9
set ylabel offset -1
TickSize = 10
TicksFont = ", ".TickSize
Title2Font = ", 14"
set yrange[0:180 < * ]
set format y "%.0f%%"
set xtics out scale 0, 0 nomirror rotate by 90 right offset -0.1,0 font ",8"
set ytics out nomirror font TicksFont
unset key
set datafile separator ";"
set style fill transparent solid 1.0 border -1
set style data boxes
set boxwidth 1.0
#Bottom-left plot
set title "C) Third (sub)plot" font Title2Font
set_label_size(s) = sprintf('{/=%d %s}', TickSize, s)
label(s) = strstrt(s, "\n") ? set_label_size(s[:strstrt(s, "\n")-1])."\n".set_label_size(s[strstrt(s, "\n")+1:]) : set_label_size(s)
plot '-' using ($0):2:(0xFF8080):xtic(label(strcol(1))) notitle lc rgb variable
# Label; Value
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. Ccccc: Ccccccc(Ccc),\nCccccccc(CCC); 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
;NaN
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. A single-liner; 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
;NaN
01. Aaaaa:\nAaaaaaaaa(AA); 100
02. Bbbbb:\nBbbbbbbbbbbb(BB); 20
03. A single-liner; 30
04. Dddd: DDDD,\nDDDDDDDDDDDD(DD); 40
05. Eeee: EEEE,\nEEEEEEEEEEEE(EE); 50
end;
Before (left) and after (right):
You can switch to the epslatex terminal and use latex syntax on your gnuplot script directly. Then you can use the \vspace{} command with a negative argument to decrease the interline spacing, something like this:
\shortstack{01. Aaaaa: \vspace{-0.2em} \\ Aaaaaaaaa(AA)}
Although you'll need to escape backslashes:
\\shortstack{01. Aaaaa: \\vspace{-0.2em} \\\\ Aaaaaaaaa(AA)}
Would it be acceptable to just change the size of the whole plot, for example by changing the plot width from 4 inches to 6 and the height from 3.72 to 5.58?
This way the aspect ratio is preserved if you include it in a different document.
Before:
After:
let's assume I have two files formatted like this:
x --- y
0 --- 2
1 --- 2.4
2 --- 3.6
which differ for the values of y.
is there a way to plot a single graph that is, for every x, the maximum value of y between the two files?
Dunno if explained my self sufficiently well.
I was trying with conditional sentences but I couldn't find any expression that let me search in 2 different files
There is no way to combine two files or more in a single plot with gnuplot only. You must use an external tool to do this, e.g. the command line utility paste:
max(x, y) = (x > y ? x : y)
plot '< paste fileA.txt fileB.txt' using 1:(max($2, $4))
The y values are contained in the second and fourth columns.
This next version uses a python script with numpy to concatenate the files, but any other scripting language would also do:
"""paste.py: merge lines of two files."""
import numpy as np
import sys
if (len(sys.argv) < 3):
raise RuntimeError('Need two files')
A = np.loadtxt(sys.argv[1])
B = np.loadtxt(sys.argv[2])
np.savetxt(sys.stdout, np.c_[A, B], delimiter='\t')
To plot, use:
max(x, y) = (x > y ? x : y)
plot '< python paste.py fileA.txt fileB.txt' using 1:(max($2, $4))
Just for the records, there is a way with gnuplot only to get the maximum out of two files.
For sure, it's probably more efficient to use Linux tools or on Windows install, e.g. CoreUtils from GnuWin, but with gnuplot-only you are surely platform independent without extra installations.
Assumption: both files have same number of lines and identical x-values
Edit: simplified code which works for all gnuplot versions>=4.6.0 and faster version for gnuplot>=5.2.0 using an array.
The simple "trick" is to write the y value of one file into a single string and address them via word(). For small data this is ok, but for larger data (>10'000 lines) it might get slow because apparently it runs with something like O(N^2). Just to get an idea (on my system): 1'000 lines take 0.4 seconds, 10'000 lines take 13 seconds and 20'000 lines already take 45 seconds.
As comparison, the array-solution for gnuplot>=5.2.0 just takes about 3 seconds for 10'000 lines.
Data:
SO19079146_1.dat
1 1.1
2 2.1
4 1.5
6 1.3
7 0.2
8 1.5
9 2.1
SO19079146_2.dat
1 2.1
2 2.5
4 1.5
6 0.3
7 0.7
8 1.0
9 1.4
Script 1: (works for gnuplot>=4.6.0, March 2012)
### plot maximum from two files
reset
FILE1 = 'SO19079146_1.dat'
FILE2 = 'SO19079146_2.dat'
data2 = ''
stats FILE2 u (data2=data2.' '.sprintf("%g",$2)) nooutput
set offset 1,1,1,1
max(col) = (i=int(column(0)+1), y1=column(col), y2=real(word(data2,i)), y1>y2 ? y1 : y2)
plot FILE1 u 1:(max(2)) w lp pt 7 lw 8 lc rgb "grey" ti "Max", \
'' u 1:2 w lp pt 7 lc rgb "red" ti "Data1", \
FILE2 u 1:2 w lp pt 7 lc rgb "blue" ti "Data2"
### end of script
Script 2: (works for gnuplot>=5.2.0, Sept. 2017)
### find the maximum out of two files/datablocks (gnuplot>=5.2.0)
reset session
FILE1 = 'SO/SO19079146_1.dat'
FILE2 = 'SO/SO19079146_2.dat'
stats FILE1 u 0 nooutput
array A[STATS_records]
stats FILE2 u (i=int($0+1), A[i]=$2) nooutput
set offset 1,1,1,1
max(col) = (i=int(column(0)+1), y1=column(col), y2=A[i], y1>y2 ? y1 : y2)
plot FILE1 u 1:(max(2)) w lp pt 7 lw 8 lc "grey" ti "Max", \
'' u 1:2 w lp pt 7 lc "red" ti "Data1", \
FILE2 u 1:2 w lp pt 7 lc "blue" ti "Data2"
### end of script
Result: (identical for all above versions)