mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-30 14:35:15 +02:00
[docs] updated and testing
This commit is contained in:
parent
55668306cb
commit
a6ad9c33a6
43 changed files with 567 additions and 116 deletions
|
|
@ -14,7 +14,7 @@ class AxisEventController(object):
|
|||
return self
|
||||
def deactivate(self):
|
||||
for cb_class in self.ax.callbacks.callbacks.values():
|
||||
for cb_num in cb_class.keys():
|
||||
for cb_num in cb_class.keys()[:]:
|
||||
self.ax.callbacks.disconnect(cb_num)
|
||||
def activate(self):
|
||||
self.ax.callbacks.connect('xlim_changed', self.xlim_changed)
|
||||
|
|
@ -98,7 +98,7 @@ class BufferedAxisChangedController(AxisChangedController):
|
|||
"""
|
||||
super(BufferedAxisChangedController, self).__init__(ax, update_lim=update_lim)
|
||||
self.plot_function = plot_function
|
||||
xmin, ymin, xmax, ymax = plot_limits#self._x_lim # self._compute_buffered(*self._x_lim)
|
||||
xmin, xmax, ymin, ymax = plot_limits#self._x_lim # self._compute_buffered(*self._x_lim)
|
||||
# imshow acts on the limits of the plot, this is why we need to override the limits here, to make sure the right plot limits are used:
|
||||
self._x_lim = xmin, xmax
|
||||
self._y_lim = ymin, ymax
|
||||
|
|
|
|||
|
|
@ -23,14 +23,21 @@ 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):
|
||||
return canvas.imshow(X, extent=(xmin, xmax,
|
||||
ymin, ymax),
|
||||
xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
|
||||
return canvas.imshow(X, extent=(xmin-xoffset, xmax+xoffset,
|
||||
ymin-yoffset, ymax+yoffset),
|
||||
vmin=vmin, vmax=vmax,
|
||||
**kwargs)
|
||||
**kwargs)
|
||||
|
||||
def update_view(self, view, X, xmin, xmax, ymin, ymax):
|
||||
view.set_data(X)
|
||||
view.set_extent((xmin, xmax, ymin, ymax))
|
||||
xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
|
||||
view.set_extent((xmin-xoffset, xmax+xoffset,
|
||||
ymin-yoffset, ymax+yoffset))
|
||||
|
||||
def _offsets(self, xmin, xmax, ymin, ymax):
|
||||
return (xmax - xmin) / (2 * self.resolution), (ymax - ymin) / (2 * self.resolution)
|
||||
|
||||
|
||||
class ImAnnotateController(ImshowController):
|
||||
def __init__(self, ax, plot_function, plot_limits, resolution=20, update_lim=.99, imshow_kwargs=None, **kwargs):
|
||||
|
|
@ -65,7 +72,4 @@ class ImAnnotateController(ImshowController):
|
|||
text.set_x(x + xoffset)
|
||||
text.set_y(y + yoffset)
|
||||
text.set_text("{}".format(X[1][j, i]))
|
||||
return view
|
||||
|
||||
def _offsets(self, xmin, xmax, ymin, ymax):
|
||||
return (xmax - xmin) / (2 * self.resolution), (ymax - ymin) / (2 * self.resolution)
|
||||
return view
|
||||
|
|
@ -69,7 +69,7 @@ ard = dict(edgecolor='k', linewidth=1.2)
|
|||
|
||||
# Input plots:
|
||||
latent = dict(aspect='auto', cmap='Greys', interpolation='bicubic')
|
||||
gradient = dict(aspect='auto', cmap='RdBu', interpolation='nearest')
|
||||
gradient = dict(aspect='auto', cmap='RdBu', interpolation='nearest', alpha=.7)
|
||||
magnification = dict(aspect='auto', cmap='Greys', interpolation='bicubic')
|
||||
latent_scatter = dict(s=40, linewidth=.2, edgecolor='k', alpha=.9)
|
||||
annotation = dict(fontdict=dict(family='sans-serif', weight='light', fontsize=9), zorder=.3)
|
||||
annotation = dict(fontdict=dict(family='sans-serif', weight='light', fontsize=9), zorder=.3, alpha=.7)
|
||||
|
|
@ -35,13 +35,14 @@ from . import defaults
|
|||
from matplotlib.colors import LinearSegmentedColormap
|
||||
from .controllers import ImshowController, ImAnnotateController
|
||||
import itertools
|
||||
from GPy.plotting.matplot_dep.util import legend_ontop
|
||||
|
||||
class MatplotlibPlots(AbstractPlottingLibrary):
|
||||
def __init__(self):
|
||||
super(MatplotlibPlots, self).__init__()
|
||||
self._defaults = defaults.__dict__
|
||||
|
||||
def get_new_canvas(self, xlabel=None, ylabel=None, zlabel=None, title=None, projection='2d', **kwargs):
|
||||
def get_new_canvas(self, projection='2d', **kwargs):
|
||||
if projection == '3d':
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
elif projection == '2d':
|
||||
|
|
@ -57,24 +58,23 @@ class MatplotlibPlots(AbstractPlottingLibrary):
|
|||
else:
|
||||
ax = plt.figure().add_subplot(111, 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 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)
|
||||
return ax, kwargs
|
||||
|
||||
def show_canvas(self, ax, plots, xlim=None, ylim=None, zlim=None, legend=False, **kwargs):
|
||||
try:
|
||||
ax.autoscale_view()
|
||||
ax.set_xlim(xlim)
|
||||
ax.set_ylim(ylim)
|
||||
if legend:
|
||||
ax.legend()
|
||||
if zlim is not None:
|
||||
ax.set_zlim(zlim)
|
||||
ax.figure.canvas.draw()
|
||||
except:
|
||||
pass
|
||||
fontdict=dict(family='sans-serif', weight='light', size=9)
|
||||
if legend >= 1:
|
||||
#ax.legend(prop=fontdict)
|
||||
legend_ontop(ax, ncol=legend)
|
||||
if zlim is not None:
|
||||
ax.set_zlim(zlim)
|
||||
ax.figure.canvas.draw()
|
||||
return plots
|
||||
|
||||
def scatter(self, ax, X, Y, Z=None, color=Tango.colorsHex['mediumBlue'], label=None, marker='o', **kwargs):
|
||||
|
|
@ -119,27 +119,32 @@ class MatplotlibPlots(AbstractPlottingLibrary):
|
|||
return ax.errorbar(X, Y, Z, yerr=error, ecolor=color, label=label, **kwargs)
|
||||
return ax.errorbar(X, Y, yerr=error, ecolor=color, label=label, **kwargs)
|
||||
|
||||
def imshow(self, ax, X, extent=None, label=None, plot_function=None, resolution=None, vmin=None, vmax=None, **kwargs):
|
||||
def imshow(self, ax, X, extent=None, label=None, plot_function=None, resolution=None, vmin=None, vmax=None, **imshow_kwargs):
|
||||
if 'origin' not in imshow_kwargs:
|
||||
imshow_kwargs['origin'] = 'lower'
|
||||
if plot_function is not None:
|
||||
return ImshowController(ax, plot_function, extent, resolution=resolution, vmin=vmin, vmax=vmax, **kwargs)
|
||||
return ax.imshow(X, label=label, extent=extent, vmin=vmin, vmax=vmax, **kwargs)
|
||||
return ImshowController(ax, plot_function, extent, resolution=resolution, vmin=vmin, vmax=vmax, **imshow_kwargs)
|
||||
return ax.imshow(X, label=label, extent=extent, vmin=vmin, vmax=vmax, **imshow_kwargs)
|
||||
|
||||
def annotation_heatmap(self, ax, X, annotation, extent, label=None, plot_function=None, resolution=None, imshow_kwargs=None, **annotation_kwargs):
|
||||
imshow_kwargs = imshow_kwargs or {}
|
||||
if 'origin' not in imshow_kwargs:
|
||||
imshow_kwargs['origin'] = 'lower'
|
||||
if plot_function is not None:
|
||||
return ImAnnotateController(ax, plot_function, extent, resolution=resolution, imshow_kwargs=imshow_kwargs or {}, **annotation_kwargs)
|
||||
if ('ha' not in annotation_kwargs) and ('horizontalalignment' not in annotation_kwargs):
|
||||
annotation_kwargs['ha'] = 'center'
|
||||
if ('va' not in annotation_kwargs) and ('verticalalignment' not in annotation_kwargs):
|
||||
annotation_kwargs['va'] = 'center'
|
||||
imshow = self.imshow(ax, X, extent, label, None, resolution, **imshow_kwargs)
|
||||
xmin, xmax, ymin, ymax = extent
|
||||
self.imshow(X, extent, label, None, resolution, **imshow_kwargs or {})
|
||||
xoffset, yoffset = (xmax - xmin) / (2 * self.resolution), (ymax - ymin) / (2 * self.resolution)
|
||||
xlin = np.linspace(xmin, xmax, self.resolution, endpoint=False)
|
||||
ylin = np.linspace(ymin, ymax, self.resolution, endpoint=False)
|
||||
xoffset, yoffset = (xmax - xmin) / (2. * resolution), (ymax - ymin) / (2. * resolution)
|
||||
xlin = np.linspace(xmin, xmax, resolution, endpoint=False)
|
||||
ylin = np.linspace(ymin, ymax, resolution, endpoint=False)
|
||||
annotations = []
|
||||
for [i, x], [j, y] in itertools.product(enumerate(xlin), enumerate(ylin[::-1])):
|
||||
annotations.append(ax.text(x + xoffset, y + yoffset, "{}".format(annotation[j, i]), **annotation_kwargs))
|
||||
return annotations
|
||||
return [imshow, annotations]
|
||||
|
||||
def contour(self, ax, X, Y, C, levels=20, label=None, **kwargs):
|
||||
return ax.contour(X, Y, C, levels=np.linspace(C.min(), C.max(), levels), label=label, **kwargs)
|
||||
|
|
|
|||
|
|
@ -31,6 +31,18 @@
|
|||
from matplotlib import pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
def legend_ontop(ax, mode='expand', ncol=3, fontdict=None):
|
||||
from mpl_toolkits.axes_grid1 import make_axes_locatable
|
||||
handles, labels = ax.get_legend_handles_labels()
|
||||
divider = make_axes_locatable(ax)
|
||||
cax = divider.append_axes("top", "5%", pad="1%")
|
||||
lgd = cax.legend(handles, labels, bbox_to_anchor=(0., 0., 1., 1.), loc=3,
|
||||
ncol=ncol, mode=mode, borderaxespad=0., prop=fontdict or {})
|
||||
cax.set_axis_off()
|
||||
#lgd = cax.legend(bbox_to_anchor=(0., 1.02, 1., 1.02), loc=3,
|
||||
# ncol=ncol, mode=mode, borderaxespad=0., prop=fontdict or {})
|
||||
return lgd
|
||||
|
||||
def removeRightTicks(ax=None):
|
||||
ax = ax or plt.gca()
|
||||
for i, line in enumerate(ax.get_yticklines()):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue