{ "metadata": { "language": "ruby", "name": "", "signature": "sha256:f7231d1fc7d414aec16543bcfafb53d43e59653868b649e802c90b2b2040c48b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Nyaplot Tutorial 1: Getting Started with Nyaplot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook is written to demonstrate Nyaplot. \n", "Nyaplot is an interactive plots generator for Ruby users.\n", "It can create various types of plots, from basic ones like scatter or line, to more practical ones like 3D charts and Map visualization.\n", "This tutorial will introduce the basic use of Nyaplot." ] }, { "cell_type": "code", "collapsed": false, "input": [ "require 'nyaplot'" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"http://d3js.org/d3.v3.min\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');\\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": "pyout", "prompt_number": 1, "text": [ "true" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make some diagrams with Nyaplot. The code shown below is the minimal code to create bar chart." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot = Nyaplot::Plot.new\n", "bar = plot.add(:bar, ['Persian', 'Maine Coon', 'American Shorthair'], [10,20,30])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"229ade56-2c0d-46d7-a378-d356bccdf2c1\"}, @xrange=[\"Persian\", \"Maine Coon\", \"American Shorthair\"], @yrange=[0, 30]>" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's all. Plot will appear on the notebook using `Nyaplot::Plot#show`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot.show" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"229ade56-2c0d-46d7-a378-d356bccdf2c1\"}, @xrange=[\"Persian\", \"Maine Coon\", \"American Shorthair\"], @yrange=[0, 30]>], :options=>{:width=>700, :xrange=>[\"Persian\", \"Maine Coon\", \"American Shorthair\"], :yrange=>[0, 30]}}>], :data=>{\"229ade56-2c0d-46d7-a378-d356bccdf2c1\"=>#\"Persian\", :data1=>10}, {:data0=>\"Maine Coon\", :data1=>20}, {:data0=>\"American Shorthair\", :data1=>30}]>}, :extension=>[]}>" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's looks good, but I noticed that x and y labels have no sence. So fix them to be correct name." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot.x_label(\"Species\")\n", "plot.y_label(\"Number\")\n", "plot.show" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"229ade56-2c0d-46d7-a378-d356bccdf2c1\"}, @xrange=[\"Persian\", \"Maine Coon\", \"American Shorthair\"], @yrange=[0, 30]>], :options=>{:width=>700, :xrange=>[\"Persian\", \"Maine Coon\", \"American Shorthair\"], :yrange=>[0, 30], :x_label=>\"Species\", :y_label=>\"Number\"}}>], :data=>{\"229ade56-2c0d-46d7-a378-d356bccdf2c1\"=>#\"Persian\", :data1=>10}, {:data0=>\"Maine Coon\", :data1=>20}, {:data0=>\"American Shorthair\", :data1=>30}]>}, :extension=>[]}>" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Good. Nyaplot::Plot has various options except `x_label` and `y_label`, and you can find them by reading documents and some other notebooks. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next the color is not so pretty, so let's try to change colors. Nyaplot has various default colorset and it's quite easy to find your favorite one." ] }, { "cell_type": "code", "collapsed": false, "input": [ "colors = Nyaplot::Colors.qual" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
rgb(251,180,174)rgb(179,205,227)rgb(204,235,197)rgb(222,203,228)rgb(254,217,166)rgb(255,255,204)rgb(229,216,189)rgb(253,218,236)rgb(242,242,242)
         
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "#" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Nyaplot::Colors#qual` finds random colorset for qualitative data and it may be usable when creating bar chart or scatter. To leran more about `Nyaplot::Colors`, visit [Tutorial3](http://nbviewer.ipython.org/github/domitry/nyaplot/blob/master/examples/notebook/Colors.ipynb). \n", "Then fill bar charts in the selected color." ] }, { "cell_type": "code", "collapsed": false, "input": [ "bar.color(colors)\n", "plot.show" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\", :color=>#}, :data=>\"229ade56-2c0d-46d7-a378-d356bccdf2c1\"}, @xrange=[\"Persian\", \"Maine Coon\", \"American Shorthair\"], @yrange=[0, 30]>], :options=>{:width=>700, :xrange=>[\"Persian\", \"Maine Coon\", \"American Shorthair\"], :yrange=>[0, 30], :x_label=>\"Species\", :y_label=>\"Number\"}}>], :data=>{\"229ade56-2c0d-46d7-a378-d356bccdf2c1\"=>#\"Persian\", :data1=>10}, {:data0=>\"Maine Coon\", :data1=>20}, {:data0=>\"American Shorthair\", :data1=>30}]>}, :extension=>[]}>" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The example above, I prepared array and use `Nyaplot::Plot.add` to add bar chart to the plot. But Nyaplot has more useful API. \n", "Let's make the same plot using `Nyaplot::DataFrame`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "df = Nyaplot::DataFrame.new({species: ['Persian', 'Maine Coon', 'American Shorthair'], number: [10,20,30]})" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
speciesnumber
Persian10
Maine Coon20
American Shorthair30
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "#\"Persian\", :number=>10}, {:species=>\"Maine Coon\", :number=>20}, {:species=>\"American Shorthair\", :number=>30}]>" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `Nyaplot::Plot#add_with_df` instead of `#add` to create plot from DataFrame." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot2 = Nyaplot::Plot.new\n", "plot2.add_with_df(df, :bar, :species, :number) # x-> column :species, y-> column :number\n", "plot2.show" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "#[#[#:bar, :options=>{:x=>:species, :y=>:number}, :data=>\"75546386-7dd5-4e6d-9aa5-7d903dfb7ffc\"}, @xrange=[\"Persian\", \"Maine Coon\", \"American Shorthair\"], @yrange=[0, 30]>], :options=>{:width=>700, :xrange=>[\"Persian\", \"Maine Coon\", \"American Shorthair\"], :yrange=>[0, 30]}}>], :data=>{\"75546386-7dd5-4e6d-9aa5-7d903dfb7ffc\"=>#\"Persian\", :number=>10}, {:species=>\"Maine Coon\", :number=>20}, {:species=>\"American Shorthair\", :number=>30}]>}, :extension=>[]}>" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "DataFrame is useful when you create more complicated plots. Go to [Tutorial2](http://nbviewer.ipython.org/github/domitry/Nyaplot/blob/master/examples/notebook/Interaction_with_DataFrame.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tutorial was written by Naoki Nishida, as a part of products in GSoC 2014." ] } ], "metadata": {} } ] }