mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-05 01:32:40 +02:00
[plotting] library is unfolding and should be working tonight
This commit is contained in:
parent
a6c0d82ef7
commit
c3afb4eaaf
13 changed files with 648 additions and 263 deletions
|
|
@ -1,54 +1,6 @@
|
|||
# Copyright (c) 2014, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
from matplotlib import pyplot as plt
|
||||
from . import defaults
|
||||
|
||||
def get_new_canvas(kwargs):
|
||||
"""
|
||||
Return a canvas, kwargupdate for matplotlib. This just a
|
||||
dictionary for the collection and we add the an axis to kwarg.
|
||||
|
||||
This method does two things, it creates an empty canvas
|
||||
and updates the kwargs (deletes the unnecessary kwargs)
|
||||
for further usage in normal plotting.
|
||||
|
||||
in matplotlib this means it deletes references to ax, as
|
||||
plotting is done on the axis itself and is not a kwarg.
|
||||
"""
|
||||
if 'ax' in kwargs:
|
||||
ax = kwargs.pop('ax')
|
||||
elif 'num' in kwargs and 'figsize' in kwargs:
|
||||
ax = plt.figure(num=kwargs.pop('num'), figsize=kwargs.pop('figsize')).add_subplot(111)
|
||||
elif 'num' in kwargs:
|
||||
ax = plt.figure(num=kwargs.pop('num')).add_subplot(111)
|
||||
elif 'figsize' in kwargs:
|
||||
ax = plt.figure(figsize=kwargs.pop('figsize')).add_subplot(111)
|
||||
else:
|
||||
ax = plt.figure().add_subplot(111)
|
||||
# Add ax to kwargs to add all subsequent plots to this axis:
|
||||
#kwargs['ax'] = ax
|
||||
return ax, kwargs
|
||||
|
||||
def show_canvas(canvas):
|
||||
try:
|
||||
canvas.figure.canvas.draw()
|
||||
canvas.figure.tight_layout()
|
||||
except:
|
||||
pass
|
||||
return canvas
|
||||
|
||||
|
||||
def scatter(ax, *args, **kwargs):
|
||||
ax.scatter(*args, **kwargs)
|
||||
|
||||
def plot(ax, *args, **kwargs):
|
||||
ax.plot(*args, **kwargs)
|
||||
|
||||
def imshow(ax, *args, **kwargs):
|
||||
ax.imshow(*args, **kwargs)
|
||||
|
||||
|
||||
from . import base_plots
|
||||
from . import models_plots
|
||||
from . import priors_plots
|
||||
|
|
|
|||
|
|
@ -39,12 +39,15 @@ it gives back an empty default, when defaults are not defined.
|
|||
'''
|
||||
|
||||
from matplotlib import cm
|
||||
from . import Tango
|
||||
|
||||
# Data:
|
||||
data_1d = dict(lw=1.5, marker='x', edgecolor='k')
|
||||
data_2d = dict(s=35, edgecolors='none', linewidth=0., cmap=cm.get_cmap('hot'))
|
||||
xerrorbar = dict(ecolor='k', fmt='none', elinewidth=.5, alpha=.5)
|
||||
yerrorbar = dict(ecolor='darkred', fmt='none', elinewidth=.5, alpha=.5)
|
||||
yerrorbar = dict(ecolor=Tango.colorsHex['darkBlue'], fmt='none', elinewidth=.5, alpha=.5)
|
||||
|
||||
# GP plots
|
||||
meanplot = dict(color='#3300FF', linewidth=2)
|
||||
meanplot_1d = dict(color=Tango.colorsHex['mediumBlue'], linewidth=2)
|
||||
meanplot_2d = dict(cmap='hot', linewidth=.5)
|
||||
confidence_interval = dict(linecolor=Tango.colorsHex['darkBlue'],fillcolor=Tango.colorsHex['lightBlue'])
|
||||
|
|
@ -259,12 +259,12 @@ def plot_fit(self, plot_limits=None, which_data_rows='all',
|
|||
|
||||
#define the frame for plotting on
|
||||
resolution = resolution or 50
|
||||
Xnew, _, _, xmin, xmax = x_frame2D(X[:,free_dims], plot_limits, resolution)
|
||||
Xnew, x, y, xmin, xmax = x_frame2D(X[:,free_dims], plot_limits, resolution)
|
||||
Xgrid = np.empty((Xnew.shape[0],self.input_dim))
|
||||
Xgrid[:,free_dims] = Xnew
|
||||
for i,v in fixed_inputs:
|
||||
Xgrid[:,i] = v
|
||||
x, y = np.linspace(xmin[0], xmax[0], resolution), np.linspace(xmin[1], xmax[1], resolution)
|
||||
#x, y = np.linspace(xmin[0], xmax[0], resolution), np.linspace(xmin[1], xmax[1], resolution)
|
||||
|
||||
#predict on the frame and plot
|
||||
if plot_raw:
|
||||
|
|
|
|||
166
GPy/plotting/matplot_dep/plot_definitions.py
Normal file
166
GPy/plotting/matplot_dep/plot_definitions.py
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
#===============================================================================
|
||||
# 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
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#===============================================================================
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
from ..abstract_plotting_library import AbstractPlottingLibrary
|
||||
from . import defaults
|
||||
|
||||
class MatplotlibPlots(AbstractPlottingLibrary):
|
||||
def __init__(self):
|
||||
super(MatplotlibPlots, self).__init__()
|
||||
self._defaults = defaults.__dict__
|
||||
|
||||
def get_new_canvas(self, kwargs):
|
||||
if 'ax' in kwargs:
|
||||
ax = kwargs.pop('ax')
|
||||
elif 'num' in kwargs and 'figsize' in kwargs:
|
||||
ax = plt.figure(num=kwargs.pop('num'), figsize=kwargs.pop('figsize')).add_subplot(111)
|
||||
elif 'num' in kwargs:
|
||||
ax = plt.figure(num=kwargs.pop('num')).add_subplot(111)
|
||||
elif 'figsize' in kwargs:
|
||||
ax = plt.figure(figsize=kwargs.pop('figsize')).add_subplot(111)
|
||||
else:
|
||||
ax = plt.figure().add_subplot(111)
|
||||
# Add ax to kwargs to add all subsequent plots to this axis:
|
||||
#kwargs['ax'] = ax
|
||||
return ax, kwargs
|
||||
|
||||
def show_canvas(self, ax, plots):
|
||||
try:
|
||||
ax.autoscale_view()
|
||||
ax.figure.canvas.draw()
|
||||
ax.figure.tight_layout()
|
||||
except:
|
||||
pass
|
||||
return ax
|
||||
|
||||
def scatter(self, ax, X, Y, **kwargs):
|
||||
return ax.scatter(X, Y, **kwargs)
|
||||
|
||||
def plot(self, ax, X, Y, **kwargs):
|
||||
return ax.plot(X, Y, **kwargs)
|
||||
|
||||
def xerrorbar(self, ax, X, Y, error, **kwargs):
|
||||
if not('linestyle' in kwargs or 'ls' in kwargs):
|
||||
kwargs['ls'] = 'none'
|
||||
return ax.errorbar(X, Y, xerr=error, **kwargs)
|
||||
|
||||
def yerrorbar(self, ax, X, Y, error, **kwargs):
|
||||
if not('linestyle' in kwargs or 'ls' in kwargs):
|
||||
kwargs['ls'] = 'none'
|
||||
return ax.errorbar(X, Y, yerr=error, **kwargs)
|
||||
|
||||
def imshow(self, ax, X, **kwargs):
|
||||
return ax.imshow(**kwargs)
|
||||
|
||||
def contour(self, ax, X, Y, C, levels=20, **kwargs):
|
||||
return ax.contour(X, Y, C, levels=np.linspace(C.min(), C.max(), levels), **kwargs)
|
||||
|
||||
def fill_between(self, ax, X, lower, upper, **kwargs):
|
||||
return ax.fill_between(X.flatten(), lower.flatten(), upper.flatten(), **kwargs)
|
||||
|
||||
def fill_gradient(self, canvas, X, percentiles, **kwargs):
|
||||
ax = canvas
|
||||
plots = []
|
||||
if not 'alpha' in kwargs.keys():
|
||||
kwargs['alpha'] = 1./(len(percentiles))
|
||||
|
||||
# pop where from kwargs
|
||||
where = kwargs.pop('where') if 'where' in kwargs else None
|
||||
# pop interpolate, which we actually do not do here!
|
||||
if 'interpolate' in kwargs: kwargs.pop('interpolate')
|
||||
|
||||
def pairwise(inlist):
|
||||
l = len(inlist)
|
||||
for i in range(int(np.ceil(l/2.))):
|
||||
yield inlist[:][i], inlist[:][(l-1)-i]
|
||||
|
||||
polycol = []
|
||||
for y1, y2 in pairwise(percentiles):
|
||||
import matplotlib.mlab as mlab
|
||||
# Handle united data, such as dates
|
||||
ax._process_unit_info(xdata=X, ydata=y1)
|
||||
ax._process_unit_info(ydata=y2)
|
||||
# Convert the arrays so we can work with them
|
||||
from numpy import ma
|
||||
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")
|
||||
|
||||
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)
|
||||
X = 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]
|
||||
|
||||
X[0] = start
|
||||
X[N + 1] = end
|
||||
|
||||
X[1:N + 1, 0] = xslice
|
||||
X[1:N + 1, 1] = y1slice
|
||||
X[N + 2:, 0] = xslice[::-1]
|
||||
X[N + 2:, 1] = y2slice[::-1]
|
||||
|
||||
polys.append(X)
|
||||
polycol.extend(polys)
|
||||
from matplotlib.collections import PolyCollection
|
||||
plots.append(PolyCollection(polycol, **kwargs))
|
||||
ax.add_collection(plots[-1], autolim=True)
|
||||
ax.autoscale_view()
|
||||
return plots
|
||||
Loading…
Add table
Add a link
Reference in a new issue