{"id":298,"date":"2019-06-16T14:14:09","date_gmt":"2019-06-16T18:14:09","guid":{"rendered":"http:\/\/bhoey.com\/blog\/?p=298"},"modified":"2020-06-24T14:49:35","modified_gmt":"2020-06-24T18:49:35","slug":"simple-time-series-graphs-with-gnuplot","status":"publish","type":"post","link":"https:\/\/bhoey.com\/blog\/simple-time-series-graphs-with-gnuplot\/","title":{"rendered":"Simple Time Series Graphs With Gnuplot"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p><a href=\"http:\/\/www.gnuplot.info\/\">Gnuplot<\/a> is a feature rich command-line graphing utility available for Windows, Linux and Mac OSX. Though capable of generating much more advanced formula-based plots its also very handy at producing quick, ad-hoc time series graphs.<\/p>\n<h3>The Test Dataset<\/h3>\n<p>For our example we'll use the following basic dataset saved to file 'mydata.txt'<\/p>\n<pre class=\"brush: plain; notranslate\">2019-02-01  15\n2019-03-01   8\n2019-04-01  16\n2019-05-01  20\n2019-06-01  14\n<\/pre>\n<h3>Gnuplot Commands<\/h3>\n<p>These commands were tested with gnuplot 5.0 patchlevel 5<\/p>\n<pre class=\"brush: plain; notranslate\">set xdata time                           # Indicate that x-axis values are time values\nset timefmt \"%Y-%m-%d\"                   # Indicate the pattern the time values will be in\nset format x \"%m\/%y\"                     # Set how the dates will be displayed on the plot\n\nset xrange [\"2019-01-01\":\"2019-12-31\"]   # Set x-axis range of values\nset yrange [0:30]                        # Set y-axis range of values\n\nset key off                              # Turn off graph legend\nset xtics rotate by -45                  # Rotate dates on x-axis 45deg for cleaner display\nset title 'Squirrels Spotted'            # Set graph title\n\nset terminal jpeg                        # Set the output format to jpeg\nset output 'output.jpg'                  # Set output file to output.jpg\n\nplot 'mydata.txt' using 1:2 with linespoints linetype 6 linewidth 2\n\n<\/pre>\n<h3>Running Gnuplot<\/h3>\n<p>Although you can paste the commands above right into gnuplot, it usually makes sense to put these commands in their own file for future reuse and reference. If we put these into the file 'gpcommands.txt' then we just need to run:<\/p>\n<pre class=\"shell: plain; notranslate\">$ gnuplot &lt; gpcommands.txt\n<\/pre>\n<p>To have it generate the following plot:<\/p>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"640\" height=\"480\" src=\"https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2019\/06\/gnuplot-squirrels-spotted.jpg\" alt=\"time series gnuplot graph\" class=\"wp-image-305\" style=\"margin-left: 50px;\" srcset=\"https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2019\/06\/gnuplot-squirrels-spotted.jpg 640w, https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2019\/06\/gnuplot-squirrels-spotted-300x225.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n<h3>Bonus Example: Multiple Series<\/h3>\n<p>The above is a good demonstration of how to create a minimal time series graph, however often you'll want to chart multiple series together. Lets adjust our source data file to add two more series (as represented by columns) and one more row as we'll be widening the dimensions later:<\/p>\n<pre class=\"brush: plain; notranslate\">2019-02-01  10  8  12\n2019-03-01   8  9  12\n2019-04-01   9  10 10\n2019-05-01  11  10 11\n2019-06-01  14  9  11\n2019-07-01  14  10 12\n<\/pre>\n<p>For the gnuplot commands we'll add a bit of polish:<\/p>\n<ul>\n<li>Shortened date range on x-axis<\/li>\n<li>Shortened value range on y-axis<\/li>\n<li>Add the legend (aka key) to the top left<\/li>\n<li>Increase the size of the plot title at the top<\/li>\n<li>Change output image dimensions to 1200x600<\/li>\n<\/ul>\n<pre class=\"brush: plain; notranslate\">set xdata time                           # Indicate that x-axis values are time values\nset timefmt \"%Y-%m-%d\"                   # Indicate the pattern the time values will be in\nset format x \"%m\/%y\"                     # Set how the dates will be displayed on the plot\n \nset xrange [\"2019-02-01\":\"2019-07-01\"]   # Set x-axis range of values\nset yrange [7:17]                        # Set y-axis range of values\n\nset key left top                         # Set lengend location to top-left \nset xtics rotate by -45                  # Rotate dates on x-axis 45deg for cleaner display\nset title 'Squirrels Spotted' font \",18\" # Set graph title, set title font size to 18\n\nset terminal jpeg size 1200,630          # Set the output format to jpeg, set dimensions to 1200x630\nset output 'output.jpg'                  # Set output file to output.jpg\n \nplot 'mydata.txt' using 1:2 with linespoints linetype 6 linewidth 3 title 'Frontyard', \\\n     'mydata.txt' using 1:3 with linespoints linetype 7 linewidth 3 title 'Backyard', \\\n     'mydata.txt' using 1:4 with linespoints linetype 2 linewidth 3 title 'On Roof'\n<\/pre>\n<p>After this plot image was generated, I pasted a small squirrel picture using an external image editor to complete the image. Although it's somewhat possible to add a background image using gnuplot, the general consensus is to use your favorite image editor instead as it (<a href=\"https:\/\/www.gimp.org\/\">GIMP<\/a> is an open source, multiplatform option for those on a tight budget).<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-814 size-large\" src=\"http:\/\/bhoey.com\/blog\/wp-content\/uploads\/2019\/06\/gnuplot-3lines-WithSquirrel-1200x630-1-1024x538.png\" alt=\"GNUPlot Time Series Example\" width=\"474\" height=\"249\" srcset=\"https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2019\/06\/gnuplot-3lines-WithSquirrel-1200x630-1-1024x538.png 1024w, https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2019\/06\/gnuplot-3lines-WithSquirrel-1200x630-1-300x158.png 300w, https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2019\/06\/gnuplot-3lines-WithSquirrel-1200x630-1-768x403.png 768w, https:\/\/bhoey.com\/blog\/wp-content\/uploads\/2019\/06\/gnuplot-3lines-WithSquirrel-1200x630-1.png 1200w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/p>\n<h3>Final Thoughts<\/h3>\n<p>Although there are other graphing libraries and software that may plot time series graphs in a more visually appealing way there are few-to-none that match gnuplot's efficiency. More often than not you'll just need something that will visualize a set of data points thats 'good enough' and for this reason gnuplot is a very effective tool to keep in mind.<\/p>","protected":false},"excerpt":{"rendered":"<p>Gnuplot is a feature rich command-line graphing utility available for Windows, Linux and Mac OSX. Though capable of generating much more advanced formula-based plots its also very handy at producing quick, ad-hoc time series graphs.&nbsp;<a href=\"https:\/\/bhoey.com\/blog\/simple-time-series-graphs-with-gnuplot\/\">[Continue&nbsp;reading...] <span class=\"screen-reader-text\">Simple Time Series Graphs With Gnuplot<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":814,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[52],"_links":{"self":[{"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/posts\/298"}],"collection":[{"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/comments?post=298"}],"version-history":[{"count":21,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/posts\/298\/revisions"}],"predecessor-version":[{"id":816,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/posts\/298\/revisions\/816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/media\/814"}],"wp:attachment":[{"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/media?parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/categories?post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bhoey.com\/blog\/wp-json\/wp\/v2\/tags?post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}