From 3251c83fe8da61e53fdcaf9ab653304b4d557834 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Tue, 13 Jun 2017 13:36:00 +0100 Subject: [PATCH] Bokeh SplinePlot handles multiple cubic splines (#1529) * Bokeh SplinePlot handles multiple cubic splines correctly * Matplotlib SplinePlot handles empty Spline * Simplified bokeh SplinePlot implementation --- holoviews/plotting/bokeh/annotation.py | 26 ++++++++++++++++---------- holoviews/plotting/mpl/annotation.py | 2 ++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/holoviews/plotting/bokeh/annotation.py b/holoviews/plotting/bokeh/annotation.py index a17821f2c5..7d3646eac6 100644 --- a/holoviews/plotting/bokeh/annotation.py +++ b/holoviews/plotting/bokeh/annotation.py @@ -87,14 +87,20 @@ class SplinePlot(ElementPlot): _plot_methods = dict(single='bezier') def get_data(self, element, ranges=None, empty=False): - data_attrs = ['x0', 'y0', 'x1', 'y1', - 'cx0', 'cx1', 'cy0', 'cy1'] - if empty: - data = {attr: [] for attr in data_attrs} - else: - verts = np.array(element.data[0]) - xs, ys = verts[:, 0], verts[:, 1] - data = dict(x0=[xs[0]], y0=[ys[0]], x1=[xs[-1]], y1=[ys[-1]], - cx0=[xs[1]], cy0=[ys[1]], cx1=[xs[2]], cy1=[ys[2]]) - + data_attrs = ['x0', 'y0', 'cx0', 'cy0', 'cx1', 'cy1', 'x1', 'y1',] + verts = np.array(element.data[0]) + inds = np.where(np.array(element.data[1])==1)[0] + data = {da: [] for da in data_attrs} + skipped = False + for vs in np.split(verts, inds[1:]): + if len(vs) != 4: + skipped = len(vs) > 1 + continue + for x, y, xl, yl in zip(vs[:, 0], vs[:, 1], data_attrs[::2], data_attrs[1::2]): + data[xl].append(x) + data[yl].append(y) + if skipped: + self.warning('Bokeh SplitPlot only support cubic splines, ' + 'unsupported splines were skipped during plotting.') + data = {da: data[da] for da in data_attrs} return (data, dict(zip(data_attrs, data_attrs))) diff --git a/holoviews/plotting/mpl/annotation.py b/holoviews/plotting/mpl/annotation.py index 684a1be5c1..b169ca59f6 100644 --- a/holoviews/plotting/mpl/annotation.py +++ b/holoviews/plotting/mpl/annotation.py @@ -106,6 +106,8 @@ class SplinePlot(AnnotationPlot): def draw_annotation(self, axis, data, opts): verts, codes = data + if not len(verts): + return [] patch = patches.PathPatch(matplotlib.path.Path(verts, codes), facecolor='none', **opts) axis.add_patch(patch)