I need to do a graph with gnoplot linux where on x-axis there are a date and on y-axis value of ok, ko and warning.
I've written the following code
#!/usr/bin/gnuplot
reset
unset title
set xlabel "Date (day/month)"
set ylabel "num Instance"
set yrange [0:40]
set term png large
set output "graph2.png"
set grid
set xdata time
set timefmt "%d/%m"
set format x "%d/%m"
plot \
'input.dat' using 1:2 with points pointtype 11 linecolor rgb "#00FF00" ti 'OK',\
'input.dat' using 1:3 with points pointtype 11 linecolor rgb "#ff0000" ti 'KO',\
'input.dat' using 1:4 with points pointtype 3 linecolor rgb "#ffed00" ti 'Warning'
with this input file
#date ok ko warning
06/02 1 30 3
07/02 5 14 16
08/02 30 15 14
09/02 9 14 9
10/02 7 30 3
11/02 2 7 21
12/02 30 12 15
13/02 10 15 14
14/02 24 14 16
15/02 20 12 15
and the graph is this
new_graph
Now I've to insert, near every point, also a value of y-axis.
Any idea?
I appreciate your help and thank you in advance
Related
I am using Gnuplot to show the precipitation measured during the last 13 monthts. Data is read from two data files, rain.dat and snow.dat. I use impulses, but on days with both rain and snow the impulses are plotted over each other. It had been better if the impulses were stacked.
#!/usr/bin/gnuplot -persist
set xdata time
set timefmt "%d.%m.%Y"
set ylabel "Precipitation (mm)"
set xrange ["01.`date --date="1 year ago" +%m.%Y`":"01`date --date="1 month" +.%m.%Y`"]
set xtics "01.`date --date="1 year ago" +%m.%Y`",2800000, \
"01.`date --date="now 1 month" +%m.%Y`" offset 3,0.2
set format x "%b"
set style line 100 lt 3 lc rgb "gray" lw 0.5
set style line 101 lt 3 lc rgb "gray" lw 0.5
set grid back xtics ytics mytics ls 100, ls 100, ls 101
set terminal png size 1000,200
set output 'precipitation.png'
plot 'rain.dat' using 1:2 title 'Rain' w impulses lt rgb '#ff0000' lw 4 , \
'snow.dat' using 1:2 title 'Snow' w impulses lt rgb '#0000ff' lw 2
rain.dat:
16.02.2021 8
22.02.2021 6
04.03.2021 10
08.03.2021 13
14.03.2021 5
snow.dat:
19.02.2021 19
22.02.2021 10
04.03.2021 14
12.03.2021 8
How can impulses be stacked with Gnuplot?
As #Ethan already mentioned, with impulses will always start at 0. If you don't mind some little extra effort, you can mimic "stacking" impulses if you first plot the sum of rain and snow with the "snow color", and then plot rain alone with the "rain color" on top of it.
But how do you get the sum of rain and snow?
plot your datablocks (or files) into the temporary datablock $Temp.
plot datablock $Temp into the datablock $SnowAndRain using the option smooth frequency which sums up snow and rain for each day. Check help smooth.
Script: (works for gnuplot>=5.2.0, Sept. 2017)
### "stacked" impulses
reset session
$Rain <<EOD
16.02.2021 8
22.02.2021 6
04.03.2021 10
08.03.2021 13
14.03.2021 5
EOD
$Snow <<EOD
19.02.2021 19
22.02.2021 10
04.03.2021 14
12.03.2021 8
EOD
myTimeFmt = "%d.%m.%Y"
set table $Temp
plot $Snow u (sprintf("%.0f",timecolumn(1,myTimeFmt))):2 w table
plot $Rain u (sprintf("%.0f",timecolumn(1,myTimeFmt))):2 w table
set table $SnowAndRain
set format x "%.0f"
plot $Temp u 1:2 smooth freq
unset table
set format x "%d %b" timedate
plot $SnowAndRain u 1:2 w impulses lw 4 lc "blue" title 'Snow', \
$Rain u (timecolumn(1,myTimeFmt)):2 w impulses lc "red" lw 4 title 'Rain', \
### end of script
Result:
Addition:
A bit more cumbersome solution which seem to work with gnuplot 5.0.0 (at least with Win10). I hope somebody can simplify this.
Script: (tested with Win10 gnuplot 5.0.0. Same result as above)
### "stacked" impulses (should work with gnuplot 5.0.0)
reset session
$Rain <<EOD
16.02.2021 8
22.02.2021 6
04.03.2021 10
08.03.2021 13
14.03.2021 5
EOD
$Snow <<EOD
19.02.2021 19
22.02.2021 10
04.03.2021 14
12.03.2021 8
EOD
myTimeFmt = "%d.%m.%Y"
set table $Temp
plot $Snow u (t=timecolumn(1,myTimeFmt)/1e5,int(t)):2:((t-int(t))*1e5) w table
plot $Rain u (t=timecolumn(1,myTimeFmt)/1e5,int(t)):2:((t-int(t))*1e5) w table
unset table
set table $SnowAndRain
set format x "%.0f"
plot $Temp u ($1*1e5+$3):2 smooth freq
unset table
set format x "%d %b" timedate
plot $SnowAndRain u 1:2 w impulses lw 4 lc "blue" title 'Snow', \
$Rain u (timecolumn(1,myTimeFmt)):2 w impulses lc "red" lw 4 title 'Rain'
### end of script
Impulses cannot be stacked. By definition they extend from y=0 to some non-zero y value.
If the two data sets were sampled at the same set of x coordinates then you could use the stacked histogram plot mode, but that isn't the case here.
How about back-to-back impulses rather than stacked impulses?
$RAIN << EOD
16.02.2021 8
22.02.2021 6
04.03.2021 10
08.03.2021 13
14.03.2021 5
EOD
$SNOW << EOD
19.02.2021 19
22.02.2021 10
04.03.2021 14
12.03.2021 8
EOD
set xzeroaxis
plot $RAIN using 1:2 with impulse lw 3, \
$SNOW using 1:(-$2) with impulse lw 3
I have tried to make a simplification to #theozh's answer, but some of the pulses are slightly displaced in time. #theozh has a much better solution of my problem.
reset session
$Rain <<EOD
16.02.2021 8
22.02.2021 6
04.03.2021 10
08.03.2021 13
14.03.2021 5
EOD
$Snow <<EOD
19.02.2021 19
22.02.2021 10
04.03.2021 14
12.03.2021 8
EOD
myTimeFmt = "%d.%m.%Y"
set table $Temp
plot $Snow u (timecolumn(1,myTimeFmt)):2 w table
plot $Rain u (timecolumn(1,myTimeFmt)):2 w table
unset table
set table $SnowAndRain
plot $Temp u 1:2 smooth freq
unset table
set format x "%d %b" timedate
plot $SnowAndRain u 1:2 w impulses lw 4 lc "blue" title 'Snow', \
$Rain u (timecolumn(1,myTimeFmt)):2 w impulses lc "red" lw 4 title 'Rain', \
### end of code
Addition:
The rain and snow pulses are drawn on top of each other if $Rain is written to table $RainTemp using (timecolumn(1,myTimeFmt)):2 before plotting the the graph. But still the timing is bit incorrect.
reset session
$Rain <<EOD
16.02.2021 8
22.02.2021 6
04.03.2021 10
08.03.2021 13
14.03.2021 5
EOD
$Snow <<EOD
19.02.2021 19
22.02.2021 10
04.03.2021 14
12.03.2021 8
EOD
myTimeFmt = "%d.%m.%Y"
set table $Temp
plot $Snow u (timecolumn(1,myTimeFmt)):2 w table
plot $Rain u (timecolumn(1,myTimeFmt)):2 w table
unset table
set table $SnowAndRain
plot $Temp u 1:2 smooth freq
unset table
set table $RainTemp
plot $Rain u (timecolumn(1,myTimeFmt)):2 w table
unset table
set format x "%d %b" timedate
plot $SnowAndRain u 1:2 w impulses lw 4 lc "blue" title 'Snow', \
$RainTemp u 1:2 w impulses lw 4 lc "red" title 'Rain'
I'm trying to follow the guide to plotting financial data on gnuplot found here:
http://gnuplot.sourceforge.net/demo/finance.html
I generated some data and tried plotting with the following script:
1 set datafile separator ","
2 set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#cccccc" behind
3
4 set xdata time
5 set timefmt "%Y-%M-%dT%H:%M:%S"
6 set format x "%d-%H"
7
8 set ylabel "Price"
9 set xlabel 'Time'
10
11 set style line 100 lt 1 lc rgb "grey" lw 0.5
12 set style line 101 lt 1 lc rgb "orange" lw 1
13
14 set grid ls 100
15 set key off
16
17 plot "lastrun.dat" using 1:2 with lines, '' using 1:3 with lines ls 101, '' using 1:4 with lines ls 101
however the resulting graph seems to not stich together the data correctly, so instead of one chart, i have three in the same space.
i'm sure i'm making a simple error but I've been stumped about what that is.
Check help time_specifiers:
%d day of the month, 01–31
%H hour, 00–23 (always two digits)
%m month, 01–12
%M minute, 0–60
%S second, integer 0–60 on output, (double) on input
%Y year, 4-digit
Given this file of data:
#date foo bar
2018-10-12_11-54-52 12 1
2018-10-12_11-55-09 14 7
2018-10-12_11-55-16 13 10
2018-10-12_13-00-01 15 8
2018-10-12_14-00-01 12 9
and this set of plot commands:
set timefmt "%Y-%m-%d_%H-%M-%S"
set xrange ["2018-10-12_11-54-52" : "2018-10-12_14-00-01"]
plot 'datafile.txt' using 1:2 with linespoints linestyle 1
plot 'datafile.txt' using 1:3 with linespoints linestyle 1
when I run gnuplot with gnuplot -p cmd_file.txt it errors with
line 3: Can't plot with an empty x range!
The xrange should be over 125 minutes long. Why does gnuplot think it is empty?
This command draws nice green sine waves from a data file, but the col4 value goes negative periodically.
"" u (myDateSP(1,2)):4 lc rgb "green" lt 1 lw 6 sm cspl notitle,\
I'd like to have it draw the line red for negative values of col4 and green for positive values and it seems as if I should be able to use the ternary operator for that but I'm having problems with the grammar:
"" u (myDateSP(1,2)):4:($4<= 0) ? lc rgb "red" lt 1 lw 6 sm cspl notitle,\:lc rgb "green" lt 1 lw 6 sm cspl notitle,\
and other variants throw errors. Can someone point out where I have this wrong?
Your table example works nicely, many thanks.
I tried to apply it to a data file like this:
2015-01-03 18:02 01 29.49 feet High Tide
2015-01-04 01:12 01 -2.29 feet Low Tide
2015-01-04 07:02 01 29.09 feet High Tide
2015-01-04 13:22 01 4.54 feet Low Tide
2015-01-04 18:41 01 29.80 feet High Tide
2015-01-04 19:54 01 Full Moon
2015-01-05 01:52 01 -1.87 feet Low Tide
2015-01-05 07:36 01 29.30 feet High Tide
2015-01-05 14:00 01 4.51 feet Low Tide
2015-01-05 19:17 01 29.99 feet High Tide
2015-01-06 02:26 01 -1.30 feet Low Tide
and am not having much luck.
Using this:
set linetype 11 linecolor rgb "green"
set linetype 12 linecolor rgb "red"
set xdata time
set timefmt '%Y-%m-%d'
set format x '%s'
set table 'data-smoothed1.txt'
set samples 1000
plot 'data.txt' using 1:4 smooth cspline
unset table
set timefmt '%s'
set format x '%Y-%m-%d'
set xzeroaxis
plot 'data-smoothed.txt' using 1:4:($4 < 0 ? 12 : 11) linecolor variable with lines no title
It complains that the xrange is wrong, so when I add an range with the beginning and ending dates, it still doesn't help. The "01" column is the month number.
You must use linecolor variable, which allows you to give an additional column which specifies the linecolor to use. But that doesn't work directly with smooth, so that you must first write the smoothed data to an external file (with gnuplot 5 you can also use a heredoc) and then plot this file with linecolor variable.
When doing this, you must also take care of which time format is used to write the smoothed data. The format is taken from the set format x settings. I would use %s, i.e. Unix timestamp.
Here is a full, self-contained example showing the different steps:
An example file data.txt:
2014-02-11 2
2014-03-12 -1
2014-04-15 3
2014-05-22 -2
And the script
set linetype 11 linecolor rgb "green"
set linetype 12 linecolor rgb "red"
set xdata time
set timefmt '%Y-%m-%d'
set format x '%s'
set table 'data-smoothed.txt'
set samples 1000
plot 'data.txt' using 1:2 smooth cspline
unset table
set timefmt '%s'
set format x '%Y-%m-%d'
set xzeroaxis
plot 'data-smoothed.txt' using 1:2:($2 < 0 ? 12 : 11) linecolor variable with lines notitle
I have the 2 files from either of them I'll plot a point line using the following code:
set terminal postscript eps color solid font "Helvetica, 22"
set size ratio 0.625
set output "example.eps"
set key right top
plot "traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
Either line forms a region together with x axis and the regions formed by both lines overlaps. I was wondering how may I shade the overlapping parts.
Thanks!
(The files used are as follows)
File 1
1 66.660000
2 47.830000
3 39.270000
4 27.940000
5 24.990000
6 27.930000
7 32.060000
8 43.650000
9 70.470000
10 73.430000
11 87.690000
12 111.790000
13 122.170000
14 114.930000
15 111.620000
16 109.330000
17 121.370000
18 118.600000
19 132.890000
20 132.480000
21 148.360000
22 152.260000
23 140.510000
24 99.120000
File 2
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0.121933
10 1.81455
11 2.25622
12 2.67994
13 2.87834
14 2.53149
15 1.29541
16 0.57571
17 0.0883007
18 0
19 0
20 0
21 0
22 0
23 0
24 0
You can use the filledcurves plotting style. For that all data must be contained in one file. You can combine the files on-the-fly e.g. with paste. For a platform-independent solution with python look e.g. this answer.
With filledcurves you can also distinguish between above and below in order to use different colors:
set key right top
set autoscale xfix
set xtics 4
plot "< paste traffic.txt solar.txt" using 1:2:($4*100) with filledcurves below lc rgb "#ffaaaa" t '', \
"" using 1:2:($4*100) with filledcurves above lc rgb "#aaaaff" t '',\
"traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
The result with 4.6.4 is:
To shade only the region where the two curves overlap, you need a workardound. First shade the region between the narrower curve and the x1 axis, and then overwrite parts of that shading with white:
set autoscale xfix
set xtics 4
plot "< paste traffic.txt solar.txt" using 1:($4*100) with filledcurves x1 lc rgb "#ffaaaa" t '', \
"" using 1:2:($4*100) with filledcurves below lc rgb "white" t '',\
"traffic.txt" using 1:2 title "traffic" with lp pt 7 ps 1 lc rgb "red", \
"solar.txt" using 1:($2*100) title "solar" with lp pt 9 ps 1 lc rgb "blue"
This gives: