mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-03 00:32:39 +02:00
provide two classes for plotly plots to remove global variable
This commit is contained in:
parent
a207d09d2b
commit
a7a2e53f6d
2 changed files with 48 additions and 30 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||||
current_lib = [None]
|
current_lib = [None]
|
||||||
|
|
||||||
supported_libraries = ['matplotlib', 'plotly', 'none']
|
supported_libraries = ['matplotlib', 'plotly', 'plotly_online', 'plotly_offline', 'none']
|
||||||
error_suggestion = "Please make sure you specify your plotting library in your configuration file (<User>/.config/GPy/user.cfg).\n\n[plotting]\nlibrary = <library>\n\nCurrently supported libraries: {}".format(", ".join(supported_libraries))
|
error_suggestion = "Please make sure you specify your plotting library in your configuration file (<User>/.config/GPy/user.cfg).\n\n[plotting]\nlibrary = <library>\n\nCurrently supported libraries: {}".format(", ".join(supported_libraries))
|
||||||
|
|
||||||
def change_plotting_library(lib, **kwargs):
|
def change_plotting_library(lib, **kwargs):
|
||||||
|
|
@ -19,10 +19,14 @@ def change_plotting_library(lib, **kwargs):
|
||||||
from .matplot_dep.plot_definitions import MatplotlibPlots
|
from .matplot_dep.plot_definitions import MatplotlibPlots
|
||||||
from .matplot_dep import visualize, mapping_plots, priors_plots, ssgplvm, svig_plots, variational_plots, img_plots
|
from .matplot_dep import visualize, mapping_plots, priors_plots, ssgplvm, svig_plots, variational_plots, img_plots
|
||||||
current_lib[0] = MatplotlibPlots()
|
current_lib[0] = MatplotlibPlots()
|
||||||
if lib == 'plotly':
|
if lib in ['plotly', 'plotly_online']:
|
||||||
import plotly
|
import plotly
|
||||||
from .plotly_dep.plot_definitions import PlotlyPlots
|
from .plotly_dep.plot_definitions import PlotlyPlotsOnline
|
||||||
current_lib[0] = PlotlyPlots(**kwargs)
|
current_lib[0] = PlotlyPlotsOnline(**kwargs)
|
||||||
|
if lib == 'plotly_offline':
|
||||||
|
import plotly
|
||||||
|
from .plotly_dep.plot_definitions import PlotlyPlotsOffline
|
||||||
|
current_lib[0] = PlotlyPlotsOffline(**kwargs)
|
||||||
if lib == 'none':
|
if lib == 'none':
|
||||||
current_lib[0] = None
|
current_lib[0] = None
|
||||||
inject_plotting()
|
inject_plotting()
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,6 @@ import numpy as np
|
||||||
from ..abstract_plotting_library import AbstractPlottingLibrary
|
from ..abstract_plotting_library import AbstractPlottingLibrary
|
||||||
from .. import Tango
|
from .. import Tango
|
||||||
from . import defaults
|
from . import defaults
|
||||||
OFFLINE=False
|
|
||||||
import plotly
|
import plotly
|
||||||
from plotly import tools
|
from plotly import tools
|
||||||
from plotly.graph_objs import Scatter, Scatter3d, Line,\
|
from plotly.graph_objs import Scatter, Scatter3d, Line,\
|
||||||
|
|
@ -53,13 +52,11 @@ SYMBOL_MAP = {
|
||||||
'd': 'diamond',
|
'd': 'diamond',
|
||||||
}
|
}
|
||||||
|
|
||||||
class PlotlyPlots(AbstractPlottingLibrary):
|
class PlotlyPlotsBase(AbstractPlottingLibrary):
|
||||||
def __init__(self, offline=False):
|
def __init__(self):
|
||||||
super(PlotlyPlots, self).__init__()
|
super(PlotlyPlotsBase, self).__init__()
|
||||||
self._defaults = defaults.__dict__
|
self._defaults = defaults.__dict__
|
||||||
self.current_states = dict()
|
self.current_states = dict()
|
||||||
global OFFLINE
|
|
||||||
OFFLINE=offline
|
|
||||||
|
|
||||||
def figure(self, rows=1, cols=1, specs=None, is_3d=False, **kwargs):
|
def figure(self, rows=1, cols=1, specs=None, is_3d=False, **kwargs):
|
||||||
if specs is None:
|
if specs is None:
|
||||||
|
|
@ -101,8 +98,8 @@ class PlotlyPlots(AbstractPlottingLibrary):
|
||||||
append_annotation(a, xref, yref)
|
append_annotation(a, xref, yref)
|
||||||
# elif isinstance(traces, (Trace)): # doesn't work
|
# elif isinstance(traces, (Trace)): # doesn't work
|
||||||
# elif type(traces) in [v for k,v in go.__dict__.iteritems()]:
|
# elif type(traces) in [v for k,v in go.__dict__.iteritems()]:
|
||||||
elif isinstance(traces, (Scatter, Scatter3d, Line, Marker, ErrorX,
|
elif isinstance(traces, (Scatter, Scatter3d, ErrorX,
|
||||||
ErrorY, Bar, Heatmap, Trace, Contour, Font, Surface)):
|
ErrorY, Bar, Heatmap, Trace, Contour, Surface)):
|
||||||
try:
|
try:
|
||||||
append_trace(traces, row, col)
|
append_trace(traces, row, col)
|
||||||
except PlotlyDictKeyError:
|
except PlotlyDictKeyError:
|
||||||
|
|
@ -120,22 +117,7 @@ class PlotlyPlots(AbstractPlottingLibrary):
|
||||||
return canvas
|
return canvas
|
||||||
|
|
||||||
def show_canvas(self, canvas, filename=None, **kwargs):
|
def show_canvas(self, canvas, filename=None, **kwargs):
|
||||||
figure, _, _ = canvas
|
return NotImplementedError
|
||||||
if len(figure.data) == 0:
|
|
||||||
# add mock data
|
|
||||||
figure.append_trace(Scatter(x=[], y=[], name='', showlegend=False), 1, 1)
|
|
||||||
from ..gpy_plot.plot_util import in_ipynb
|
|
||||||
if in_ipynb():
|
|
||||||
if OFFLINE:
|
|
||||||
plotly.offline.init_notebook_mode(connected=True)
|
|
||||||
return plotly.offline.iplot(figure, filename=filename, **kwargs)#self.current_states[hex(id(figure))]['filename'])
|
|
||||||
else:
|
|
||||||
return plotly.plotly.iplot(figure, filename=filename, **kwargs)
|
|
||||||
else:
|
|
||||||
if OFFLINE:
|
|
||||||
return plotly.offline.plot(figure, filename=filename, **kwargs)
|
|
||||||
else:
|
|
||||||
return plotly.plotly.plot(figure, filename=filename, **kwargs)#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):
|
def scatter(self, ax, X, Y, Z=None, color=Tango.colorsHex['mediumBlue'], cmap=None, label=None, marker='o', marker_kwargs=None, **kwargs):
|
||||||
try:
|
try:
|
||||||
|
|
@ -245,7 +227,7 @@ class PlotlyPlots(AbstractPlottingLibrary):
|
||||||
|
|
||||||
def imshow_interact(self, ax, plot_function, extent=None, label=None, resolution=None, vmin=None, vmax=None, **imshow_kwargs):
|
def imshow_interact(self, ax, plot_function, extent=None, label=None, resolution=None, vmin=None, vmax=None, **imshow_kwargs):
|
||||||
# TODO stream interaction?
|
# TODO stream interaction?
|
||||||
super(PlotlyPlots, self).imshow_interact(ax, plot_function)
|
super(PlotlyPlotsBase, self).imshow_interact(ax, plot_function)
|
||||||
|
|
||||||
def annotation_heatmap(self, ax, X, annotation, extent=None, label='Gradient', imshow_kwargs=None, **annotation_kwargs):
|
def annotation_heatmap(self, ax, X, annotation, extent=None, label='Gradient', imshow_kwargs=None, **annotation_kwargs):
|
||||||
imshow_kwargs.setdefault('label', label)
|
imshow_kwargs.setdefault('label', label)
|
||||||
|
|
@ -272,7 +254,7 @@ class PlotlyPlots(AbstractPlottingLibrary):
|
||||||
return imshow, annotations
|
return imshow, annotations
|
||||||
|
|
||||||
def annotation_heatmap_interact(self, ax, plot_function, extent, label=None, resolution=15, imshow_kwargs=None, **annotation_kwargs):
|
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)
|
super(PlotlyPlotsBase, self).annotation_heatmap_interact(ax, plot_function, extent)
|
||||||
|
|
||||||
def contour(self, ax, X, Y, C, levels=20, label=None, **kwargs):
|
def contour(self, ax, X, Y, C, levels=20, label=None, **kwargs):
|
||||||
return Contour(x=X, y=Y, z=C,
|
return Contour(x=X, y=Y, z=C,
|
||||||
|
|
@ -325,3 +307,35 @@ class PlotlyPlots(AbstractPlottingLibrary):
|
||||||
name=None, line=Line(width=1, smoothing=0, color=fcolor), mode='none', fill='tonextx',
|
name=None, line=Line(width=1, smoothing=0, color=fcolor), mode='none', fill='tonextx',
|
||||||
legendgroup='density', hoverinfo='none', **kwargs))
|
legendgroup='density', hoverinfo='none', **kwargs))
|
||||||
return polycol
|
return polycol
|
||||||
|
|
||||||
|
|
||||||
|
class PlotlyPlotsOnline(PlotlyPlotsBase):
|
||||||
|
def __init__(self):
|
||||||
|
super(PlotlyPlotsOnline, self).__init__()
|
||||||
|
|
||||||
|
def show_canvas(self, canvas, filename=None, **kwargs):
|
||||||
|
figure, _, _ = canvas
|
||||||
|
if len(figure.data) == 0:
|
||||||
|
# add mock data
|
||||||
|
figure.append_trace(Scatter(x=[], y=[], name='', showlegend=False), 1, 1)
|
||||||
|
from ..gpy_plot.plot_util import in_ipynb
|
||||||
|
if in_ipynb():
|
||||||
|
return plotly.plotly.iplot(figure, filename=filename, **kwargs)
|
||||||
|
else:
|
||||||
|
return plotly.plotly.plot(figure, filename=filename, **kwargs)#self.current_states[hex(id(figure))]['filename'])
|
||||||
|
|
||||||
|
class PlotlyPlotsOffline(PlotlyPlotsBase):
|
||||||
|
def __init__(self):
|
||||||
|
super(PlotlyPlotsOffline, self).__init__()
|
||||||
|
|
||||||
|
def show_canvas(self, canvas, filename=None, **kwargs):
|
||||||
|
figure, _, _ = canvas
|
||||||
|
if len(figure.data) == 0:
|
||||||
|
# add mock data
|
||||||
|
figure.append_trace(Scatter(x=[], y=[], name='', showlegend=False), 1, 1)
|
||||||
|
from ..gpy_plot.plot_util import in_ipynb
|
||||||
|
if in_ipynb():
|
||||||
|
plotly.offline.init_notebook_mode(connected=True)
|
||||||
|
return plotly.offline.iplot(figure, filename=filename, **kwargs)#self.current_states[hex(id(figure))]['filename'])
|
||||||
|
else:
|
||||||
|
return plotly.offline.plot(figure, filename=filename, **kwargs)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue