mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-05 01:32:40 +02:00
[plotly] starting plotly
This commit is contained in:
parent
cbb06f3efc
commit
e0d48a0558
18 changed files with 120 additions and 128 deletions
|
|
@ -9,7 +9,7 @@ import numpy
|
|||
|
||||
|
||||
class ImshowController(BufferedAxisChangedController):
|
||||
def __init__(self, ax, plot_function, plot_limits, resolution=50, update_lim=.8, **kwargs):
|
||||
def __init__(self, ax, plot_function, plot_limits, resolution=50, update_lim=.9, **kwargs):
|
||||
"""
|
||||
:param plot_function:
|
||||
function to use for creating image for plotting (return ndarray-like)
|
||||
|
|
@ -23,7 +23,7 @@ class ImshowController(BufferedAxisChangedController):
|
|||
super(ImshowController, self).__init__(ax, plot_function, plot_limits, resolution, update_lim, **kwargs)
|
||||
|
||||
def _init_view(self, canvas, X, xmin, xmax, ymin, ymax, vmin=None, vmax=None, **kwargs):
|
||||
xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
|
||||
xoffset, yoffset = 0, 0#self._offsets(xmin, xmax, ymin, ymax)
|
||||
return canvas.imshow(X, extent=(xmin-xoffset, xmax+xoffset,
|
||||
ymin-yoffset, ymax+yoffset),
|
||||
vmin=vmin, vmax=vmax,
|
||||
|
|
@ -31,7 +31,7 @@ class ImshowController(BufferedAxisChangedController):
|
|||
|
||||
def update_view(self, view, X, xmin, xmax, ymin, ymax):
|
||||
view.set_data(X)
|
||||
xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
|
||||
xoffset, yoffset = 0, 0#self._offsets(xmin, xmax, ymin, ymax)
|
||||
view.set_extent((xmin-xoffset, xmax+xoffset,
|
||||
ymin-yoffset, ymax+yoffset))
|
||||
|
||||
|
|
@ -57,19 +57,19 @@ class ImAnnotateController(ImshowController):
|
|||
def _init_view(self, ax, X, xmin, xmax, ymin, ymax, **kwargs):
|
||||
view = [super(ImAnnotateController, self)._init_view(ax, X[0], xmin, xmax, ymin, ymax, **self.imshow_kwargs)]
|
||||
xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
|
||||
xlin = numpy.linspace(xmin-xoffset, xmax+xoffset, self.resolution, endpoint=False)
|
||||
ylin = numpy.linspace(ymin-yoffset, ymax+yoffset, self.resolution, endpoint=False)
|
||||
xlin = numpy.linspace(xmin, xmax, self.resolution, endpoint=False)
|
||||
ylin = numpy.linspace(ymin, ymax, self.resolution, endpoint=False)
|
||||
for [i, x], [j, y] in itertools.product(enumerate(xlin), enumerate(ylin)):
|
||||
view.append(ax.text(x, y, "{}".format(X[1][j, i]), ha='center', va='center', **kwargs))
|
||||
view.append(ax.text(x+xoffset, y+yoffset, "{}".format(X[1][j, i]), ha='center', va='center', **kwargs))
|
||||
return view
|
||||
|
||||
def update_view(self, view, X, xmin, xmax, ymin, ymax):
|
||||
super(ImAnnotateController, self).update_view(view[0], X[0], xmin, xmax, ymin, ymax)
|
||||
xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
|
||||
xlin = numpy.linspace(xmin-xoffset, xmax+xoffset, self.resolution, endpoint=False)
|
||||
ylin = numpy.linspace(ymin-yoffset, ymax+yoffset, self.resolution, endpoint=False)
|
||||
xlin = numpy.linspace(xmin, xmax, self.resolution, endpoint=False)
|
||||
ylin = numpy.linspace(ymin, ymax, self.resolution, endpoint=False)
|
||||
for [[i, x], [j, y]], text in zip(itertools.product(enumerate(xlin), enumerate(ylin)), view[1:]):
|
||||
text.set_x(x + xoffset)
|
||||
text.set_y(y + yoffset)
|
||||
text.set_x(x+xoffset)
|
||||
text.set_y(y+yoffset)
|
||||
text.set_text("{}".format(X[1][j, i]))
|
||||
return view
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
#import numpy as np
|
||||
#import Tango
|
||||
#from base_plots import gpplot, x_frame1D, x_frame2D
|
||||
|
||||
from . import plotting_library as pl
|
||||
|
||||
def plot_optimizer(optimizer, **kwargs):
|
||||
if optimizer.trace == None:
|
||||
print("No trace present so I can't plot it. Please check that the optimizer actually supplies a trace.")
|
||||
else:
|
||||
canvas, kwargs = pl.get_new_canvas(**kwargs)
|
||||
plots = dict(trace=pl.plot(range(len(optimizer.trace)), optimizer.trace))
|
||||
return pl.show_canvas(canvas, plots, xlabel='Iteration', ylabel='f(x)')
|
||||
|
||||
def plot_sgd_traces(optimizer):
|
||||
pb.figure()
|
||||
pb.subplot(211)
|
||||
pb.title('Parameters')
|
||||
for k in optimizer.param_traces.keys():
|
||||
pb.plot(optimizer.param_traces[k], label=k)
|
||||
pb.legend(loc=0)
|
||||
pb.subplot(212)
|
||||
pb.title('Objective function')
|
||||
pb.plot(optimizer.fopt_trace)
|
||||
|
|
@ -35,7 +35,7 @@ from . import defaults
|
|||
from matplotlib.colors import LinearSegmentedColormap
|
||||
from .controllers import ImshowController, ImAnnotateController
|
||||
import itertools
|
||||
from GPy.plotting.matplot_dep.util import legend_ontop
|
||||
from .util import legend_ontop
|
||||
|
||||
class MatplotlibPlots(AbstractPlottingLibrary):
|
||||
def __init__(self):
|
||||
|
|
@ -43,48 +43,52 @@ class MatplotlibPlots(AbstractPlottingLibrary):
|
|||
self._defaults = defaults.__dict__
|
||||
|
||||
def figure(self, rows=1, cols=1, **kwargs):
|
||||
self.rows = rows
|
||||
self.cols = cols
|
||||
return plt.figure(**kwargs)
|
||||
fig = plt.figure(**kwargs)
|
||||
fig.rows = rows
|
||||
fig.cols = cols
|
||||
return fig
|
||||
|
||||
def get_new_canvas(self, projection='2d', figure=None, col=1, row=1, **kwargs):
|
||||
def new_canvas(self, figure=None, col=1, row=1, projection='2d', xlabel=None, ylabel=None, zlabel=None, title=None, xlim=None, ylim=None, zlim=None, **kwargs):
|
||||
if projection == '3d':
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
elif projection == '2d':
|
||||
projection = None
|
||||
if 'ax' in kwargs:
|
||||
ax = kwargs.pop('ax')
|
||||
elif 'num' in kwargs and 'figsize' in kwargs:
|
||||
fig = self.figure(num=kwargs.pop('num'), figsize=kwargs.pop('figsize'))
|
||||
elif 'num' in kwargs:
|
||||
fig = self.figure(num=kwargs.pop('num'))
|
||||
elif 'figsize' in kwargs:
|
||||
fig = self.figure(figsize=kwargs.pop('figsize'))
|
||||
else:
|
||||
fig = self.figure()
|
||||
if 'num' in kwargs and 'figsize' in kwargs:
|
||||
fig = self.figure(num=kwargs.pop('num'), figsize=kwargs.pop('figsize'))
|
||||
elif 'num' in kwargs:
|
||||
fig = self.figure(num=kwargs.pop('num'))
|
||||
elif 'figsize' in kwargs:
|
||||
fig = self.figure(figsize=kwargs.pop('figsize'))
|
||||
else:
|
||||
fig = self.figure()
|
||||
|
||||
ax = fig.add_subplot(self.rows, self.cols, )
|
||||
if hasattr(fig, 'rows') and hasattr(fig, 'cols'):
|
||||
ax = fig.add_subplot(fig.rows, fig.cols, (col,row), projection=projection)
|
||||
else:
|
||||
ax = fig.add_subplot(1, 1, (1, 1), projection=projection)
|
||||
|
||||
return ax, kwargs
|
||||
|
||||
def show_canvas(self, ax, plots, xlabel=None, ylabel=None, zlabel=None, title=None, xlim=None, ylim=None, zlim=None, legend=False, **kwargs):
|
||||
ax.autoscale_view()
|
||||
ax.set_xlim(xlim)
|
||||
ax.set_ylim(ylim)
|
||||
if xlim is not None: ax.set_xlim(xlim)
|
||||
if ylim is not None: ax.set_ylim(ylim)
|
||||
if xlabel is not None: ax.set_xlabel(xlabel)
|
||||
if ylabel is not None: ax.set_ylabel(ylabel)
|
||||
if zlabel is not None: ax.set_zlabel(zlabel)
|
||||
if title is not None: ax.set_title(title)
|
||||
if projection == '3d':
|
||||
if zlim is not None: ax.set_zlim(zlim)
|
||||
if zlabel is not None: ax.set_zlabel(zlabel)
|
||||
return ax, kwargs
|
||||
|
||||
def show_canvas(self, ax, plots, legend=False, title=None, **kwargs):
|
||||
ax.autoscale_view()
|
||||
fontdict=dict(family='sans-serif', weight='light', size=9)
|
||||
if legend is True:
|
||||
ax.legend(*ax.get_legend_handles_labels())
|
||||
elif legend >= 1:
|
||||
#ax.legend(prop=fontdict)
|
||||
legend_ontop(ax, ncol=legend, fontdict=fontdict)
|
||||
if zlim is not None:
|
||||
ax.set_zlim(zlim)
|
||||
ax.figure.canvas.draw()
|
||||
ax.figure.show()
|
||||
if title is not None: ax.figure.suptitle(title)
|
||||
ax.figure.canvas.draw()
|
||||
return plots
|
||||
|
||||
|
|
@ -158,12 +162,11 @@ class MatplotlibPlots(AbstractPlottingLibrary):
|
|||
extent = (0, X.shape[0], 0, X.shape[1])
|
||||
xmin, xmax, ymin, ymax = extent
|
||||
xoffset, yoffset = (xmax - xmin) / (2. * X.shape[0]), (ymax - ymin) / (2. * X.shape[1])
|
||||
xmin, xmax, ymin, ymax = extent = xmin+xoffset, xmax-xoffset, ymin+yoffset, ymax-yoffset
|
||||
xlin = np.linspace(xmin, xmax, X.shape[0], endpoint=False)
|
||||
ylin = np.linspace(ymin, ymax, X.shape[1], endpoint=False)
|
||||
annotations = []
|
||||
for [i, x], [j, y] in itertools.product(enumerate(xlin), enumerate(ylin)):
|
||||
annotations.append(ax.text(x, y, "{}".format(annotation[j, i]), **annotation_kwargs))
|
||||
annotations.append(ax.text(x+xoffset, y+yoffset, "{}".format(annotation[j, i]), **annotation_kwargs))
|
||||
return imshow, annotations
|
||||
|
||||
def annotation_heatmap_interact(self, ax, plot_function, extent, label=None, resolution=15, imshow_kwargs=None, **annotation_kwargs):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue