Skip to content

Commit

Permalink
Add example notebooks for Elements (#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored and jlstevens committed May 31, 2017
1 parent 14a380c commit be31282
Show file tree
Hide file tree
Showing 62 changed files with 6,415 additions and 16 deletions.
131 changes: 131 additions & 0 deletions examples/elements/bokeh/Area.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
"<dl class=\"dl-horizontal\">\n",
" <dt>Title</dt> <dd> Area Element</dd>\n",
" <dt>Dependencies</dt> <dd>Bokeh</dd>\n",
" <dt>Backends</dt> <dd>[Bokeh](./Area.ipynb)</dd> <dd>[Matplotlib](../matplotlib/Area.ipynb)</dd>\n",
"</dl>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"hv.notebook_extension('bokeh')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"``Area`` elements are ``Curve`` elements where the area below the line is filled. Like ``Curve`` elements, ``Area`` elements are used to display the development of quantitative values over an interval or time period. ``Area`` Elements may also be stacked to display multiple data series in a cumulative fashion over the value dimension.\n",
"\n",
"The data of an ``Area`` Element should be tabular with one key dimension representing the samples over the interval or the timeseries and one or two value dimensions. A single value dimension will fill the area between the curve and the x-axis, while two value dimensions will fill the area between the curves. See the [Columnar Data Tutorial](../Tutorials/Columnar_Data.ipynb) for supported data formats, which include arrays, pandas dataframes and dictionaries of arrays."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Area under the curve\n",
"\n",
"By default the Area Element draws just the area under the curve, i.e. the region between the curve and the origin."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"xs = np.linspace(0, np.pi*4, 40)\n",
"hv.Area((xs, np.sin(xs)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Area between curves\n",
"\n",
"When supplied a second value dimension the area is defined as the area between two curves."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"X = np.linspace(0,3,200)\n",
"Y = X**2 + 3\n",
"Y2 = np.exp(X) + 2\n",
"Y3 = np.cos(X)\n",
"hv.Area((X, Y, Y2), vdims=['y', 'y2']) * hv.Area((X, Y, Y3), vdims=['y', 'y3'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Stacked areas \n",
"\n",
"Areas are also useful to visualize multiple variables changing over time, but in order to be able to compare them the areas need to be stacked. To do this, use the ``Area.stack`` classmethod to stack multiple ``Area`` elements in an (Nd)Overlay.\n",
"\n",
"In this example we will generate a set of 5 arrays representing percentages and create an ``Overlay`` of them. Then we simply call the ``stack`` method with this overlay to get a stacked area chart.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"values = np.random.rand(5, 20)\n",
"percentages = (values/values.sum(axis=0)).T*100\n",
"\n",
"overlay = hv.Overlay([hv.Area(percentages[:, i], vdims=[hv.Dimension('value', unit='%')]) for i in range(5)])\n",
"overlay + hv.Area.stack(overlay)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:science]",
"language": "python",
"name": "conda-env-science-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
116 changes: 116 additions & 0 deletions examples/elements/bokeh/Bars.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
"<dl class=\"dl-horizontal\">\n",
" <dt>Title</dt> <dd> Bars Element</dd>\n",
" <dt>Dependencies</dt> <dd>Bokeh</dd>\n",
" <dt>Backends</dt> <dd>[Bokeh](./Bars.ipynb)</dd> <dd>[Matplotlib](../matplotlib/Bars.ipynb)</dd>\n",
"</dl>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"hv.notebook_extension('bokeh')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``Bars`` Element uses bars to show discrete, numerical comparisons across categories. One axis of the chart shows the specific categories being compared and the other axis represents a continuous value.\n",
"\n",
"Bars may also be stacked by supplying a second key dimensions representing sub-categories. Therefore the ``Bars`` Element expects a tabular data format with one or two key dimensions and one value dimension. See the [Columnar Data Tutorial](../Tutorials/Columnar_Data.ipynb) for supported data formats, which include arrays, pandas dataframes and dictionaries of arrays."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"data = [('one',8),('two', 10), ('three', 16), ('four', 8), ('five', 4), ('six', 1)]\n",
"bars = hv.Bars(data, kdims=[hv.Dimension('Car occupants')], vdims=['Count'])\n",
"bars"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can 'slice' a ``Bars`` element by selecting categories as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"bars[['one', 'two', 'three']] + bars[['four', 'five', 'six']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"``Bars`` support stacking just like the ``Area`` element as well as grouping by a second key dimension. To activate grouping and stacking set the ``group_index`` or ``stack_index`` to the dimension name or dimension index:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%opts Bars.Grouped [group_index='Group'] Bars.Stacked [stack_index='Group']\n",
"from itertools import product\n",
"np.random.seed(3)\n",
"index, groups = ['A', 'B'], ['a', 'b']\n",
"keys = product(index, groups)\n",
"bars = hv.Bars([k+(np.random.rand()*100.,) for k in keys],\n",
" kdims=['Index', 'Group'], vdims=['Count'])\n",
"bars.relabel(group='Grouped') + bars.relabel(group='Stacked')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:science]",
"language": "python",
"name": "conda-env-science-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
89 changes: 89 additions & 0 deletions examples/elements/bokeh/Bounds.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
"<dl class=\"dl-horizontal\">\n",
" <dt>Title</dt> <dd> Bounds Element</dd>\n",
" <dt>Dependencies</dt> <dd>Bokeh</dd>\n",
" <dt>Backends</dt> <dd>[Bokeh](./Bounds.ipynb)</dd> <dd>[Matplotlib](../matplotlib/Bounds.ipynb)</dd>\n",
"</dl>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"hv.notebook_extension('bokeh')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A ``Bounds`` element is an box shaped annotation that is specified as a tuple in (left, bottom, right, top) format:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%opts Bounds (color='orange' line_width=6)\n",
"penguins = hv.RGB.load_image('../../../doc/assets/penguins.png')\n",
"penguins * hv.Bounds((-0.15, -0.4, 0.2, 0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is useful for denoting a region of interest defined by some bounds (such as a slice) which is unlike the [``Box``](./Box.ipynb) element which is useful for drawing a box at a specific location. Here we show the a slice of the ``RGB`` element (green channel only) outlined by the bounds. Note that the values in the ``Bounds`` tuple are the same ones as used in the slice:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"penguins * penguins[-0.15:0.2, -0.4:0, 'G'] * hv.Bounds((-0.15, -0.4, 0.2, 0))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit be31282

Please sign in to comment.