To extend GPy with new plotting capabilities, you should implement your plotting functions within the GPy.plotting hierarchy. All plotting-related code must reside in GPy.plotting or its submodules.
Implementation Steps
- Create a module: Write your plotting function into a module under
GPy.plotting.gpy_plot.<module_name>. - Use the plotting library: Access functionality via the
GPy.plotting.plotting_library. It is recommended to use the pattern from . import plotting_library as pl and access methods via pl().. - Define parameters: The first argument of the plotting function must be
self (to allow attaching the function to a class). Ensure you document all parameters, including kwargs for the specific backend. - Prepare data: Use
helper_for_plot_data from .plot_util to generate the necessary grids (Xgrid, xx, yy) and handle dimension selection. - Create a canvas: Use
pl().new_canvas(...) to initialize the plot. Always pass through kwargs to the new_canvas method to support backend-specific options. - Apply defaults: Use
update_not_existing_kwargs(kwargs, pl().defaults.<default_name>) to ensure your plot respects the standard GPy plotting defaults for that specific backend and plot type. - Execute plotting: Use
pl().plot(), pl().contour(), or pl().surface() depending on the dimensionality and projection, then return the result of pl().add_to_canvas(canvas, plots). - Attach to a class: To make the function available on a GPy object (e.g., a Kernel), inject it into the class in
GPy.plotting.__init__.
Example of injecting a function into the Kern class:
from ..kern import Kern
Kern.plot_covariance = gpy_plot.kernel_plots.plot_covariance
from . import plotting_library as pl
def plot_covariance(kernel, x=None, label=None,
plot_limits=None, visible_dims=None, resolution=None,
projection=None, levels=20, **kwargs):
# ... implementation ...
canvas, kwargs = pl().new_canvas(projection=projection, xlabel=xlabel, ylabel=ylabel, zlabel=zlabel, **kwargs)
# ...
return pl().add_to_canvas(canvas, plots)