{
"metadata": {
"language": "ruby",
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Nyaplot Tutorial 3: Picking colors suitable for your plots"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nyaplot has an interface to various prepared colorsets. All of those colorsets are provided by [Colorbrewer](http://colorbrewer2.org/) that is licensed under [Apache License Version 2.0](https://github.com/domitry/nyaplot/blob/master/lib/nyaplot/colorbrewer/LICENSE)."
]
},
{
"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\\\", \\\"https://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": [
"Use Nyaplot::Colors#lists to show the lists of prepared colorsets."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.lists"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"[\"Spectral\", \"RdYlGn\", \"RdBu\", \"PiYG\", \"PRGn\", \"RdYlBu\", \"BrBG\", \"RdGy\", \"PuOr\", \"Set2\", \"Accent\", \"Set1\", \"Set3\", \"Dark2\", \"Paired\", \"Pastel2\", \"Pastel1\", \"OrRd\", \"PuBu\", \"BuPu\", \"Oranges\", \"BuGn\", \"YlOrBr\", \"YlGn\", \"Reds\", \"RdPu\", \"Greens\", \"YlGnBu\", \"Purples\", \"GnBu\", \"Greys\", \"YlOrRd\", \"PuRd\", \"Blues\", \"PuBuGn\"]"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try some of those colorsets."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.Pastel2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"
rgb(179,226,205) | rgb(253,205,172) | rgb(203,213,232) | rgb(244,202,228) | rgb(230,245,201) | rgb(255,242,174) | rgb(241,226,204) | rgb(204,204,204) |
---|
| | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 35,
"text": [
"#"
]
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.jet"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(215,48,39) | rgb(244,109,67) | rgb(253,174,97) | rgb(254,224,144) | rgb(255,255,191) | rgb(224,243,248) | rgb(171,217,233) | rgb(116,173,209) | rgb(69,117,180) |
---|
| | | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": [
"#"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.GnBu"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(247,252,240) | rgb(224,243,219) | rgb(204,235,197) | rgb(168,221,181) | rgb(123,204,196) | rgb(78,179,211) | rgb(43,140,190) | rgb(8,104,172) | rgb(8,64,129) |
---|
| | | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 29,
"text": [
"#"
]
}
],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.binary"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(255,255,255) | rgb(240,240,240) | rgb(217,217,217) | rgb(189,189,189) | rgb(150,150,150) | rgb(115,115,115) | rgb(82,82,82) | rgb(37,37,37) | rgb(0,0,0) |
---|
| | | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 27,
"text": [
"#"
]
}
],
"prompt_number": 27
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nyaplot::Colors#seq return random colors that is suitable for sequential data. Similarly, Nyaplot::Colors#div returns colors for diverging data, and Nyaplot::Colors#qual is for qualitative data."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.seq"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(255,245,235) | rgb(254,230,206) | rgb(253,208,162) | rgb(253,174,107) | rgb(253,141,60) | rgb(241,105,19) | rgb(217,72,1) | rgb(166,54,3) | rgb(127,39,4) |
---|
| | | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"#"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.div"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(213,62,79) | rgb(244,109,67) | rgb(253,174,97) | rgb(254,224,139) | rgb(255,255,191) | rgb(230,245,152) | rgb(171,221,164) | rgb(102,194,165) | rgb(50,136,189) |
---|
| | | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 19,
"text": [
"#"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.qual"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(166,206,227) | rgb(31,120,180) | rgb(178,223,138) | rgb(51,160,44) | rgb(251,154,153) | rgb(227,26,28) | rgb(253,191,111) | rgb(255,127,0) | rgb(202,178,214) |
---|
| | | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
"#"
]
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Those methods can have one argument to specify required number of colors."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.seq(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(224,243,219) | rgb(168,221,181) | rgb(67,162,202) |
---|
| | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"#"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Colors.Spectral(11)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(158,1,66) | rgb(213,62,79) | rgb(244,109,67) | rgb(253,174,97) | rgb(254,224,139) | rgb(255,255,191) | rgb(230,245,152) | rgb(171,221,164) | rgb(102,194,165) | rgb(50,136,189) | rgb(94,79,162) |
---|
| | | | | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
"#"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use Nyaplot::Color to try your favorite color (Credits: [fatalflaws](http://www.colourlovers.com/lover/fatalflaws/loveNote))"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Nyaplot::Color.new([\"#DEDEDE\", \"#ACACAC\", \"#1F141C\", \"#4A173D\", \"#8C547E\"])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"#DEDEDE | #ACACAC | #1F141C | #4A173D | #8C547E |
---|
| | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 45,
"text": [
"#"
]
}
],
"prompt_number": 45
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then try those colors in some plots."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plot = Nyaplot::Plot.new\n",
"bar = plot.add(:bar, ['Persian', 'Maine Coon', 'American Shorthair'], [10,20,30])\n",
"plot.show"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"\"\\n\\n\""
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"colors = Nyaplot::Colors.qual(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(179,226,205) | rgb(253,205,172) | rgb(203,213,232) |
---|
| | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"text": [
"#"
]
}
],
"prompt_number": 25
},
{
"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": 26,
"text": [
"\"\\n\\n\""
]
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x=[]; y=[]; fill=[]\n",
"-5.step(5, 1) do |i|\n",
" -5.step(5, 1) do |j|\n",
" x.push(i)\n",
" y.push(j)\n",
" val = Math.sin(Math.sqrt(i*i+j*j))/Math.sqrt(i*i+j*j)\n",
" fill.push((val.nan? ? 0 : val))\n",
" end\n",
"end\n",
"\n",
"plot2 = Nyaplot::Plot.new\n",
"hm = plot2.add(:heatmap, x, y, fill)\n",
"hm.width(1)\n",
"hm.height(1)\n",
"plot2.legend(true)\n",
"plot2.show"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 43,
"text": [
"\"\\n\\n\""
]
}
],
"prompt_number": 43
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"colors = Nyaplot::Colors.GnBu"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"rgb(247,252,240) | rgb(224,243,219) | rgb(204,235,197) | rgb(168,221,181) | rgb(123,204,196) | rgb(78,179,211) | rgb(43,140,190) | rgb(8,104,172) | rgb(8,64,129) |
---|
| | | | | | | | |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 39,
"text": [
"#"
]
}
],
"prompt_number": 39
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"hm.color(colors)\n",
"plot2.show"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 41,
"text": [
"\"\\n\\n\""
]
}
],
"prompt_number": 41
}
],
"metadata": {}
}
]
}