Skip to content

Commit

Permalink
Added Labels element
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 1, 2018
1 parent 8532e3a commit 2b3a004
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
15 changes: 15 additions & 0 deletions holoviews/element/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from ..core.util import datetime_types, basestring
from ..core import Dimension, Element2D

from .chart import Points


class Annotation(Element2D):
"""
Expand Down Expand Up @@ -251,3 +253,16 @@ def __init__(self, x, y, text, fontsize=12,
super(Text, self).__init__(info, x=x, y=y, text=text,
fontsize=fontsize, rotation=rotation,
halign=halign, valign=valign, **params)


class Labels(Points):
"""
Labels represents a collection of text labels associated with 2D
coordinates. Unlike other Annotation types it is vectorized to
draw labels from a columnar dataset.
"""

group = param.String(default='Labels', constant=True)

vdims = param.List([Dimension('Label')], bounds=(1, 1), doc="""
Defines the value dimension corresponding to the label text.""")
6 changes: 4 additions & 2 deletions holoviews/plotting/bokeh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ErrorBars, Text, HLine, VLine, Spline, Spikes,
Table, ItemTable, Area, HSV, QuadMesh, VectorField,
Graph, Nodes, EdgePaths, Distribution, Bivariate,
TriMesh, Violin, Chord)
TriMesh, Violin, Chord, Labels)
from ...core.options import Options, Cycle, Palette
from ...core.util import VersionError

Expand All @@ -28,7 +28,8 @@
except:
DFrame = None

from .annotation import TextPlot, LineAnnotationPlot, SplinePlot, ArrowPlot
from .annotation import (LabelsPlot, TextPlot, LineAnnotationPlot,
SplinePlot, ArrowPlot)
from .callbacks import Callback # noqa (API import)
from .element import OverlayPlot, ElementPlot
from .chart import (PointPlot, CurvePlot, SpreadPlot, ErrorPlot, HistogramPlot,
Expand Down Expand Up @@ -90,6 +91,7 @@
HLine: LineAnnotationPlot,
VLine: LineAnnotationPlot,
Text: TextPlot,
Labels: LabelsPlot,
Spline: SplinePlot,
Arrow: ArrowPlot,

Expand Down
26 changes: 26 additions & 0 deletions holoviews/plotting/bokeh/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ def get_extents(self, element, ranges=None):



class LabelsPlot(ElementPlot):

style_opts = text_properties

_plot_methods = dict(single='text', batched='text')
_batched_style_opts = text_properties

def get_data(self, element, ranges=None, empty=False):
style = self.style[self.cyclic_index]
dims = element.dimensions(label=True)

xidx, yidx = (1, 0) if self.invert_axes else (0, 1)
mapping = dict(x=dims[xidx], y=dims[yidx])
data = {}

xdim, ydim = dims[xidx], dims[yidx]
data[xdim] = element.dimension_values(xidx)
data[ydim] = element.dimension_values(yidx)
self._categorize_data(data, (xdim, ydim), element.dimensions())

text_dim = dims[2]
data[text_dim] = element.dimension_values(text_dim)
return data, mapping



class LineAnnotationPlot(ElementPlot):

style_opts = line_properties
Expand Down
1 change: 1 addition & 0 deletions holoviews/plotting/mpl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def grid_selector(grid):
Arrow: ArrowPlot,
Spline: SplinePlot,
Text: TextPlot,
Labels: LabelsPlot,

# Path plots
Contours: ContourPlot,
Expand Down
24 changes: 24 additions & 0 deletions holoviews/plotting/mpl/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ def draw_annotation(self, axis, data, opts):
rotation=rotation, **opts)]


class LabelsPlot(ElementPlot):

style_opts = ['alpha', 'color', 'visible', 'linewidth',
'linestyle', 'marker', 'ms']

_plot_methods = dict(single='annotate')

def get_data(self, element, ranges, style):
xs, ys = (element.dimension_values(i) for i in range(2))
text = element.dimension_values(2)
positions = (ys, xs) if self.invert_axes else (xs, ys)
return positions + (text,), style, {}

def init_artists(self, ax, plot_args, plot_kwargs):
texts = []
for x, y, text in zip(*plot_args):
texts.append(ax.text(x, y, text, **plot_kwargs))
return {'artist': texts}

def teardown_handles(self):
if 'artist' in self.handles:
for artist in self.handles['artist']:
artist.remove()


class ArrowPlot(AnnotationPlot):
"Draw an arrow using the information supplied to the Arrow annotation"
Expand Down

0 comments on commit 2b3a004

Please sign in to comment.