{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating a Histogram with statsample\n", "\n", "This example demonstrates how a histogram can be created with statsample.\n", "\n", "The 'histogram' function creates a histogram by using the Statsample::Graph::Histogram \n", "class. This class accepts data in a Daru::Vector (as created by `rnorm`).\n", "\n", "A line showing normal distribution can be drawn by setting the `:line_normal_distribution`\n", "option to *true*." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "if(window['d3'] === undefined ||\n", " window['Nyaplot'] === undefined){\n", " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", "\n", "\n", "\n", " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", "\n", " require.config({paths: path, shim:shim});\n", "\n", "\n", "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", "\n", "\tvar script = d3.select(\"head\")\n", "\t .append(\"script\")\n", "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", "\t .attr(\"async\", true);\n", "\n", "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", "\n", "\n", "\t var event = document.createEvent(\"HTMLEvents\");\n", "\t event.initEvent(\"load_nyaplot\",false,false);\n", "\t window.dispatchEvent(event);\n", "\t console.log('Finished loading Nyaplotjs');\n", "\n", "\t};\n", "\n", "\n", "});});\n", "}\n" ], "text/plain": [ "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " 0\n", " 20\n", " 40\n", " 60\n", " 80\n", " 100\n", " 120\n", " 140\n", " 160\n", " 180\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " −80\n", " −60\n", " −40\n", " −20\n", " 0\n", " 20\n", " 40\n", " 60\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ], "text/plain": [ "#\"Histograma ()\", :width=>400, :height=>300, :margin_top=>10, :margin_bottom=>20, :margin_left=>30, :margin_right=>20, :minimum_x=>nil, :maximum_x=>nil, :minimum_y=>nil, :maximum_y=>nil, :bins=>nil, :line_normal_distribution=>true}, @name=\"Histograma ()\", @width=400, @height=300, @margin_top=10, @margin_bottom=20, @margin_left=30, @margin_right=20, @minimum_x=-80.0, @maximum_x=70.0, @minimum_y=0, @maximum_y=180.0, @bins=54, @line_normal_distribution=true, @data=\n", "#\n", " nil\n", " 0 -27.415128518696843\n", " 1 20.414946668543656\n", " 2 -11.465160151139251\n", " 3 -10.669292096044256\n", " 4 -31.822455566553614\n", " 5 -15.970476997382004\n", " 6 28.82730021926586\n", " 7 10.644442868376617\n", " 8 24.37670022828611\n", " 9 1.9399065127446649\n", " 10 7.904088293193112\n", " 11 -23.775213234392098\n", " 12 -21.06770241613534\n", " 13 -2.405593358605801\n", " 14 -65.40839665902821\n", " 15 -23.86625428519426\n", " 16 18.72778311420171\n", " ... ...\n", ", @mean=0.219926125946352, @sd=19.864921286875045, @hist=#, @x_scale=#], @type=:number, @n=true, @f=#, @g=#, @tick_format=#>, @y_scale=#], @type=:number, @n=false, @f=#, @g=#, @tick_format=#>>" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'statsample'\n", "include Statsample::Shorthand\n", "\n", "h = histogram(rnorm(3000,0,20), line_normal_distribution: true)\n", "h" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.2.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.2.1" } }, "nbformat": 4, "nbformat_minor": 0 }