示例#1
0
def plot_deep(model, xlim=None, Nsamples=0):
    if model.layerX.input_dim==1 and model.layerY.output_dim==1:
        fig, ax1 = plt.subplots(1)
        if xlim is None:
            Xnew, xmin, xmax = x_frame1D(model.layerX.X, resolution=200)
        else:
            xmin = xlim[0]
            xmax = xlim[1]
            Xnew = np.linspace(xmin, xmax, 200)[:, None]
        Xnew = np.linspace(xmin,xmax,200)[:,None]
        s = model.predict_sampling(Xnew, 1000)
        yTest = model.predict_means(Xnew)[0]
        H, xedges, yedges = np.histogram2d(np.repeat(Xnew.T,  1000, 0).flatten(), 
                                           s.flatten(), 
                                           bins=[Xnew.flatten(),
                                                 np.linspace(s.min(),s.max(),50)])
        ax1.imshow(H.T, 
                   extent=[xedges.min(), xedges.max(),
                           yedges.min(), yedges.max()], 
                   cmap=plt.cm.Blues, 
                   interpolation='nearest',
                   origin='lower',
                   aspect='auto')
        ax1.plot(model.layerX.X, model.layerY.Y, 'kx', mew=1.3)
        ax1.plot(Xnew.flatten(), yTest.flatten())
        ax1.set_ylim(yedges.min(), yedges.max())
        ax1.set_xlim(xmin, xmax)

        for n in range(Nsamples):
            Y = model.posterior_sample(Xnew)
            ax1.plot(Xnew, Y, 'r', lw=1.4)
示例#2
0
文件: plotting.py 项目: zhmxu/deepGPy
def plot_hidden_layer(layer):
    if layer.input_dim == 1:
        fig = plt.figure()
        ax1 = fig.add_axes([0.2, 0.2, 0.7, 0.7])

        Xnew, xmin, xmax = x_frame1D(np.vstack(
            (layer.Z * 1, layer.q_of_X_in.mean * 1)),
                                     resolution=200)

        sausage_plot(layer, Xnew, ax1)
        errorbars(layer, ax1)
        plt.setp(ax1.get_xticklabels(), visible=False)
        plt.setp(ax1.get_yticklabels(), visible=False)

        #do the gaussians for the input
        ax2 = fig.add_axes([0.2, 0.1, 0.7, 0.1], sharex=ax1)
        ax2.set_yticks([])
        plot_gaussians(layer.q_of_X_in, ax2, (xmin, xmax))
        ax2.set_xlim(xmin, xmax)
        #ax2.set_ylim(ax2.get_ylim()[::-1])

        ax3 = fig.add_axes([0.1, 0.2, 0.1, 0.7], sharey=ax1)
        plot_gaussians(layer.q_of_X_out, ax3, ax1.get_ylim(), vertical=True)
        ax3.set_xticks([])
        ax3.set_xlim(ax3.get_xlim()[::-1])
示例#3
0
文件: plotting.py 项目: zhmxu/deepGPy
def plot_deep(model, xlim=None, Nsamples=0):
    if model.layerX.input_dim == 1 and model.layerY.output_dim == 1:
        fig, ax1 = plt.subplots(1)
        if xlim is None:
            Xnew, xmin, xmax = x_frame1D(model.layerX.X, resolution=200)
        else:
            xmin = xlim[0]
            xmax = xlim[1]
            Xnew = np.linspace(xmin, xmax, 200)[:, None]
        Xnew = np.linspace(xmin, xmax, 200)[:, None]
        s = model.predict_sampling(Xnew, 1000)
        yTest = model.predict_means(Xnew)[0]
        H, xedges, yedges = np.histogram2d(
            np.repeat(Xnew.T, 1000, 0).flatten(),
            s.flatten(),
            bins=[Xnew.flatten(),
                  np.linspace(s.min(), s.max(), 50)])
        ax1.imshow(
            H.T,
            extent=[xedges.min(),
                    xedges.max(),
                    yedges.min(),
                    yedges.max()],
            cmap=plt.cm.Blues,
            interpolation='nearest',
            origin='lower',
            aspect='auto')
        ax1.plot(model.layerX.X, model.layerY.Y, 'kx', mew=1.3)
        ax1.plot(Xnew.flatten(), yTest.flatten())
        ax1.set_ylim(yedges.min(), yedges.max())
        ax1.set_xlim(xmin, xmax)

        for n in range(Nsamples):
            Y = model.posterior_sample(Xnew)
            ax1.plot(Xnew, Y, 'r', lw=1.4)
示例#4
0
    def plot(self,
             plot_limits=None,
             levels=20,
             samples=0,
             fignum=None,
             ax=None,
             resolution=None,
             plot_raw=False,
             plot_filter=False,
             linecol=Tango.colorsHex['darkBlue'],
             fillcol=Tango.colorsHex['lightBlue']):

        # Deal with optional parameters
        if ax is None:
            fig = pb.figure(num=fignum)
            ax = fig.add_subplot(111)

        # Define the frame on which to plot
        resolution = resolution or 200
        Xgrid, xmin, xmax = x_frame1D(self.X, plot_limits=plot_limits)

        # Make a prediction on the frame and plot it
        if plot_raw:
            m, v = self.predict_raw(Xgrid, filteronly=plot_filter)
            lower = m - 2 * np.sqrt(v)
            upper = m + 2 * np.sqrt(v)
            Y = self.Y
        else:
            m, v, lower, upper = self.predict(Xgrid, filteronly=plot_filter)
            Y = self.Y

        # Plot the values
        gpplot(Xgrid,
               m,
               lower,
               upper,
               axes=ax,
               edgecol=linecol,
               fillcol=fillcol)
        ax.plot(self.X, self.Y, 'kx', mew=1.5)

        # Optionally plot some samples
        if samples:
            if plot_raw:
                Ysim = self.posterior_samples_f(Xgrid, samples)
            else:
                Ysim = self.posterior_samples(Xgrid, samples)
            for yi in Ysim.T:
                ax.plot(Xgrid, yi, Tango.colorsHex['darkBlue'], linewidth=0.25)

        # Set the limits of the plot to some sensible values
        ymin, ymax = min(np.append(Y.flatten(), lower.flatten())), max(
            np.append(Y.flatten(), upper.flatten()))
        ymin, ymax = ymin - 0.1 * (ymax - ymin), ymax + 0.1 * (ymax - ymin)
        ax.set_xlim(xmin, xmax)
        ax.set_ylim(ymin, ymax)
示例#5
0
def plot_gaussians(q, ax,limits=None, vertical=False, color='k'):
    if limits is None:
        Xnew, xmin, xmax = x_frame1D(q.mean, resolution=2000)
    else:
        xmin, xmax = limits
        Xnew = np.linspace(xmin, xmax, 2000)[:,None]

    #compute Gaussian densities
    log_density = -0.5*np.log(2*np.pi) -0.5*np.log(q.variance) -0.5*(q.mean-Xnew.T)**2/q.variance
    density = np.exp(log_density)
    if vertical:
        [ax.plot(d, Xnew[:,0], color, linewidth=1.) for d in density]
        [ax.fill(d, Xnew[:,0], color=color, linewidth=0., alpha=0.2) for d in density]
    else:
        ax.plot(Xnew, density.T, color, linewidth=1.)
        ax.fill(Xnew, density.T, color=color, linewidth=0., alpha=0.2)
示例#6
0
文件: plotting.py 项目: zhmxu/deepGPy
def plot_output_layer(layer):
    if layer.input_dim == 1:
        fig = plt.figure()
        ax1 = fig.add_axes([0.1, 0.2, 0.8, 0.7])

        Xnew, xmin, xmax = x_frame1D(layer.Z, resolution=200)

        sausage_plot(layer, Xnew, ax1)
        errorbars(layer, ax1)
        plt.setp(ax1.get_xticklabels(), visible=False)

        #plot the data
        ax1.plot(layer.q_of_X_in.mean * 1., layer.Y, 'kx', mew=2, ms=9)

        #do the gaussians for the input
        ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.1], sharex=ax1)
        ax2.set_yticks([])
        plot_gaussians(layer.q_of_X_in, ax2, (xmin, xmax))
        ax2.set_xlim(xmin, xmax)
示例#7
0
def plot_output_layer(layer):
    if layer.input_dim==1:
        fig = plt.figure()
        ax1 = fig.add_axes([0.1, 0.2, 0.8, 0.7])

        Xnew, xmin, xmax = x_frame1D(layer.Z, resolution=200)

        sausage_plot(layer, Xnew, ax1)
        errorbars(layer, ax1)
        plt.setp(ax1.get_xticklabels(), visible=False)

        #plot the data
        ax1.plot(layer.q_of_X_in.mean*1., layer.Y, 'kx', mew=2, ms=9 )

        #do the gaussians for the input
        ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.1], sharex=ax1)
        ax2.set_yticks([])
        plot_gaussians(layer.q_of_X_in, ax2, (xmin, xmax))
        ax2.set_xlim(xmin, xmax)
示例#8
0
文件: plotting.py 项目: zhmxu/deepGPy
def plot_gaussians(q, ax, limits=None, vertical=False, color='k'):
    if limits is None:
        Xnew, xmin, xmax = x_frame1D(q.mean, resolution=2000)
    else:
        xmin, xmax = limits
        Xnew = np.linspace(xmin, xmax, 2000)[:, None]

    #compute Gaussian densities
    log_density = -0.5 * np.log(2 * np.pi) - 0.5 * np.log(
        q.variance) - 0.5 * (q.mean - Xnew.T)**2 / q.variance
    density = np.exp(log_density)
    if vertical:
        [ax.plot(d, Xnew[:, 0], color, linewidth=1.) for d in density]
        [
            ax.fill(d, Xnew[:, 0], color=color, linewidth=0., alpha=0.2)
            for d in density
        ]
    else:
        ax.plot(Xnew, density.T, color, linewidth=1.)
        ax.fill(Xnew, density.T, color=color, linewidth=0., alpha=0.2)
示例#9
0
文件: plotting.py 项目: zhmxu/deepGPy
def plot_input_layer(layer):
    if layer.input_dim == 1:
        fig = plt.figure()
        ax1 = fig.add_axes([0.2, 0.2, 0.7, 0.7])

        Xnew, xmin, xmax = x_frame1D(layer.Z, resolution=200)

        sausage_plot(layer, Xnew, ax1)
        errorbars(layer, ax1)
        plt.setp(ax1.get_xticklabels(), visible=False)
        plt.setp(ax1.get_yticklabels(), visible=False)

        #do crosses for the input
        ax2 = fig.add_axes([0.2, 0.1, 0.7, 0.1], sharex=ax1)
        ax2.set_yticks([])
        ax2.plot(layer.X * 1, layer.X * 0, 'kx', mew=2., ms=9)
        ax2.set_xlim(xmin, xmax)

        ax3 = fig.add_axes([0.1, 0.2, 0.1, 0.7], sharey=ax1)
        plot_gaussians(layer.q_of_X_out, ax3, vertical=True)
        ax3.set_xticks([])
        ax3.set_xlim(ax3.get_xlim()[::-1])
示例#10
0
def plot_input_layer(layer):
    if layer.input_dim==1:
        fig = plt.figure()
        ax1 = fig.add_axes([0.2, 0.2, 0.7, 0.7])

        Xnew, xmin, xmax = x_frame1D(layer.Z, resolution=200)

        sausage_plot(layer, Xnew, ax1)
        errorbars(layer, ax1)
        plt.setp(ax1.get_xticklabels(), visible=False)
        plt.setp(ax1.get_yticklabels(), visible=False)

        #do crosses for the input
        ax2 = fig.add_axes([0.2, 0.1, 0.7, 0.1], sharex=ax1)
        ax2.set_yticks([])
        ax2.plot(layer.X*1, layer.X*0, 'kx', mew=2., ms=9)
        ax2.set_xlim(xmin, xmax)

        ax3 = fig.add_axes([0.1, 0.2, 0.1, 0.7], sharey=ax1)
        plot_gaussians(layer.q_of_X_out, ax3,vertical=True)
        ax3.set_xticks([])
        ax3.set_xlim(ax3.get_xlim()[::-1])
示例#11
0
def plot_hidden_layer(layer):
    if layer.input_dim==1:
        fig = plt.figure()
        ax1 = fig.add_axes([0.2, 0.2, 0.7, 0.7])

        Xnew, xmin, xmax = x_frame1D(np.vstack((layer.Z*1, layer.q_of_X_in.mean*1)), resolution=200)

        sausage_plot(layer, Xnew, ax1)
        errorbars(layer, ax1)
        plt.setp(ax1.get_xticklabels(), visible=False)
        plt.setp(ax1.get_yticklabels(), visible=False)

        #do the gaussians for the input
        ax2 = fig.add_axes([0.2, 0.1, 0.7, 0.1], sharex=ax1)
        ax2.set_yticks([])
        plot_gaussians(layer.q_of_X_in, ax2, (xmin, xmax))
        ax2.set_xlim(xmin, xmax)
        #ax2.set_ylim(ax2.get_ylim()[::-1])

        ax3 = fig.add_axes([0.1, 0.2, 0.1, 0.7], sharey=ax1)
        plot_gaussians(layer.q_of_X_out, ax3, ax1.get_ylim(), vertical=True)
        ax3.set_xticks([])
        ax3.set_xlim(ax3.get_xlim()[::-1])
示例#12
0
文件: plot.py 项目: jfutoma/savigp
def plot_fit(model, plot_limits=None, which_data_rows='all',
        which_data_ycols='all', fixed_inputs=[],
        levels=20, samples=0, fignum=None, ax=None, resolution=None,
        plot_raw=False,
        linecol='darkBlue',fillcol='lightBlue', Y_metadata=None, data_symbol='kx'):
    """
    Plot the posterior of the GP.
      - In one dimension, the function is plotted with a shaded region identifying two standard deviations.
      - In two dimsensions, a contour-plot shows the mean predicted function
      - In higher dimensions, use fixed_inputs to plot the GP  with some of the inputs fixed.

    Can plot only part of the data and part of the posterior functions
    using which_data_rowsm which_data_ycols.

    :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
    :type plot_limits: np.array
    :param which_data_rows: which of the training data to plot (default all)
    :type which_data_rows: 'all' or a slice object to slice model.X, model.Y
    :param which_data_ycols: when the data has several columns (independant outputs), only plot these
    :type which_data_rows: 'all' or a list of integers
    :param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input index i should be set to value v.
    :type fixed_inputs: a list of tuples
    :param resolution: the number of intervals to sample the GP on. Defaults to 200 in 1D and 50 (a 50x50 grid) in 2D
    :type resolution: int
    :param levels: number of levels to plot in a contour plot.
    :type levels: int
    :param samples: the number of a posteriori samples to plot
    :type samples: int
    :param fignum: figure to plot on.
    :type fignum: figure number
    :param ax: axes to plot on.
    :type ax: axes handle
    :type output: integer (first output is 0)
    :param linecol: color of line to plot.
    :type linecol:
    :param fillcol: color of fill
    :param levels: for 2D plotting, the number of contour levels to use is ax is None, create a new figure
    """
    #deal with optional arguments
    if which_data_rows == 'all':
        which_data_rows = slice(None)
    if which_data_ycols == 'all':
        which_data_ycols = np.arange(model.output_dim)
    #if len(which_data_ycols)==0:
        #raise ValueError('No data selected for plotting')
    if ax is None:
        fig = pb.figure(num=fignum)
        ax = fig.add_subplot(111)

    if hasattr(model, 'has_uncertain_inputs') and model.has_uncertain_inputs():
        X = model.X.mean
        X_variance = model.X.variance
    else:
        X = model.X
    Y = model.Y

    if hasattr(model, 'Z'): Z = model.Z

    #work out what the inputs are for plotting (1D or 2D)
    fixed_dims = np.array([i for i,v in fixed_inputs])
    free_dims = np.setdiff1d(np.arange(model.input_dim),fixed_dims)
    plots = {}
    #one dimensional plotting
    if len(free_dims) == 1:

        #define the frame on which to plot
        Xnew, xmin, xmax = x_frame1D(X[:,free_dims], plot_limits=plot_limits, resolution=resolution or 200)
        Xgrid = np.empty((Xnew.shape[0],model.input_dim))
        Xgrid[:,free_dims] = Xnew
        for i,v in fixed_inputs:
            Xgrid[:,i] = v

        #make a prediction on the frame and plot it
        m, v = model.predict(Xgrid)
        lower = m - 2*np.sqrt(v)
        upper = m + 2*np.sqrt(v)


        for d in which_data_ycols:
            plots['gpplot'] = gpplot(Xnew, m[:, d], lower[:, d], upper[:, d], ax=ax, edgecol=linecol, fillcol=fillcol)
            plots['dataplot'] = ax.plot(X[which_data_rows,free_dims], Y[which_data_rows, d], data_symbol, mew=1.5)

        #optionally plot some samples
        if samples: #NOTE not tested with fixed_inputs
            Ysim = model.posterior_samples(Xgrid, samples)
            for yi in Ysim.T:
                plots['posterior_samples'] = ax.plot(Xnew, yi[:,None], Tango.colorsHex['darkBlue'], linewidth=0.25)
                #ax.plot(Xnew, yi[:,None], marker='x', linestyle='--',color=Tango.colorsHex['darkBlue']) #TODO apply this line for discrete outputs.


        #add error bars for uncertain (if input uncertainty is being modelled)
        if hasattr(model,"has_uncertain_inputs") and model.has_uncertain_inputs():
            plots['xerrorbar'] = ax.errorbar(X[which_data_rows, free_dims].flatten(), Y[which_data_rows, which_data_ycols].flatten(),
                        xerr=2 * np.sqrt(X_variance[which_data_rows, free_dims].flatten()),
                        ecolor='k', fmt=None, elinewidth=.5, alpha=.5)


        #set the limits of the plot to some sensible values
        ymin, ymax = min(np.append(Y[which_data_rows, which_data_ycols].flatten(), lower)), max(np.append(Y[which_data_rows, which_data_ycols].flatten(), upper))
        ymin, ymax = ymin - 0.1 * (ymax - ymin), ymax + 0.1 * (ymax - ymin)
        ax.set_xlim(xmin, xmax)
        ax.set_ylim(ymin, ymax)



    #2D plotting
    elif len(free_dims) == 2:

        #define the frame for plotting on
        resolution = resolution or 50
        Xnew, _, _, xmin, xmax = x_frame2D(X[:,free_dims], plot_limits, resolution)
        Xgrid = np.empty((Xnew.shape[0],model.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)

        #predict on the frame and plot
        if plot_raw:
            m, _ = model.predict(Xgrid)
        else:
            if isinstance(model,GPCoregionalizedRegression) or isinstance(model,SparseGPCoregionalizedRegression):
                meta = {'output_index': Xgrid[:,-1:].astype(np.int)}
            else:
                meta = None
            m, v = model.predict(Xgrid, full_cov=False, Y_metadata=meta)
        for d in which_data_ycols:
            m_d = m[:,d].reshape(resolution, resolution).T
            plots['contour'] = ax.contour(x, y, m_d, levels, vmin=m.min(), vmax=m.max(), cmap=pb.cm.jet)
            if not plot_raw: plots['dataplot'] = ax.scatter(X[which_data_rows, free_dims[0]], X[which_data_rows, free_dims[1]], 40, Y[which_data_rows, d], cmap=pb.cm.jet, vmin=m.min(), vmax=m.max(), linewidth=0.)

        #set the limits of the plot to some sensible values
        ax.set_xlim(xmin[0], xmax[0])
        ax.set_ylim(xmin[1], xmax[1])

        if samples:
            warnings.warn("Samples are rather difficult to plot for 2D inputs...")

        #add inducing inputs (if a sparse model is used)
        if hasattr(model,"Z"):
            #Zu = model.Z[:,free_dims] * model._Xscale[:,free_dims] + model._Xoffset[:,free_dims]
            Zu = Z[:,free_dims]
            plots['inducing_inputs'] = ax.plot(Zu[:,free_dims[0]], Zu[:,free_dims[1]], 'wo')

    else:
        raise NotImplementedError, "Cannot define a frame with more than two input dimensions"
    return plots
示例#13
0
文件: plot.py 项目: zphilip/Sparse-GP
def plot_fit(model,
             plot_limits=None,
             which_data_rows='all',
             which_data_ycols='all',
             fixed_inputs=[],
             levels=20,
             samples=0,
             fignum=None,
             ax=None,
             resolution=None,
             plot_raw=False,
             linecol='darkBlue',
             fillcol='lightBlue',
             Y_metadata=None,
             data_symbol='kx'):
    """
    Plot the posterior of the GP.
      - In one dimension, the function is plotted with a shaded region identifying two standard deviations.
      - In two dimsensions, a contour-plot shows the mean predicted function
      - In higher dimensions, use fixed_inputs to plot the GP  with some of the inputs fixed.

    Can plot only part of the data and part of the posterior functions
    using which_data_rowsm which_data_ycols.

    :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
    :type plot_limits: np.array
    :param which_data_rows: which of the training data to plot (default all)
    :type which_data_rows: 'all' or a slice object to slice model.X, model.Y
    :param which_data_ycols: when the data has several columns (independant outputs), only plot these
    :type which_data_rows: 'all' or a list of integers
    :param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input index i should be set to value v.
    :type fixed_inputs: a list of tuples
    :param resolution: the number of intervals to sample the GP on. Defaults to 200 in 1D and 50 (a 50x50 grid) in 2D
    :type resolution: int
    :param levels: number of levels to plot in a contour plot.
    :type levels: int
    :param samples: the number of a posteriori samples to plot
    :type samples: int
    :param fignum: figure to plot on.
    :type fignum: figure number
    :param ax: axes to plot on.
    :type ax: axes handle
    :type output: integer (first output is 0)
    :param linecol: color of line to plot.
    :type linecol:
    :param fillcol: color of fill
    :param levels: for 2D plotting, the number of contour levels to use is ax is None, create a new figure
    """
    #deal with optional arguments
    if which_data_rows == 'all':
        which_data_rows = slice(None)
    if which_data_ycols == 'all':
        which_data_ycols = np.arange(model.output_dim)
    #if len(which_data_ycols)==0:
    #raise ValueError('No data selected for plotting')
    if ax is None:
        fig = pb.figure(num=fignum)
        ax = fig.add_subplot(111)

    if hasattr(model, 'has_uncertain_inputs') and model.has_uncertain_inputs():
        X = model.X.mean
        X_variance = model.X.variance
    else:
        X = model.X
    Y = model.Y

    if hasattr(model, 'Z'): Z = model.Z

    #work out what the inputs are for plotting (1D or 2D)
    fixed_dims = np.array([i for i, v in fixed_inputs])
    free_dims = np.setdiff1d(np.arange(model.input_dim), fixed_dims)
    plots = {}
    #one dimensional plotting
    if len(free_dims) == 1:

        #define the frame on which to plot
        Xnew, xmin, xmax = x_frame1D(X[:, free_dims],
                                     plot_limits=plot_limits,
                                     resolution=resolution or 200)
        Xgrid = np.empty((Xnew.shape[0], model.input_dim))
        Xgrid[:, free_dims] = Xnew
        for i, v in fixed_inputs:
            Xgrid[:, i] = v

        #make a prediction on the frame and plot it
        m, v = model.predict(Xgrid)
        lower = m - 2 * np.sqrt(v)
        upper = m + 2 * np.sqrt(v)

        for d in which_data_ycols:
            plots['gpplot'] = gpplot(Xnew,
                                     m[:, d],
                                     lower[:, d],
                                     upper[:, d],
                                     ax=ax,
                                     edgecol=linecol,
                                     fillcol=fillcol)
            plots['dataplot'] = ax.plot(X[which_data_rows, free_dims],
                                        Y[which_data_rows, d],
                                        data_symbol,
                                        mew=1.5)

        #optionally plot some samples
        if samples:  #NOTE not tested with fixed_inputs
            Ysim = model.posterior_samples(Xgrid, samples)
            for yi in Ysim.T:
                plots['posterior_samples'] = ax.plot(
                    Xnew,
                    yi[:, None],
                    Tango.colorsHex['darkBlue'],
                    linewidth=0.25)
                #ax.plot(Xnew, yi[:,None], marker='x', linestyle='--',color=Tango.colorsHex['darkBlue']) #TODO apply this line for discrete outputs.

        #add error bars for uncertain (if input uncertainty is being modelled)
        if hasattr(model,
                   "has_uncertain_inputs") and model.has_uncertain_inputs():
            plots['xerrorbar'] = ax.errorbar(
                X[which_data_rows, free_dims].flatten(),
                Y[which_data_rows, which_data_ycols].flatten(),
                xerr=2 *
                np.sqrt(X_variance[which_data_rows, free_dims].flatten()),
                ecolor='k',
                fmt=None,
                elinewidth=.5,
                alpha=.5)

        #set the limits of the plot to some sensible values
        ymin, ymax = min(
            np.append(Y[which_data_rows, which_data_ycols].flatten(),
                      lower)), max(
                          np.append(
                              Y[which_data_rows, which_data_ycols].flatten(),
                              upper))
        ymin, ymax = ymin - 0.1 * (ymax - ymin), ymax + 0.1 * (ymax - ymin)
        ax.set_xlim(xmin, xmax)
        ax.set_ylim(ymin, ymax)

    #2D plotting
    elif len(free_dims) == 2:

        #define the frame for plotting on
        resolution = resolution or 50
        Xnew, _, _, xmin, xmax = x_frame2D(X[:, free_dims], plot_limits,
                                           resolution)
        Xgrid = np.empty((Xnew.shape[0], model.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)

        #predict on the frame and plot
        if plot_raw:
            m, _ = model.predict(Xgrid)
        else:
            if isinstance(model, GPCoregionalizedRegression) or isinstance(
                    model, SparseGPCoregionalizedRegression):
                meta = {'output_index': Xgrid[:, -1:].astype(np.int)}
            else:
                meta = None
            m, v = model.predict(Xgrid, full_cov=False, Y_metadata=meta)
        for d in which_data_ycols:
            m_d = m[:, d].reshape(resolution, resolution).T
            plots['contour'] = ax.contour(x,
                                          y,
                                          m_d,
                                          levels,
                                          vmin=m.min(),
                                          vmax=m.max(),
                                          cmap=pb.cm.jet)
            if not plot_raw:
                plots['dataplot'] = ax.scatter(X[which_data_rows,
                                                 free_dims[0]],
                                               X[which_data_rows,
                                                 free_dims[1]],
                                               40,
                                               Y[which_data_rows, d],
                                               cmap=pb.cm.jet,
                                               vmin=m.min(),
                                               vmax=m.max(),
                                               linewidth=0.)

        #set the limits of the plot to some sensible values
        ax.set_xlim(xmin[0], xmax[0])
        ax.set_ylim(xmin[1], xmax[1])

        if samples:
            warnings.warn(
                "Samples are rather difficult to plot for 2D inputs...")

        #add inducing inputs (if a sparse model is used)
        if hasattr(model, "Z"):
            #Zu = model.Z[:,free_dims] * model._Xscale[:,free_dims] + model._Xoffset[:,free_dims]
            Zu = Z[:, free_dims]
            plots['inducing_inputs'] = ax.plot(Zu[:, free_dims[0]],
                                               Zu[:, free_dims[1]], 'wo')

    else:
        raise NotImplementedError, "Cannot define a frame with more than two input dimensions"
    return plots