diff --git a/GPy/__init__.py b/GPy/__init__.py index f7d6e1d6..32f0c1c4 100644 --- a/GPy/__init__.py +++ b/GPy/__init__.py @@ -17,8 +17,6 @@ from numpy.testing import Tester from . import kern from . import plotting -from .plotting import plotting_library - # Direct imports for convenience: from .core import Model from .core.parameterization import Param, Parameterized, ObsAr diff --git a/GPy/plotting/matplot_dep/controllers/axis_event_controller.py b/GPy/plotting/matplot_dep/controllers/axis_event_controller.py index 387f2ae2..0e721d8d 100644 --- a/GPy/plotting/matplot_dep/controllers/axis_event_controller.py +++ b/GPy/plotting/matplot_dep/controllers/axis_event_controller.py @@ -97,22 +97,19 @@ class BufferedAxisChangedController(AxisChangedController): :param kwargs: additional kwargs are for pyplot.imshow(**kwargs) """ super(BufferedAxisChangedController, self).__init__(ax, update_lim=update_lim) + self.resolution = resolution self.plot_function = plot_function 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 - self.resolution = resolution - self._not_init = False self.view = self._init_view(self.ax, self.recompute_X(buffered=False), xmin, xmax, ymin, ymax, **kwargs) - self._not_init = True def update(self, ax): super(BufferedAxisChangedController, self).update(ax) - if self._not_init: - xmin, xmax = self._compute_buffered(*self._x_lim) - ymin, ymax = self._compute_buffered(*self._y_lim) - self.update_view(self.view, self.recompute_X(), xmin, xmax, ymin, ymax) + xmin, xmax = self._compute_buffered(*self._x_lim) + ymin, ymax = self._compute_buffered(*self._y_lim) + self.update_view(self.view, self.recompute_X(), xmin, xmax, ymin, ymax) def _init_view(self, ax, X, xmin, xmax, ymin, ymax): raise NotImplementedError('return view for this controller') diff --git a/GPy/plotting/matplot_dep/controllers/imshow_controller.py b/GPy/plotting/matplot_dep/controllers/imshow_controller.py index a0eb3bef..d67c9b4b 100644 --- a/GPy/plotting/matplot_dep/controllers/imshow_controller.py +++ b/GPy/plotting/matplot_dep/controllers/imshow_controller.py @@ -23,9 +23,9 @@ 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 = 0, 0#self._offsets(xmin, xmax, ymin, ymax) - return canvas.imshow(X, extent=(xmin-xoffset, xmax+xoffset, - ymin-yoffset, ymax+yoffset), + #xoffset, yoffset = 0, 0#self._offsets(xmin, xmax, ymin, ymax) + return canvas.imshow(X, extent=(xmin, xmax, + ymin, ymax), vmin=vmin, vmax=vmax, **kwargs) @@ -36,7 +36,7 @@ class ImshowController(BufferedAxisChangedController): ymin-yoffset, ymax+yoffset)) def _offsets(self, xmin, xmax, ymin, ymax): - return (xmax - xmin) / (2 * self.resolution), (ymax - ymin) / (2 * self.resolution) + return float(xmax - xmin) / (2 * self.resolution), float(ymax - ymin) / (2 * self.resolution) class ImAnnotateController(ImshowController): diff --git a/GPy/plotting/matplot_dep/plot_definitions.py b/GPy/plotting/matplot_dep/plot_definitions.py index 3c6ace0c..a33e6bbe 100644 --- a/GPy/plotting/matplot_dep/plot_definitions.py +++ b/GPy/plotting/matplot_dep/plot_definitions.py @@ -65,10 +65,8 @@ class MatplotlibPlots(AbstractPlottingLibrary): else: fig = self.figure() - 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) + #if hasattr(fig, 'rows') and hasattr(fig, 'cols'): + ax = fig.add_subplot(fig.rows, fig.cols, (col,row), projection=projection) if xlim is not None: ax.set_xlim(xlim) if ylim is not None: ax.set_ylim(ylim) @@ -149,13 +147,14 @@ class MatplotlibPlots(AbstractPlottingLibrary): #xmin, xmax, ymin, ymax = extent = xmin-xoffset, xmax+xoffset, ymin-yoffset, ymax+yoffset return ax.imshow(X, label=label, extent=extent, vmin=vmin, vmax=vmax, **imshow_kwargs) - 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, label=None, resolution=None, vmin=None, vmax=None, **imshow_kwargs): + if imshow_kwargs is None: imshow_kwargs = {} if 'origin' not in imshow_kwargs: imshow_kwargs['origin'] = 'lower' return ImshowController(ax, plot_function, extent, resolution=resolution, vmin=vmin, vmax=vmax, **imshow_kwargs) def annotation_heatmap(self, ax, X, annotation, extent=None, label=None, imshow_kwargs=None, **annotation_kwargs): - imshow_kwargs = imshow_kwargs or {} + if imshow_kwargs is None: imshow_kwargs = {} if 'origin' not in imshow_kwargs: imshow_kwargs['origin'] = 'lower' if ('ha' not in annotation_kwargs) and ('horizontalalignment' not in annotation_kwargs): @@ -175,6 +174,7 @@ class MatplotlibPlots(AbstractPlottingLibrary): return imshow, annotations def annotation_heatmap_interact(self, ax, plot_function, extent, label=None, resolution=15, imshow_kwargs=None, **annotation_kwargs): + if imshow_kwargs is None: imshow_kwargs = {} if 'origin' not in imshow_kwargs: imshow_kwargs['origin'] = 'lower' return ImAnnotateController(ax, plot_function, extent, resolution=resolution, imshow_kwargs=imshow_kwargs or {}, **annotation_kwargs) diff --git a/GPy/testing/plotting_tests.py b/GPy/testing/plotting_tests.py index 7f788261..0c1e21f9 100644 --- a/GPy/testing/plotting_tests.py +++ b/GPy/testing/plotting_tests.py @@ -33,6 +33,7 @@ from nose import SkipTest from ..util.config import config from ..plotting import change_plotting_library +import unittest change_plotting_library('matplotlib') if config.get('plotting', 'library') != 'matplotlib': @@ -93,6 +94,49 @@ def _image_comparison(baseline_images, extensions=['pdf','svg','ong'], tol=11): yield do_test plt.close('all') +def test_figure(): + np.random.seed(1239847) + from GPy.plotting import plotting_library as pl + import matplotlib + matplotlib.rcParams.update(matplotlib.rcParamsDefault) + matplotlib.rcParams[u'figure.figsize'] = (4,3) + matplotlib.rcParams[u'text.usetex'] = False + + ax, _ = pl().new_canvas(num=1) + def test_func(x): + return x[:, 0].reshape(3,3) + pl().imshow_interact(ax, test_func, extent=(-1,1,-1,1), resolution=3) + + ax, _ = pl().new_canvas() + def test_func_2(x): + y = x[:, 0].reshape(3,3) + anno = np.argmax(x, axis=1).reshape(3,3) + return y, anno + pl().annotation_heatmap_interact(ax, test_func_2, extent=(-1,1,-1,1), resolution=3) + pl().annotation_heatmap_interact(ax, test_func_2, extent=(-1,1,-1,1), resolution=3, imshow_kwargs=dict(interpolation='nearest')) + + ax, _ = pl().new_canvas(figsize=(4,3)) + x = np.linspace(0,1,100) + y = [0,1,2] + array = np.array([.4,.5]) + cmap = matplotlib.colors.LinearSegmentedColormap.from_list('WhToColor', ('r', 'b'), N=array.size) + pl().fill_gradient(ax, x, y, facecolors=['r', 'g'], array=array, cmap=cmap) + try: + pl().show_canvas(ax, tight_layout=True) + except: + # macosx tight layout not stable + pl().show_canvas(ax, tight_layout=False) + + ax, _ = pl().new_canvas(num=4, figsize=(4,3), projection='3d', xlabel='x', ylabel='y', zlabel='z', title='awsome title', xlim=(-1,1), ylim=(-1,1), zlim=(-3,3)) + z = 2-np.abs(np.linspace(-2,2,(100)))+1 + x, y = z*np.sin(np.linspace(-2*np.pi,2*np.pi,(100))), z*np.cos(np.linspace(-np.pi,np.pi,(100))) + pl().plot(ax, x, y, z, linewidth=2) + for do_test in _image_comparison( + baseline_images=['coverage_{}'.format(sub) for sub in ["imshow_interact",'annotation_interact','gradient','3d_plot',]], + extensions=extensions): + yield (do_test, ) + + def test_kernel(): np.random.seed(1239847) import matplotlib diff --git a/GPy/testing/plotting_tests/baseline/coverage_3d_plot.png b/GPy/testing/plotting_tests/baseline/coverage_3d_plot.png new file mode 100644 index 00000000..c5469dd0 Binary files /dev/null and b/GPy/testing/plotting_tests/baseline/coverage_3d_plot.png differ diff --git a/GPy/testing/plotting_tests/baseline/coverage_annotation_interact.png b/GPy/testing/plotting_tests/baseline/coverage_annotation_interact.png new file mode 100644 index 00000000..7555a872 Binary files /dev/null and b/GPy/testing/plotting_tests/baseline/coverage_annotation_interact.png differ diff --git a/GPy/testing/plotting_tests/baseline/coverage_gradient.png b/GPy/testing/plotting_tests/baseline/coverage_gradient.png new file mode 100644 index 00000000..de5fb4f3 Binary files /dev/null and b/GPy/testing/plotting_tests/baseline/coverage_gradient.png differ diff --git a/GPy/testing/plotting_tests/baseline/coverage_imshow_interact.png b/GPy/testing/plotting_tests/baseline/coverage_imshow_interact.png new file mode 100644 index 00000000..70c59276 Binary files /dev/null and b/GPy/testing/plotting_tests/baseline/coverage_imshow_interact.png differ