scripting http://simonsilk.com/taxonomy/term/36 en Plotting ASCII histograms in Perl with Gnuplot http://simonsilk.com/blog-entry/2019-feb/plotting-ascii-histograms-perl-gnuplot <span class="field field--name-title field--type-string field--label-hidden">Plotting ASCII histograms in Perl with Gnuplot</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Histograms are one of the simplest and handiest data visualizations tools. They give you a quick view of how tightly or loosely your data clusters, whether it is normally distributed, and whether it has outliers. If you spend a lot of time processing data in Perl and don't need a high fidelity plot, it is nice to just plot using ASCII art. Luckily, the Perl library <a href="http://search.cpan.org/dist/Chart-Gnuplot/lib/Chart/Gnuplot.pm">Chart::Gnuplot</a> give you access to the powerful Linux command line plotting tool <a href="http://gnuplot.info/">Gnuplot</a> that can be used to do exactly that. </p> <p>First off, we load a few libaries: <em>strict</em> and <em>warnings</em> to help catch obvious coding mistakes, <em>POSIX</em> to give us Floor and Ceiling functions, and <em>Chart::Gnuplot</em> for obvious reasons...</p> <p></p><div class="geshifilter"><pre class="perl geshifilter-perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">use</span> POSIX<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Floor, Ceiling functions</span> <span style="color: #000000; font-weight: bold;">use</span> Chart<span style="color: #339933;">::</span><span style="color: #006600;">Gnuplot</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Perl Gnuplot library</span></pre></div> <p>Next, generate an array of data for which you want to plot a histogram. This would typically be data you've parsed out of a text file or calculated based on its contents. For this example I generated some fake classroom grade data for 20 students in Excel using the formula <em>norm.inv(rand(), 70, 10)</em> (that's a Normal distribution with a mean of 70 and standard deviation of 10). </p> <p></p><div class="geshifilter"><pre class="perl geshifilter-perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@data</span><span style="color: #339933;">=</span><span style="color: #009900;">(</span> <span style="color: #cc66cc;">61.98482</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">65.68389</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">56.50473</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">82.21215</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">54.66456</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">76.90416</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">70.34352</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">47.24502</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">53.32660</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">66.73437</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">76.98941</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">75.92255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">76.78182</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">66.64782</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">68.04657</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">54.65173</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">68.58608</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">63.18633</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">89.06680</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">69.02130</span><span style="color: #339933;">,</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre></div> <p>Now convert the raw data into the data for the histogram plot itself:</p> <p></p><div class="geshifilter"><pre class="perl geshifilter-perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Generate the histogram plot data</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$binwidth</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@histdata</span><span style="color: #339933;">=</span><a href="http://perldoc.perl.org/functions/sort.html"><span style="color: #000066;">sort</span></a> <span style="color: #009900;">{</span><span style="color: #0000ff;">$a</span> <span style="color: #339933;">&lt;=&gt;</span> <span style="color: #0000ff;">$b</span><span style="color: #009900;">}</span> <span style="color: #0000ff;">@data</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Sort data numerically</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$x_min</span><span style="color: #339933;">=</span>floor<span style="color: #009900;">(</span><span style="color: #0000ff;">$histdata</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">]</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span><span style="color: #009900;">)</span><span style="color: #0000ff;">*$binwidth</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Beginning of x plot range</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$x_max</span><span style="color: #339933;">=</span>ceil<span style="color: #009900;">(</span><span style="color: #0000ff;">$histdata</span><span style="color: #009900;">[</span><span style="color: #0000ff;">@histdata</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span><span style="color: #009900;">)</span><span style="color: #0000ff;">*$binwidth</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># End of x plot range</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@bins</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/map.html"><span style="color: #000066;">map</span></a> <span style="color: #0000ff;">$binwidth</span><span style="color: #339933;">*</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$x_min</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$x_max</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@histo</span><span style="color: #339933;">=</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> x <span style="color: #0000ff;">@bins</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">(</span><span style="color: #0000ff;">@histdata</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;"># Create the histogram plot data</span> <span style="color: #0000ff;">$histo</span><span style="color: #009900;">[</span> <a href="http://perldoc.perl.org/functions/int.html"><span style="color: #000066;">int</span></a><span style="color: #009900;">(</span> <span style="color: #009900;">(</span> <span style="color: #0000ff;">$_</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$x_min</span> <span style="color: #009900;">)</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span> <span style="color: #009900;">)</span> <span style="color: #009900;">]</span><span style="color: #339933;">++;</span> <span style="color: #009900;">}</span></pre></div> <p>After that, we create the Gnuplot chart and configure it for histogram plotting:</p> <p></p><div class="geshifilter"><pre class="perl geshifilter-perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tmpfile</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">"temp.tmp"</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$chart</span> <span style="color: #339933;">=</span> Chart<span style="color: #339933;">::</span><span style="color: #006600;">Gnuplot</span><span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">(</span> terminal<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">'dumb'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;"># ASCII plotting</span> output <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">$tmpfile</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;"># Gnuplot will plot to this temp file</span> title<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">"Final exam grades distribution"</span><span style="color: #339933;">,</span> xlabel<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">"Grade"</span><span style="color: #339933;">,</span> ylabel<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">''</span><span style="color: #339933;">,</span> border <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">{</span> sides <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"bottom, left"</span><span style="color: #339933;">,</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> xtics <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">{</span> mirror <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"off"</span><span style="color: #339933;">,</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> ytics <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">{</span> mirror <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"off"</span><span style="color: #339933;">,</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre></div> <p>Setting the terminal type to 'dumb' makes Gnuplot plot in ASCII art to the file denoted by 'output'.</p> <p>After that, we add our histogram data as a data set in the chart, then tell Gnuplot to plot it. Finally we read back the ASCII plot from the temp file and print it to the console or a summary file...</p> <p></p><div class="geshifilter"><pre class="perl geshifilter-perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dataset</span> <span style="color: #339933;">=</span> Chart<span style="color: #339933;">::</span><span style="color: #006600;">Gnuplot</span><span style="color: #339933;">::</span><span style="color: #006600;">DataSet</span><span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">(</span> using <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"2:xticlabels(1)"</span><span style="color: #339933;">,</span> style <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"histograms"</span><span style="color: #339933;">,</span> xdata <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@bins</span><span style="color: #339933;">,</span> ydata <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@histo</span><span style="color: #339933;">,</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #0000ff;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">plot2d</span><span style="color: #009900;">(</span><span style="color: #0000ff;">$dataset</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$content</span><span style="color: #339933;">;</span> <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">(</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$fh</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'&lt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$tmpfile</span><span style="color: #009900;">)</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">"cannot open file $tmpfile"</span><span style="color: #339933;">;</span> <span style="color: #009900;">{</span> <a href="http://perldoc.perl.org/functions/local.html"><span style="color: #000066;">local</span></a> <span style="color: #0000ff;">$/</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$content</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$fh</span><span style="color: #339933;">&gt;;</span> <span style="color: #009900;">}</span> <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a><span style="color: #009900;">(</span><span style="color: #0000ff;">$content</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre></div> <p>Putting it all together we get the full code at the bottom of this post, and running that, we get the following output:</p> <p></p><div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"> Final exam grades distribution   <span style="color: #000000;">8</span> +-+ <span style="color: #000000; font-weight: bold;">*****</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">7</span> <span style="color: #000000; font-weight: bold;">|</span>-+ <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">|</span>-+ <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">|</span>-+ <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">****</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span>-+ <span style="color: #000000; font-weight: bold;">*****</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">|</span>-+ <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">|</span>-+ <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">****</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>-+ <span style="color: #000000; font-weight: bold;">*****</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> + <span style="color: #000000;">0</span> +----------------------------------------------------------------------+ <span style="color: #000000;">40</span> <span style="color: #000000;">50</span> <span style="color: #000000;">60</span> <span style="color: #000000;">70</span> <span style="color: #000000;">80</span> <span style="color: #000000;">90</span> Grade</pre></div> <p>If you found this useful, please let me know about it in the comments below. Thanks for reading! </p> <p>Full code for this post:</p> <p></p><div class="geshifilter"><pre class="perl geshifilter-perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">use</span> POSIX<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Floor, Ceiling functions</span> <span style="color: #000000; font-weight: bold;">use</span> Chart<span style="color: #339933;">::</span><span style="color: #006600;">Gnuplot</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Perl Gnuplot library</span>   <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@data</span><span style="color: #339933;">=</span><span style="color: #009900;">(</span> <span style="color: #666666; font-style: italic;"># Some dummy class grades generated in Excel</span> <span style="color: #cc66cc;">61.98482</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">65.68389</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">56.50473</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">82.21215</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">54.66456</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">76.90416</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">70.34352</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">47.24502</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">53.32660</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">66.73437</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">76.98941</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">75.92255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">76.78182</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">66.64782</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">68.04657</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">54.65173</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">68.58608</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">63.18633</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">89.06680</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">69.02130</span><span style="color: #339933;">,</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #009900;">(</span><span style="color: #ff0000;">"The raw data:<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #339933;">.</span><a href="http://perldoc.perl.org/functions/join.html"><span style="color: #000066;">join</span></a><span style="color: #009900;">(</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #339933;">,</span><span style="color: #0000ff;">@data</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;"># Generate the histogram plot data</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$binwidth</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@histdata</span><span style="color: #339933;">=</span><a href="http://perldoc.perl.org/functions/sort.html"><span style="color: #000066;">sort</span></a> <span style="color: #009900;">{</span><span style="color: #0000ff;">$a</span> <span style="color: #339933;">&lt;=&gt;</span> <span style="color: #0000ff;">$b</span><span style="color: #009900;">}</span> <span style="color: #0000ff;">@data</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Sort data numerically</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$x_min</span><span style="color: #339933;">=</span>floor<span style="color: #009900;">(</span><span style="color: #0000ff;">$histdata</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">]</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span><span style="color: #009900;">)</span><span style="color: #0000ff;">*$binwidth</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Beginning of x plot range</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$x_max</span><span style="color: #339933;">=</span>ceil<span style="color: #009900;">(</span><span style="color: #0000ff;">$histdata</span><span style="color: #009900;">[</span><span style="color: #0000ff;">@histdata</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span><span style="color: #009900;">)</span><span style="color: #0000ff;">*$binwidth</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># End of x plot range</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@bins</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/map.html"><span style="color: #000066;">map</span></a> <span style="color: #0000ff;">$binwidth</span><span style="color: #339933;">*</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$x_min</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$x_max</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@histo</span><span style="color: #339933;">=</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> x <span style="color: #0000ff;">@bins</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">(</span><span style="color: #0000ff;">@histdata</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;"># Create the histogram plot data</span> <span style="color: #0000ff;">$histo</span><span style="color: #009900;">[</span> <a href="http://perldoc.perl.org/functions/int.html"><span style="color: #000066;">int</span></a><span style="color: #009900;">(</span> <span style="color: #009900;">(</span> <span style="color: #0000ff;">$_</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$x_min</span> <span style="color: #009900;">)</span><span style="color: #339933;">/</span><span style="color: #0000ff;">$binwidth</span> <span style="color: #009900;">)</span> <span style="color: #009900;">]</span><span style="color: #339933;">++;</span> <span style="color: #009900;">}</span>   <span style="color: #666666; font-style: italic;"># Create a Gunplot chart and configure it for histogram plotting</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tmpfile</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">"temp.tmp"</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$chart</span> <span style="color: #339933;">=</span> Chart<span style="color: #339933;">::</span><span style="color: #006600;">Gnuplot</span><span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">(</span> terminal<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">'dumb'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;"># ASCII plotting</span> output <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">$tmpfile</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;"># Gnuplot will plot to this temp file</span> title<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">"Final exam grades distribution"</span><span style="color: #339933;">,</span> xlabel<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">"Grade"</span><span style="color: #339933;">,</span> ylabel<span style="color: #339933;">=&gt;</span><span style="color: #ff0000;">''</span><span style="color: #339933;">,</span> border <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">{</span> sides <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"bottom, left"</span><span style="color: #339933;">,</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> xtics <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">{</span> mirror <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"off"</span><span style="color: #339933;">,</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> ytics <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">{</span> mirror <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"off"</span><span style="color: #339933;">,</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;"># Add the data set in @histo, with bins demarcated by @bins</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dataset</span> <span style="color: #339933;">=</span> Chart<span style="color: #339933;">::</span><span style="color: #006600;">Gnuplot</span><span style="color: #339933;">::</span><span style="color: #006600;">DataSet</span><span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">(</span> using <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"2:xticlabels(1)"</span><span style="color: #339933;">,</span> style <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">"histograms"</span><span style="color: #339933;">,</span> xdata <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@bins</span><span style="color: #339933;">,</span> ydata <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@histo</span><span style="color: #339933;">,</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;"># Plot the histogram - this line plots to the temp file denoted above</span> <span style="color: #0000ff;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">plot2d</span><span style="color: #009900;">(</span><span style="color: #0000ff;">$dataset</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;"># Read the plot back from the temp file...</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$content</span><span style="color: #339933;">;</span> <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">(</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$fh</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'&lt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$tmpfile</span><span style="color: #009900;">)</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">"cannot open file $tmpfile"</span><span style="color: #339933;">;</span> <span style="color: #009900;">{</span> <a href="http://perldoc.perl.org/functions/local.html"><span style="color: #000066;">local</span></a> <span style="color: #0000ff;">$/</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$content</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$fh</span><span style="color: #339933;">&gt;;</span> <span style="color: #009900;">}</span> <span style="color: #666666; font-style: italic;"># ... and print to the terminal</span> <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a><span style="color: #009900;">(</span><span style="color: #0000ff;">$content</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre></div> <p> </p> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/users/simonsilk" typeof="schema:Person" property="schema:name" datatype="">SimonSilk</span></span> <span class="field field--name-created field--type-created field--label-hidden">Mon, 06/04/2018 - 20:40</span> <div class="field field--name-tags field--type-entity-reference field--label-hidden field--entity-reference-target-type-taxonomy-term clearfix field__items"> <div class="field__item"><a href="/taxonomy/term/35" hreflang="en">perl</a></div> <div class="field__item"><a href="/category/tags/programming" hreflang="en">Programming</a></div> <div class="field__item"><a href="/taxonomy/term/36" hreflang="en">scripting</a></div> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=38&amp;2=comment&amp;3=comment" token="88YnWYrN3ujyGI51VWmD8FxR7asdn1I5LJkPLDb8LWM"></drupal-render-placeholder> </section> Mon, 04 Jun 2018 23:40:35 +0000 SimonSilk 38 at http://simonsilk.com