Skip to content

Commit

Permalink
Added a groupby argument to the histogram operation (#1725)
Browse files Browse the repository at this point in the history
* Added a groupby argument to the histogram operation

* Added examples for grouped histograms
  • Loading branch information
philippjfr authored and jlstevens committed Jul 15, 2017
1 parent 5c38ec9 commit aff5715
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 2 deletions.
66 changes: 66 additions & 0 deletions examples/gallery/demos/bokeh/autompg_histogram.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Most examples work across multiple plotting backends, this example is also available for:\n",
"\n",
"* [Matplotlib - autompg_histogram](../matplotlib/autompg_histogram.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import holoviews as hv\n",
"hv.extension('bokeh','matplotlib')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Declaring data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from bokeh.sampledata.autompg import autompg\n",
"\n",
"autompg_ds = hv.Dataset(autompg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%opts Histogram (alpha=0.9) [width=600]\n",
"autompg_ds.hist(dimension='mpg', groupby='cyl', adjoin=False)"
]
}
],
"metadata": {
},
"nbformat": 4,
"nbformat_minor": 2
}
66 changes: 66 additions & 0 deletions examples/gallery/demos/matplotlib/autompg_histogram.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Most examples work across multiple plotting backends, this example is also available for:\n",
"\n",
"* [Bokeh - autompg_histogram](../bokeh/autompg_histogram.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import holoviews as hv\n",
"hv.extension('matplotlib')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Declaring data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from bokeh.sampledata.autompg import autompg\n",
"\n",
"autompg_ds = hv.Dataset(autompg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%opts Histogram [fig_size=200 aspect=2]\n",
"autompg_ds.hist(dimension='mpg', groupby='cyl', adjoin=False)"
]
}
],
"metadata": {
},
"nbformat": 4,
"nbformat_minor": 2
}
14 changes: 12 additions & 2 deletions holoviews/operation/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from param import _is_number

from ..core import (Operation, NdOverlay, Overlay, GridMatrix,
HoloMap, Dataset, Element, Collator)
HoloMap, Dataset, Element, Collator, Dimension)
from ..core.data import ArrayInterface, DictInterface
from ..core.util import find_minmax, group_sanitizer, label_sanitizer, pd
from ..core.util import find_minmax, group_sanitizer, label_sanitizer, pd, basestring
from ..element.chart import Histogram, Scatter
from ..element.raster import Raster, Image, RGB, QuadMesh
from ..element.path import Contours, Polygons
Expand Down Expand Up @@ -471,6 +471,9 @@ class histogram(Operation):
dimension = param.String(default=None, doc="""
Along which dimension of the Element to compute the histogram.""")

groupby = param.ClassSelector(default=None, class_=(basestring, Dimension), doc="""
Defines a dimension to group the Histogram returning an NdOverlay of Histograms.""")

individually = param.Boolean(default=True, doc="""
Specifies whether the histogram will be rescaled for each Element in a UniformNdMapping.""")

Expand All @@ -496,6 +499,13 @@ class histogram(Operation):
Used for setting a common style for histograms in a HoloMap or AdjointLayout.""")

def _process(self, view, key=None):
if self.p.groupby:
if not isinstance(view, Dataset):
raise ValueError('Cannot use histogram groupby on non-Dataset Element')
grouped = view.groupby(self.p.groupby, group_type=Dataset, container_type=NdOverlay)
self.p.groupby = None
return grouped.map(self._process, Dataset)

if self.p.dimension:
selected_dim = self.p.dimension
else:
Expand Down

0 comments on commit aff5715

Please sign in to comment.