📜 ⬆️ ⬇️

Gnuplot: build graphs from experimental data

Recently I had a task to build graphs based on readings from remote sensors. In particular, it was necessary to process the data of air temperature fluctuations over time. It looked like this. From a remote sensor, a signal with information is sent to a receiving device, then to a computer and then written to Mysql. And the next step was to process and generate graphs for reports. Charts were built of several types - daily, weekly, monthly.

The task is simple, only it was necessary to take into account a number of points. Thus, the interval between the "points" of measurements could vary. If in the test configuration, the sensor sent measurements every 2 minutes, then in the working configuration, measurements were taken with an interval of 15 minutes. Then the packets during transmission due to radio interference could be lost, respectively, there could be irregular gaps in the observations, therefore interpolation of values ​​was required.

The whole process had to be automated; the tasks of scheduling were planned to be run on the crown on a special server (Ubuntu Server). As a graphing tool, I chose gnuplot. The program is very convenient and the main thing is able to work with scripts, it supports such graphic formats as PNG, EPS, SVG. In general - that is necessary.
')
So, it was necessary to draw 2 axes, on “X” - to postpone the time coordinates (in hours), on “Y” - coordinates of temperature values ​​(from -40 to 40). And then on this grid to set the measurement points connecting their curve.

As an example for analyzing how gnuplot works, here’s a script for plotting fluctuations in daily temperature, i.e. in 24 hours.

The data file — let's call it daily.dat, contains measurement readings for the required period of time, for a specific day. The first column indicates the date and time of the measurement, and the next one indicates the temperature.
2010-05-12 00:38:11 13
2010-05-12 00:53:42 13
2010-05-12 01:09:13 13
2010-05-12 01:24:45 13


The script with the instructions gnuplot we call daily.gnu
  1. #! / usr / bin / gnuplot -persist
  2. # set the output mode to file, PNG format
  3. # default font for couri.ttf, 8px,
  4. # background graphics are white, black font
  5. # graphics size 640x480px
  6. set term png enhanced font "/usr/share/fonts/truetype/msttcorefonts/couri.ttf,8" \
  7. xffffff x000000 size 640,480
  8. set output "/var/graph/images/daily.png" # specify the path and file name
  9. set encoding iso_8859_1 # set encoding
  10. set tmargin 1 # indent above
  11. set tics out # expose dash outside
  12. set size 1.0,1.0 # set the proportions of the chart
  13. set nokey # do not display header
  14. set grid # display grid
  15. set xzeroaxis lt -1 # draw the zero line
  16. set yzeroaxis
  17. # parameters on "X"
  18. set xdata time # set the data for “X” this time
  19. set timefmt "% Y-% m-% d% H:% M:% S" # time format
  20. set xtics 7200 # step 2 hours (60 * 60 * 2)
  21. set format x "% H" # only the hour value is displayed on the coordinate (hour)
  22. # parameters for "Y"
  23. set ytics 5 # step 5 degrees
  24. set yrange [-40: 40] # value range from -40 to 40
  25. set format y "{% g} ^ o" # on the coordinate we display the value and write a degree in degrees
  26. # parameters for "Y2"
  27. set y2range [-40: 40]
  28. set y2tics 5
  29. set format y2 "{% g} ^ o"
  30. # set the line style 1 (line 1)
  31. #linetype sets the line color (default values ​​1,2,3 ... can be used)
  32. # or specify your color - rgb "# 409ff1"
  33. #lw - linewidth, pt - pointtype, ps - pointsize
  34. set style line 1 lt rgb "# 409ff1" lw 2 pt 3 ps 0.5
  35. # draw a graph
  36. # specify where to get the data - daily.dat
  37. # what columns to watch - using 1: 3
  38. # say what interpolation method to use - smooth csplines
  39. # say what line style to use - linestyle 1
  40. plot "./daily.dat" using 1: 3 smooth csplines linestyle 1
* This source code was highlighted with Source Code Highlighter .

Well, in order to generate a graph, it remains only to run gnuplot with this script in the terminal.
# gnuplot ./daily.gnu

And in the specified location "/var/graph/images/daily.png" a file should appear.


If you need to generate a vector file, for example in SVG - for this you need to change just a couple of lines:
set term svg enhanced size 640,480 font "/usr/share/fonts/truetype/msttcorefonts/couri.ttf,8"
set output "var/graph/images/daily.svg"

In the working example, files with a script and data are formed with a skip on python, are formed and then run. In the script itself (daily.gnu), the line with the name of the chart - “daily_2010.05.12.png” changes every time, and daily.dat is updated to the new values ​​each time.
Actually that's all.

For more details on gnuplot instructions, see the official site.
There is also excellent documentation PDF, 2Mb

Script source data can be downloaded from here.
Thank you for your attention and good coding!

Source: https://habr.com/ru/post/94025/


All Articles