Gnuplot Tips
Enable Javascript for TOC.
Configuration
Passing parameters to Gnuplot Script
$ ./pu_selftest_verification/gnuplot_diagram.sh abc
Error: Wrong parameter count
Usage: ./pu_selftest_verification/gnuplot_diagram.sh INPUT_FILE OUTPUT_FILE
INPUT_FILE CVS file e.g. pu_ast_sys_diff_100411.log
OUTPUT_FILE Image file e.g. pu_ast_sys_diff_100411.png
$
Passing to the shebang the gnuplot path with -c
, the parameters
can be accessed with ARG0
, ARG1
, etc.
if (ARGC < 2) {
print 'Error: Wrong parameter count'
print ''
print 'Usage: ', ARG0, ' INPUT_FILE OUTPUT_FILE'
print ''
print ' INPUT_FILE CVS file e.g. pu_ast_sys_diff_100411.log'
print ' OUTPUT_FILE Image file e.g. pu_ast_sys_diff_100411.png'
exit
}
input_file=ARG1
output_file=ARG2
Source: Stackoverflow
Colored Legend Entries
set key textcolor variable
First Line is Header
When having a CSV like this...
Local-Counter,DateTime,memtotal,hightotal,lowtotal,swaptotal,memfree,highfree,lowfree,swapfree,memshared,cached,active,bigfree,buffers,swapcached,inactive
1,2024-10-12 11:35:04,14953.9,-0.0,-0.0,15360.0,1263.1,-0.0,-0.0,8030.8,10.5,9074.0,6167.4,-1.0,0.5,122.5,6618.7
2,2024-10-12 11:35:05,14953.9,-0.0,-0.0,15360.0,1260.2,-0.0,-0.0,8030.8,10.5,9074.0,6167.4,-1.0,0.5,122.5,6618.3
3,2024-10-12 11:35:06,14953.9,-0.0,-0.0,15360.0,1235.8,-0.0,-0.0,8030.8,10.2,9073.9,6167.5,-1.0,0.5,122.5,6645.3
4,2024-10-12 11:35:07,14953.9,-0.0,-0.0,15360.0,1194.3,-0.0,-0.0,8031.1,10.0,9081.4,6164.1,-1.0,0.5,122.5,6690.2
the following command makes the first line as a header, which is used in the legend:
set key autotitle columnhead
Reading the date-time from row 2, the following lines are needed
set datafile separator ','
set timefmt '%Y-%m-%d %H:%M:%S'
plot input_file using 2:12 with lines
Vertical Legend
set key top
set key horizontal
Using Transparency
E.g. when plotting the average together with the original (noisy) data, it would make sense
to plot the noisy data with transparency. This can be done with these lines:
set terminal pngcairo size 2000,1280 enhanced font 'Helvetica,16'
set style line 5 linecolor rgb "#77FF0000" linewidth 1
set style line 6 linecolor rgb "#FF0000" linewidth 2
plot
input_file using 2:(($3-$7)+($6-$10)+$11-$12) with lines linestyle 5, \
input_file using 2:(avg_n(($3-$7)+($6-$10)+$11-$12)) with lines linestyle 6
Plotting
Histogram
Using the following data file:
Session;Session Date;Date;AST-Sys Diff
1;2022-10-06T15:59:39;2022-10-06T14:03:19.497394Z;-6588
1;2022-10-06T15:59:39;2022-10-06T14:08:16.059585Z;-3873
1;2022-10-06T15:59:39;2022-10-06T14:08:18.531537Z;-4201
1;2022-10-06T15:59:39;2022-10-06T14:08:21.007526Z;-4484
1;2022-10-06T15:59:39;2022-10-06T14:08:23.527556Z;205
1;2022-10-06T15:59:39;2022-10-06T14:08:25.999621Z;111
1;2022-10-06T15:59:39;2022-10-06T14:08:28.514464Z;20
1;2022-10-06T15:59:39;2022-10-06T14:08:31.058529Z;3
1;2022-10-06T15:59:39;2022-10-06T14:08:33.527563Z;142
With this script a image is generated with the data from the 4th column:
#!/usr/bin/gnuplot -c
#
# Plotting CSV file as Histogram
#
input_file=ARG1
output_file=ARG2
boxw_ratio = 0.7
use_log = 0
binwidth = 200
binstart = -100
set datafile separator ';'
set terminal png size 1000,600 enhanced font 'Helvetica,16'
set output output_file
set title Synchronization Error on device ' . sn
set xlabel 'Synchronization Error [ns]'
if (use_log) {
set logscale y
set ylabel 'Occurrence (logarithmic)'
}
else {
unset logscale y
set ylabel 'Occurrence'
}
set boxwidth boxw_ratio*binwidth
set style fill solid 0.5
hist = 'u (binwidth*(floor(($4-binstart)/binwidth)+0.5)+binstart):(1.0) smooth freq w boxes'
plot input_file i 0 @hist ls 2 notitle
When having use_log = 0
a histogram with regular axis is printed:
If use_log = 1
the axis have logarithmic scale:
Multi-Line Diagram
The input data look like this:
Date;Interface;RX packets;errors;dropped;overruns;frame;TX packets;errors;dropped;overruns;carrier;RX bytes;TX bytes;Load 1m;Load 5m;Load 15m;
2023-06-15 09:46:00;eth_internal;147367327;0;76;0;4;221201173;0;0;0;0;164893180624;285983366415; 0.43; 0.70; 0.64
2023-06-15 09:46:01;eth_internal;147367414;0;76;0;4;221201229;0;0;0;0;164893283396;285983372441; 0.56; 0.72; 0.64
2023-06-15 09:46:02;eth_internal;147367422;0;76;0;4;221201256;0;0;0;0;164893285420;285983374964; 0.56; 0.72; 0.64
2023-06-15 09:46:03;eth_internal;147373803;0;76;0;4;221202149;0;0;0;0;164902740012;285983441976; 0.56; 0.72; 0.64
2023-06-15 09:46:04;eth_internal;147373807;0;76;0;4;221202172;0;0;0;0;164902740264;285983444306; 0.56; 0.72; 0.64
2023-06-15 09:46:05;eth_internal;147373814;0;76;0;4;221202193;0;0;0;0;164902744314;285983446370; 0.56; 0.72; 0.64
2023-06-15 09:46:06;eth_internal;147373822;0;76;0;4;221202220;0;0;0;0;164902746338;285983448893; 0.51; 0.71; 0.64
2023-06-15 09:46:07;eth_internal;147373824;0;76;0;4;221202238;0;0;0;0;164902746464;285983450759; 0.51; 0.71; 0.64
#!/usr/bin/gnuplot -c
#
# Plotting CSV file as multiline Graph
#
if (ARGC < 2) {
print 'Error: Wrong parameter count'
print ''
print 'Usage: ', ARG0, ' INPUT_FILE OUTPUT_FILE [AVG_CNT]'
print ''
print ' INPUT_FILE CVS file e.g. pu_ast_sys_diff_v1.1_100411.log'
print ' OUTPUT_FILE Image file e.g. pu_ast_sys_diff_v1.1_100411.png'
exit
}
input_file=ARG1
output_file=ARG2
event_time="2023-06-15 10:10:24"
set key autotitle columnhead
set datafile separator ';'
set terminal png size 1000,600 enhanced font 'Helvetica,16'
set xdata time
set xlabel 'Localtime (+02:00)'
set format x '%H:%M'
#set timefmt '%Y-%m-%dT%H:%M:%SZ'
set timefmt '%Y-%m-%d %H:%M:%S'
# printing vertical line at given date and time
set arrow from event_time,graph(0,0) to event_time,graph(1,1) lt rgb 'red' nohead
set output output_file
set title 'Load Average'
set ylabel 'Competing Processes'
plot input_file using 1:15 with points pointtype 7 pointsize 0.3 title '1 minute avg', \
input_file using 1:16 with points pointtype 7 pointsize 0.3 title '5 minute avg', \
input_file using 1:17 with points pointtype 7 pointsize 0.3 title '15 minute avg'
Results over Time
In case that we have 128 samples per second, we can use this formula and have an x-axis in seconds:
$ head XCZU2EG.cap
22.470us
21.580us
21.450us
21.560us
21.260us
21.560us
21.260us
21.320us
21.450us
21.380us
$ gnuplot
G N U P L O T
Version 5.2 patchlevel 6 last modified 2019-01-01
gnuplot>
gnuplot> plot 'XCZU2EG.cap' using ($0/128):1 with points title "XCZU2EG over time"
Plot every nth Measurement Result
If only every 500th point shall be plotted:
gnuplot> plot 'XCZU2EG.cap' every 500 using ($0*500/128):1 with points title "XCZU2EG over time"