{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Note : Examples are taken from daru : http://nbviewer.jupyter.org/github/SciRuby/sciruby-notebooks/blob/master/Data%20Analysis/Plotting/Visualization.ipynb" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions.\n", "\n", "Install the mechanize gem version ~>2.7.5 for using mechanize functions.\n" ] }, { "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\":\"http://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\", \"http://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\\\":\\\"http://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\\\", \\\"http://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": { "text/plain": [ "true" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'daru/view'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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\":\"http://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\", \"http://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\\\":\\\"http://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\\\", \\\"http://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": { "text/plain": [ ":nyaplot" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Daru::View.plotting_library = :nyaplot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Categorical Vector Visualization" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ " Daru::Vector(6):category \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", "
0a
1a
2a
3b
4b
5c
" ], "text/plain": [ "#\n", " 0 a\n", " 1 a\n", " 2 a\n", " 3 b\n", " 4 b\n", " 5 c" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv = Daru::Vector.new [:a, :a, :a, :b, :b, :c], type: :category" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bar graph\n", "\n", "##### 1. Frequency (count)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#\n", " 0 a\n", " 1 a\n", " 2 a\n", " 3 b\n", " 4 b\n", " 5 c, @options={:type=>:bar, :method=>:count}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"d2703cc9-f28f-4c6a-adcf-3be6187d1969\"}, @xrange=[:a, :b, :c], @yrange=[0, 3]>], :options=>{}}>>" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph1 = Daru::View::Plot.new(dv, type: :bar)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Nyaplot::Plot" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph1.chart.class" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"d2703cc9-f28f-4c6a-adcf-3be6187d1969\"}, @xrange=[:a, :b, :c], @yrange=[0, 3]>], :options=>{:width=>700, :xrange=>[:a, :b, :c], :yrange=>[0, 3]}}>], :data=>{\"d2703cc9-f28f-4c6a-adcf-3be6187d1969\"=>#:a, :data1=>3}, {:data0=>:b, :data1=>2}, {:data0=>:c, :data1=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "#" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph1.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv = Daru::Vector.new ['III']*10 + ['II']*5 + ['I']*5, type: :category, categories: ['I', 'II', 'III']\n", "dv.type" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#\n", " 0 III\n", " 1 III\n", " 2 III\n", " 3 III\n", " 4 III\n", " 5 III\n", " 6 III\n", " 7 III\n", " 8 III\n", " 9 III\n", " 10 II\n", " 11 II\n", " 12 II\n", " 13 II\n", " 14 II\n", " ... ..., @options={:type=>:bar, :method=>:count}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"5c7ce333-5a85-4667-99dc-70f28f8c1f03\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 10]>], :options=>{}}>>" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2 = Daru::View::Plot.new(dv, type: :bar)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Daru::View::Plot" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2.class" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Nyaplot::Plot" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2.chart.class" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"5c7ce333-5a85-4667-99dc-70f28f8c1f03\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 10]>], :options=>{:x_label=>\"Cat\", :y_label=>\"Frequency\", :width=>700, :xrange=>[\"I\", \"II\", \"III\"], :yrange=>[0, 10]}}>" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Nyaplot methods will work.Since #chart is Nyaplot::Plot class object\n", "bar_graph2.chart.x_label 'Cat'\n", "bar_graph2.chart.y_label 'Frequency'" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"5c7ce333-5a85-4667-99dc-70f28f8c1f03\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 10]>], :options=>{:x_label=>\"Cat\", :y_label=>\"Frequency\", :width=>700, :xrange=>[\"I\", \"II\", \"III\"], :yrange=>[0, 10]}}>" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2.chart # or bar_graph2.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Percentage" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#\n", " 0 III\n", " 1 III\n", " 2 III\n", " 3 III\n", " 4 III\n", " 5 III\n", " 6 III\n", " 7 III\n", " 8 III\n", " 9 III\n", " 10 II\n", " 11 II\n", " 12 II\n", " 13 II\n", " 14 II\n", " ... ..., @options={:type=>:bar, :method=>:percentage}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"4cfa1107-b2e6-49bb-9c04-375158cfe800\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 50.0]>], :options=>{:yrange=>[0, 100]}}>>" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2_per = Daru::View::Plot.new(dv, type: :bar, method: :percentage)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"4cfa1107-b2e6-49bb-9c04-375158cfe800\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 50.0]>], :options=>{:yrange=>[0, 100], :x_label=>\"Categories\", :y_label=>\"Percentage (%)\", :width=>700, :xrange=>[\"I\", \"II\", \"III\"]}}>], :data=>{\"4cfa1107-b2e6-49bb-9c04-375158cfe800\"=>#\"I\", :data1=>25.0}, {:data0=>\"II\", :data1=>25.0}, {:data0=>\"III\", :data1=>50.0}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "#" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2_per.chart.x_label 'Categories'\n", "bar_graph2_per.chart.y_label 'Percentage (%)'\n", "bar_graph2_per.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Fraction" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#\n", " 0 III\n", " 1 III\n", " 2 III\n", " 3 III\n", " 4 III\n", " 5 III\n", " 6 III\n", " 7 III\n", " 8 III\n", " 9 III\n", " 10 II\n", " 11 II\n", " 12 II\n", " 13 II\n", " 14 II\n", " ... ..., @options={:type=>:bar, :method=>:fraction}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"9d2ff911-93f1-4a5e-b266-e403eafe49b5\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 0.5]>], :options=>{:yrange=>[0, 1]}}>>" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph2_frac = Daru::View::Plot.new(dv, type: :bar, method: :fraction)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"9d2ff911-93f1-4a5e-b266-e403eafe49b5\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 0.5]>], :options=>{:yrange=>[0, 1], :y_label=>\"Fraction\", :width=>700, :xrange=>[\"I\", \"II\", \"III\"]}}>], :data=>{\"9d2ff911-93f1-4a5e-b266-e403eafe49b5\"=>#\"I\", :data1=>0.25}, {:data0=>\"II\", :data1=>0.25}, {:data0=>\"III\", :data1=>0.5}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "#" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# bar_graph2_frac.chart.x_label 'Categories'\n", "bar_graph2_frac.chart.y_label 'Fraction'\n", "bar_graph2_frac.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Categorical data visualization in Dataframe\n", "### Bar Graph" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = Daru::DataFrame.new({\n", " a: [1, 2, 4, -2, 5, 23, 0],\n", " b: [3, 1, 3, -6, 2, 1, 0],\n", " c: ['I', 'II', 'I', 'III', 'I', 'III', 'II']\n", " })\n", "df.to_category :c\n", "df[:c].type" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#\n", " a b c\n", " 0 1 3 I\n", " 1 2 1 II\n", " 2 4 3 I\n", " 3 -2 -6 III\n", " 4 5 2 I\n", " 5 23 1 III\n", " 6 0 0 II, @options={:type=>:bar, :x=>:c}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#[#:bar, :options=>{:value=>:c}, :data=>\"71214b09-e3c4-48cf-95d1-5000d0600ded\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 7]>], :options=>{}}>>" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph3 = Daru::View::Plot.new(df, type: :bar, x: :c)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:value=>:c}, :data=>\"71214b09-e3c4-48cf-95d1-5000d0600ded\"}, @xrange=[\"I\", \"II\", \"III\"], @yrange=[0, 7]>], :options=>{:width=>700, :xrange=>[\"I\", \"II\", \"III\"], :yrange=>[0, 7]}}>], :data=>{\"71214b09-e3c4-48cf-95d1-5000d0600ded\"=>#1, :b=>3, :c=>\"I\"}, {:a=>2, :b=>1, :c=>\"II\"}, {:a=>4, :b=>3, :c=>\"I\"}, {:a=>-2, :b=>-6, :c=>\"III\"}, {:a=>5, :b=>2, :c=>\"I\"}, {:a=>23, :b=>1, :c=>\"III\"}, {:a=>0, :b=>0, :c=>\"II\"}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "#" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_graph3.show_in_iruby" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scatter plot categorized by categorical variable¶\n", "#### Plots can be categorized by\n", "\n", "- Color\n", "- Size\n", "- Shape" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = Daru::DataFrame.new({\n", " a: [1, 2, 4, -2, 5, 23, 0],\n", " b: [3, 1, 3, -6, 2, 1, 0],\n", " c: ['I', 'II', 'I', 'III', 'I', 'III', 'II']\n", " })\n", "df.to_category :c\n", "df[:c].type" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#\n", " a b c\n", " 0 1 3 I\n", " 1 2 1 II\n", " 2 4 3 I\n", " 3 -2 -6 III\n", " 4 5 2 I\n", " 5 23 1 III\n", " 6 0 0 II, @options={:type=>:scatter, :x=>:a, :y=>:b, :categorized=>{:by=>:c, :method=>:color}}, @adapter=Daru::View::Adapter::NyaplotAdapter, @chart=#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>\"rgb(141,211,199)\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"00976c4d-e2ef-4329-8154-3c8442b6494c\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>\"rgb(255,255,179)\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"6f96eb03-6646-4b90-9396-375d8821d5cb\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>\"rgb(190,186,218)\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"36e2cd12-ae08-4302-8438-eeb6fd1b29f1\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true}}>>" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_1 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color})" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>\"rgb(141,211,199)\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"00976c4d-e2ef-4329-8154-3c8442b6494c\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>\"rgb(255,255,179)\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"6f96eb03-6646-4b90-9396-375d8821d5cb\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>\"rgb(190,186,218)\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"36e2cd12-ae08-4302-8438-eeb6fd1b29f1\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-2, 23], :yrange=>[-6, 3]}}>], :data=>{\"00976c4d-e2ef-4329-8154-3c8442b6494c\"=>#1, :b=>3}, {:a=>4, :b=>3}, {:a=>5, :b=>2}]>, \"6f96eb03-6646-4b90-9396-375d8821d5cb\"=>#2, :b=>1}, {:a=>0, :b=>0}]>, \"36e2cd12-ae08-4302-8438-eeb6fd1b29f1\"=>#-2, :b=>-6}, {:a=>23, :b=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "#" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_1.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>\"rgb(141,211,199)\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"00976c4d-e2ef-4329-8154-3c8442b6494c\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>\"rgb(255,255,179)\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"6f96eb03-6646-4b90-9396-375d8821d5cb\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>\"rgb(190,186,218)\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"36e2cd12-ae08-4302-8438-eeb6fd1b29f1\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_1.chart.xrange [-10, 10]\n", "scatter_1.chart.yrange [-10, 10]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :shape=>\"circle\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"a135424e-1568-4148-b5b5-cf6537476e36\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :shape=>\"triangle-up\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"1d41dabb-cc54-44b3-b2f4-95169f2fc67a\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :shape=>\"diamond\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"3aca8cf3-7b08-440b-924f-6e81140ac32b\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-2, 23], :yrange=>[-6, 3]}}>], :data=>{\"a135424e-1568-4148-b5b5-cf6537476e36\"=>#1, :b=>3}, {:a=>4, :b=>3}, {:a=>5, :b=>2}]>, \"1d41dabb-cc54-44b3-b2f4-95169f2fc67a\"=>#2, :b=>1}, {:a=>0, :b=>0}]>, \"3aca8cf3-7b08-440b-924f-6e81140ac32b\"=>#-2, :b=>-6}, {:a=>23, :b=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "#" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_2 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :shape})\n", "scatter_2.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :shape=>\"circle\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"a135424e-1568-4148-b5b5-cf6537476e36\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :shape=>\"triangle-up\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"1d41dabb-cc54-44b3-b2f4-95169f2fc67a\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :shape=>\"diamond\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"3aca8cf3-7b08-440b-924f-6e81140ac32b\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_2.chart.xrange [-10, 10]\n", "scatter_2.chart.yrange [-10, 10]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>:red, :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"92c6986e-8be7-4634-bfc6-0d0e41245b4e\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>:blue, :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"085cdbbc-695a-41cc-b5e4-957daca39adb\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>:green, :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"1dc04551-c809-4f8d-b6dd-1a1bfd6155df\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-2, 23], :yrange=>[-6, 3]}}>], :data=>{\"92c6986e-8be7-4634-bfc6-0d0e41245b4e\"=>#1, :b=>3}, {:a=>4, :b=>3}, {:a=>5, :b=>2}]>, \"085cdbbc-695a-41cc-b5e4-957daca39adb\"=>#2, :b=>1}, {:a=>0, :b=>0}]>, \"1dc04551-c809-4f8d-b6dd-1a1bfd6155df\"=>#-2, :b=>-6}, {:a=>23, :b=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :shape=>\"circle\", :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"a135424e-1568-4148-b5b5-cf6537476e36\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :shape=>\"triangle-up\", :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"1d41dabb-cc54-44b3-b2f4-95169f2fc67a\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :shape=>\"diamond\", :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"3aca8cf3-7b08-440b-924f-6e81140ac32b\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_3 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :color, color: [:red, :blue, :green]})\n", "scatter_3.show_in_iruby\n", "scatter_2.chart.xrange [-10, 10]\n", "scatter_2.chart.yrange [-10, 10]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :size=>300, :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"c8b2d2fb-b456-4d1a-9344-dbf6e92cb852\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :size=>600, :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"55d7cef5-b545-4e13-97a4-2d087786bdb3\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :size=>900, :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"0ad59131-74c8-4271-82be-bae6ffb8bba4\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-2, 23], :yrange=>[-6, 3]}}>], :data=>{\"c8b2d2fb-b456-4d1a-9344-dbf6e92cb852\"=>#1, :b=>3}, {:a=>4, :b=>3}, {:a=>5, :b=>2}]>, \"55d7cef5-b545-4e13-97a4-2d087786bdb3\"=>#2, :b=>1}, {:a=>0, :b=>0}]>, \"0ad59131-74c8-4271-82be-bae6ffb8bba4\"=>#-2, :b=>-6}, {:a=>23, :b=>1}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :size=>300, :tooltip_contents=>[\"I\", \"I\", \"I\"]}, :data=>\"c8b2d2fb-b456-4d1a-9344-dbf6e92cb852\"}, @xrange=[1, 5], @yrange=[2, 3]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :size=>600, :tooltip_contents=>[\"II\", \"II\"]}, :data=>\"55d7cef5-b545-4e13-97a4-2d087786bdb3\"}, @xrange=[0, 2], @yrange=[0, 1]>, #:scatter, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :size=>900, :tooltip_contents=>[\"III\", \"III\"]}, :data=>\"0ad59131-74c8-4271-82be-bae6ffb8bba4\"}, @xrange=[-2, 23], @yrange=[-6, 1]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter_4 = Daru::View::Plot.new(df, type: :scatter, x: :a, y: :b, categorized: {by: :c, method: :size, size: [300, 600, 900]})\n", "scatter_4.show_in_iruby\n", "scatter_4.chart.xrange [-10, 10]\n", "scatter_4.chart.yrange [-10, 10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Line plot categorized by categorical variable\n", "It works similar to Scatter plot above and all options are same except that there's no categorization by size but instead there is categorization by stroke_width in line plots." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":category" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = Daru::DataFrame.new({\n", " a: [1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " b: [2, 4, 6, 1, 3, 5, 6, 4, 3],\n", " c: ['I']*3 + ['II']*3 + ['III']*3\n", " })\n", "df.to_category :c\n", "df[:c].type" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:line, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :color=>\"rgb(127,201,127)\"}, :data=>\"469e8205-cf0c-4de4-9a37-1ca9cd140aea\"}, @xrange=[1, 3], @yrange=[2, 6]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :color=>\"rgb(190,174,212)\"}, :data=>\"4b61beaa-eec4-4a1a-8046-da2c94bddaa6\"}, @xrange=[4, 6], @yrange=[1, 5]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :color=>\"rgb(253,192,134)\"}, :data=>\"90abd6d7-28e5-427a-bf9d-934a57601da2\"}, @xrange=[7, 9], @yrange=[3, 6]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[1, 9], :yrange=>[1, 6]}}>], :data=>{\"469e8205-cf0c-4de4-9a37-1ca9cd140aea\"=>#1, :b=>2}, {:a=>2, :b=>4}, {:a=>3, :b=>6}]>, \"4b61beaa-eec4-4a1a-8046-da2c94bddaa6\"=>#4, :b=>1}, {:a=>5, :b=>3}, {:a=>6, :b=>5}]>, \"90abd6d7-28e5-427a-bf9d-934a57601da2\"=>#7, :b=>6}, {:a=>8, :b=>4}, {:a=>9, :b=>3}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "#" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line_1 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :color})\n", "line_1.show_in_iruby" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:line, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :stroke_width=>2}, :data=>\"324905ca-9065-419d-bedd-5e971820960d\"}, @xrange=[1, 3], @yrange=[2, 6]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :stroke_width=>4}, :data=>\"be3c5176-9ff4-4b6d-a50b-8749075892ff\"}, @xrange=[4, 6], @yrange=[1, 5]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :stroke_width=>6}, :data=>\"29b2327f-24a6-4bbc-89bb-813f5487219e\"}, @xrange=[7, 9], @yrange=[3, 6]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[1, 9], :yrange=>[1, 6]}}>], :data=>{\"324905ca-9065-419d-bedd-5e971820960d\"=>#1, :b=>2}, {:a=>2, :b=>4}, {:a=>3, :b=>6}]>, \"be3c5176-9ff4-4b6d-a50b-8749075892ff\"=>#4, :b=>1}, {:a=>5, :b=>3}, {:a=>6, :b=>5}]>, \"29b2327f-24a6-4bbc-89bb-813f5487219e\"=>#7, :b=>6}, {:a=>8, :b=>4}, {:a=>9, :b=>3}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#:line, :options=>{:x=>:a, :y=>:b, :title=>\"I\", :stroke_width=>2}, :data=>\"324905ca-9065-419d-bedd-5e971820960d\"}, @xrange=[1, 3], @yrange=[2, 6]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"II\", :stroke_width=>4}, :data=>\"be3c5176-9ff4-4b6d-a50b-8749075892ff\"}, @xrange=[4, 6], @yrange=[1, 5]>, #:line, :options=>{:x=>:a, :y=>:b, :title=>\"III\", :stroke_width=>6}, :data=>\"29b2327f-24a6-4bbc-89bb-813f5487219e\"}, @xrange=[7, 9], @yrange=[3, 6]>], :options=>{:legend=>true, :zoom=>true, :width=>800, :xrange=>[-10, 10], :yrange=>[-10, 10]}}>" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line_2 = Daru::View::Plot.new(df, type: :line, x: :a, y: :b, categorized: {by: :c, method: :stroke_width})\n", "line_2.show_in_iruby\n", "line_2.chart.xrange [-10, 10]\n", "line_2.chart.yrange [-10, 10]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.4.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.4.1" } }, "nbformat": 4, "nbformat_minor": 2 }