From f4f6eb32c220485d9c1e095aa2460492edbf1fde Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Fri, 9 Oct 2015 09:52:41 +0100 Subject: [PATCH] [plotly] last minute change for ipython notebook --- GPy/installation.cfg | 4 +- GPy/plotting/plotly_dep/plot_definitions.py | 117 ++++++++++---------- 2 files changed, 60 insertions(+), 61 deletions(-) diff --git a/GPy/installation.cfg b/GPy/installation.cfg index 2056c0b9..d3b20c32 100644 --- a/GPy/installation.cfg +++ b/GPy/installation.cfg @@ -1,5 +1,5 @@ # This is the local installation configuration file for GPy [plotting] -#library = plotly -library = matplotlib \ No newline at end of file +library = plotly +#library = matplotlib diff --git a/GPy/plotting/plotly_dep/plot_definitions.py b/GPy/plotting/plotly_dep/plot_definitions.py index 4b06658c..f7eba66b 100644 --- a/GPy/plotting/plotly_dep/plot_definitions.py +++ b/GPy/plotting/plotly_dep/plot_definitions.py @@ -1,21 +1,21 @@ #=============================================================================== # Copyright (c) 2015, Max Zwiessele # All rights reserved. -# +# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: -# +# # * Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. -# +# # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. -# +# # * Neither the name of GPy.plotting.matplot_dep.plot_definitions nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. -# +# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -57,18 +57,18 @@ class PlotlyPlots(AbstractPlottingLibrary): super(PlotlyPlots, self).__init__() self._defaults = defaults.__dict__ self.current_states = dict() - + def figure(self, rows=1, cols=1, specs=None, is_3d=False): if specs is None: specs = [[{'is_3d': is_3d}]*cols]*rows figure = tools.make_subplots(rows, cols, specs=specs) return figure - + def new_canvas(self, canvas=None, row=1, col=1, projection='2d', xlabel=None, ylabel=None, zlabel=None, title=None, xlim=None, ylim=None, zlim=None, **kwargs): #if 'filename' not in kwargs: # print('PlotlyWarning: filename was not given, this may clutter your plotly workspace') # filename = None - #else: + #else: # filename = kwargs.pop('filename') if canvas is None: figure = self.figure(is_3d=projection=='3d') @@ -76,7 +76,7 @@ class PlotlyPlots(AbstractPlottingLibrary): else: return canvas, kwargs return (figure, row, col), kwargs - + def add_to_canvas(self, canvas, traces, legend=False, **kwargs): figure, row, col = canvas def append_annotation(a, xref, yref): @@ -90,7 +90,7 @@ class PlotlyPlots(AbstractPlottingLibrary): def recursive_append(traces): if isinstance(traces, Annotations): xref, yref = figure._grid_ref[row-1][col-1] - for a in traces: + for a in traces: append_annotation(a, xref, yref) elif isinstance(traces, (Trace)): try: @@ -108,7 +108,7 @@ class PlotlyPlots(AbstractPlottingLibrary): recursive_append(traces) figure.layout['showlegend'] = legend return canvas - + def show_canvas(self, canvas, filename=None, **kwargs): figure, _, _ = canvas if len(figure.data) == 0: @@ -116,11 +116,10 @@ class PlotlyPlots(AbstractPlottingLibrary): figure.append_trace(Scatter(x=[], y=[], name='', showlegend=False), 1, 1) from ..gpy_plot.plot_util import in_ipynb if in_ipynb(): - py.iplot(figure, filename=filename)#self.current_states[hex(id(figure))]['filename']) + return py.iplot(figure, filename=filename)#self.current_states[hex(id(figure))]['filename']) else: - py.plot(figure, filename=filename)#self.current_states[hex(id(figure))]['filename']) - return figure - + return py.plot(figure, filename=filename)#self.current_states[hex(id(figure))]['filename']) + def scatter(self, ax, X, Y, Z=None, color=Tango.colorsHex['mediumBlue'], cmap=None, label=None, marker='o', marker_kwargs=None, **kwargs): try: marker = SYMBOL_MAP[marker] @@ -130,7 +129,7 @@ class PlotlyPlots(AbstractPlottingLibrary): if Z is not None: return Scatter3d(x=X, y=Y, z=Z, mode='markers', showlegend=label is not None, marker=Marker(color=color, symbol=marker, colorscale=cmap, **marker_kwargs or {}), name=label, **kwargs) return Scatter(x=X, y=Y, mode='markers', showlegend=label is not None, marker=Marker(color=color, symbol=marker, colorscale=cmap, **marker_kwargs or {}), name=label, **kwargs) - + def plot(self, ax, X, Y, Z=None, color=None, label=None, line_kwargs=None, **kwargs): if 'mode' not in kwargs: kwargs['mode'] = 'lines' @@ -144,7 +143,7 @@ class PlotlyPlots(AbstractPlottingLibrary): for n, row in enumerate(X): annotations.append( Annotation( - text='', + text='', x=row[0], y=0, yref='paper', ax=0, ay=20, @@ -157,13 +156,13 @@ class PlotlyPlots(AbstractPlottingLibrary): #if Z is not None: # return Scatter3d(x=X[:,0], y=X[:,1], z=0, zref='paper', showlegend=label is not None, mode='markers', marker=Marker(color=color, symbol='diamond-tall', **marker_kwargs or {}), name=label, **kwargs) #return Scatter(x=X, y=0, mode='markers', showlegend=label is not None, marker=Marker(yref='paper', color=color, symbol='diamond-tall', **marker_kwargs or {}), name=label, **kwargs) - + def barplot(self, canvas, x, height, width=0.8, bottom=0, color=Tango.colorsHex['mediumBlue'], label=None, **kwargs): figure, _, _ = canvas if 'barmode' in kwargs: figure.layout['barmode'] = kwargs.pop('barmode') return Bar(x=x, y=height, marker=Marker(color=color), name=label) - + def xerrorbar(self, ax, X, Y, error, Z=None, color=Tango.colorsHex['mediumBlue'], label=None, error_kwargs=None, **kwargs): error_kwargs = error_kwargs or {} if (error.shape[0] == 2) and (error.ndim == 2): @@ -171,16 +170,16 @@ class PlotlyPlots(AbstractPlottingLibrary): else: error_kwargs.update(dict(array=error, symmetric=True)) if Z is not None: - return Scatter3d(x=X, y=Y, z=Z, mode='markers', - error_x=ErrorX(color=color, **error_kwargs or {}), - marker=Marker(size='0'), name=label, + return Scatter3d(x=X, y=Y, z=Z, mode='markers', + error_x=ErrorX(color=color, **error_kwargs or {}), + marker=Marker(size='0'), name=label, showlegend=label is not None, **kwargs) - return Scatter(x=X, y=Y, mode='markers', - error_x=ErrorX(color=color, **error_kwargs or {}), - marker=Marker(size='0'), name=label, - showlegend=label is not None, + return Scatter(x=X, y=Y, mode='markers', + error_x=ErrorX(color=color, **error_kwargs or {}), + marker=Marker(size='0'), name=label, + showlegend=label is not None, **kwargs) - + def yerrorbar(self, ax, X, Y, error, Z=None, color=Tango.colorsHex['mediumBlue'], label=None, error_kwargs=None, **kwargs): error_kwargs = error_kwargs or {} if (error.shape[0] == 2) and (error.ndim == 2): @@ -188,24 +187,24 @@ class PlotlyPlots(AbstractPlottingLibrary): else: error_kwargs.update(dict(array=error, symmetric=True)) if Z is not None: - return Scatter3d(x=X, y=Y, z=Z, mode='markers', - error_y=ErrorY(color=color, **error_kwargs or {}), - marker=Marker(size='0'), name=label, + return Scatter3d(x=X, y=Y, z=Z, mode='markers', + error_y=ErrorY(color=color, **error_kwargs or {}), + marker=Marker(size='0'), name=label, showlegend=label is not None, **kwargs) - return Scatter(x=X, y=Y, mode='markers', - error_y=ErrorY(color=color, **error_kwargs or {}), - marker=Marker(size='0'), name=label, - showlegend=label is not None, + return Scatter(x=X, y=Y, mode='markers', + error_y=ErrorY(color=color, **error_kwargs or {}), + marker=Marker(size='0'), name=label, + showlegend=label is not None, **kwargs) - + def imshow(self, ax, X, extent=None, label=None, vmin=None, vmax=None, **imshow_kwargs): if not 'showscale' in imshow_kwargs: imshow_kwargs['showscale'] = False - return Heatmap(z=X, name=label, + return Heatmap(z=X, name=label, x0=extent[0], dx=float(extent[1]-extent[0])/(X.shape[0]-1), y0=extent[2], dy=float(extent[3]-extent[2])/(X.shape[1]-1), - zmin=vmin, zmax=vmax, - showlegend=label is not None, + zmin=vmin, zmax=vmax, + showlegend=label is not None, hoverinfo='z', **imshow_kwargs) @@ -228,7 +227,7 @@ class PlotlyPlots(AbstractPlottingLibrary): var = X[n][m] annotations.append( Annotation( - text=str(val), + text=str(val), x=x[m], y=y[n], xref='x1', yref='y1', font=dict(color='white' if np.abs(var) > 0.8 else 'black', size=10), @@ -238,10 +237,10 @@ class PlotlyPlots(AbstractPlottingLibrary): def annotation_heatmap_interact(self, ax, plot_function, extent, label=None, resolution=15, imshow_kwargs=None, **annotation_kwargs): super(PlotlyPlots, self).annotation_heatmap_interact(ax, plot_function, extent) - + def contour(self, ax, X, Y, C, levels=20, label=None, **kwargs): - return Contour(x=X, y=Y, z=C, - #ncontours=levels, contours=Contours(start=C.min(), end=C.max(), size=(C.max()-C.min())/levels), + return Contour(x=X, y=Y, z=C, + #ncontours=levels, contours=Contours(start=C.min(), end=C.max(), size=(C.max()-C.min())/levels), name=label, **kwargs) def surface(self, ax, X, Y, Z, color=None, label=None, **kwargs): @@ -258,18 +257,18 @@ class PlotlyPlots(AbstractPlottingLibrary): u = Scatter(x=X, y=upper, fillcolor=fcolor, showlegend=label is not None, name=label, fill='tonexty', **kwargs) fcolor = '{}, {alpha})'.format(','.join(fcolor.split(',')[:-1]), alpha=0.0) l = Scatter(x=X, y=lower, fillcolor=fcolor, showlegend=False, fill='tonexty', name=label, **kwargs) - return l, u + return l, u def fill_gradient(self, canvas, X, percentiles, color=Tango.colorsHex['mediumBlue'], label=None, **kwargs): ax = canvas plots = [] - + if 'edgecolors' not in kwargs: kwargs['edgecolors'] = 'none' - + if 'facecolors' in kwargs: color = kwargs.pop('facecolors') - + if 'array' in kwargs: array = kwargs.pop('array') else: @@ -303,8 +302,8 @@ class PlotlyPlots(AbstractPlottingLibrary): # pass a, b = tee(iterable) next(b, None) - return zip(a, b) - + return zip(a, b) + polycol = [] for y1, y2 in pairwise(percentiles): import matplotlib.mlab as mlab @@ -316,51 +315,51 @@ class PlotlyPlots(AbstractPlottingLibrary): x = ma.masked_invalid(ax.convert_xunits(X)) y1 = ma.masked_invalid(ax.convert_yunits(y1)) y2 = ma.masked_invalid(ax.convert_yunits(y2)) - + if y1.ndim == 0: y1 = np.ones_like(x) * y1 if y2.ndim == 0: y2 = np.ones_like(x) * y2 - + if where is None: where = np.ones(len(x), np.bool) else: where = np.asarray(where, np.bool) - + if not (x.shape == y1.shape == y2.shape == where.shape): raise ValueError("Argument dimensions are incompatible") - + from functools import reduce mask = reduce(ma.mask_or, [ma.getmask(a) for a in (x, y1, y2)]) if mask is not ma.nomask: where &= ~mask - + polys = [] for ind0, ind1 in mlab.contiguous_regions(where): xslice = x[ind0:ind1] y1slice = y1[ind0:ind1] y2slice = y2[ind0:ind1] - + if not len(xslice): continue - + N = len(xslice) p = np.zeros((2 * N + 2, 2), np.float) - + # the purpose of the next two lines is for when y2 is a # scalar like 0 and we want the fill to go all the way # down to 0 even if none of the y1 sample points do start = xslice[0], y2slice[0] end = xslice[-1], y2slice[-1] - + p[0] = start p[N + 1] = end - + p[1:N + 1, 0] = xslice p[1:N + 1, 1] = y1slice p[N + 2:, 0] = xslice[::-1] p[N + 2:, 1] = y2slice[::-1] - + polys.append(p) polycol.extend(polys) from matplotlib.collections import PolyCollection