Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a groupby argument to the histogram operation #1725

Merged
merged 2 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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