{"id":2819,"date":"2018-10-21T19:02:58","date_gmt":"2018-10-22T00:02:58","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=2819"},"modified":"2018-10-21T19:03:14","modified_gmt":"2018-10-22T00:03:14","slug":"creating-a-histogram-with-gnuplot","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2018\/10\/creating-a-histogram-with-gnuplot\/","title":{"rendered":"Creating a histogram with Gnuplot"},"content":{"rendered":"<p><a href=\"http:\/\/www.gnuplot.info\/\">Gnuplot<\/a> has plenty of examples on its <a href=\"http:\/\/gnuplot.sourceforge.net\/demo\/histograms.html\">histograms demo page<\/a>. The demos use <code>immigration.dat<\/code> as a datasource, which you can find in gnuplot&#8217;s GitHub repository: <a href=\"https:\/\/github.com\/gnuplot\/gnuplot\/blob\/master\/demo\/immigration.dat\">immigration.dat data source<\/a>.<\/p>\n<p>While the examples demonstrate many of the available features, it&#8217;s not clear what some of the specific options do. You could read the documentation, but who has time for that? Some of us are just trying to create really simple histograms and don&#8217;t need to master the nuances of gnuplot.<br \/>\n<!--more--><\/p>\n<p>Here&#8217;s my sample data, <code>colors.data<\/code>, a series of attributes and a value associates with each attribute:<\/p>\n<pre><code>#Color Count\r\nRed 45\r\nOrange 17\r\nYellow 92\r\nGreen 262\r\nBlue 129\r\nPurple 80<\/code><\/pre>\n<p>Start gnuplot:<\/p>\n<pre><code>$ gnuplot<\/code><\/pre>\n<p>Set the style to histograms and plot the datafile:<\/p>\n<pre><code>gnuplot&gt; set style data histograms\r\ngnuplot&gt; plot '.\/colors.data' using 2:xtic(1)<\/code><\/pre>\n<p>The <code>plot<\/code> command above indicates that were are plotting the data from column 2, and we are using column 1 for the xtic labels (the x-axis item markers).<\/p>\n<p>In the resulting histogram, the title says <code>'color.data' using using 2:xtic(1)<\/code> which is not particularly meaningful. You can change this by including a title:<\/p>\n<pre><code>gnuplot&gt; plot 'colors.data' using 2:xtic(1) title 'Values by Color'<\/code><\/pre>\n<p>That&#8217;s it! Here&#8217;s the result:<\/p>\n<p><a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2018\/10\/creating-a-histogram-with-gnuplot\/gnuplot-histogram-hollow\/\" rel=\"attachment wp-att-2821\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-hollow.png\" alt=\"An example histogram displaying several vertical bars of different heights. In this example, the bars are hollow, defined by a purple outline.\" width=\"938\" height=\"646\" class=\"alignnone size-full wp-image-2821\" srcset=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-hollow.png 938w, https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-hollow-300x207.png 300w, https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-hollow-768x529.png 768w\" sizes=\"auto, (max-width: 938px) 100vw, 938px\" \/><\/a><\/p>\n<p>I admit, the hollow bars were bothering me, so I make them solid:<\/p>\n<pre><code>gnuplot&gt; set style fill solid<\/code><\/pre>\n<p><a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2018\/10\/creating-a-histogram-with-gnuplot\/gnuplot-histogram-solid\/\" rel=\"attachment wp-att-2822\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-solid.png\" alt=\"An example histogram showing several vertical bars of different heights. The bars are solid purple in this example.\" width=\"938\" height=\"646\" class=\"alignnone size-full wp-image-2822\" srcset=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-solid.png 938w, https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-solid-300x207.png 300w, https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-solid-768x529.png 768w\" sizes=\"auto, (max-width: 938px) 100vw, 938px\" \/><\/a><\/p>\n<p>The purple color looked great, but if you&#8217;re planning to print something in black-and-white you might want to change the color as well:<\/p>\n<pre><code>gnuplot&gt; plot '.\/colors.data' using 2:xtic(1) linecolor 'black' title 'Values by Color'<\/code><\/pre>\n<p><a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2018\/10\/creating-a-histogram-with-gnuplot\/gnuplot-histogram-solid-black\/\" rel=\"attachment wp-att-2823\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-solid-black.png\" alt=\"An example histogram showing several vertical bars of different heights. In this example, the bars are solid black.\" width=\"938\" height=\"646\" class=\"alignnone size-full wp-image-2823\" srcset=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-solid-black.png 938w, https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-solid-black-300x207.png 300w, https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2018\/10\/gnuplot-histogram-solid-black-768x529.png 768w\" sizes=\"auto, (max-width: 938px) 100vw, 938px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a novice gnuplot user, I wanted to generate a simple histogram, but the number of different options available was bewildering. I came up with a simpler example than what the gnuplot demo page provided.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[520],"class_list":["post-2819","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-gnuplot"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=2819"}],"version-history":[{"count":5,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2819\/revisions"}],"predecessor-version":[{"id":2827,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2819\/revisions\/2827"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=2819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=2819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=2819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}