2

I have calculated two GP regression models and would like to have them plotted in the same figure.

Model 1

kernel = GPy.kern.RBF(input_dim=1, variance=.1, lengthscale=1.)
m1 = GPy.models.GPRegression(xa, ya,kernel)
m1.optimize_restarts(num_restarts = 10)
m1.optimize(messages=True)

from IPython.display import display
display(m1)

fig1 = m1.plot(plot_density=True) 
m1.plot(plot_density=True)
GPy.plotting.show(fig2, filename='2')

Model 2

m2 = GPy.models.GPRegression(xe, ye,kernel)
m2.optimize_restarts(num_restarts = 10)
m2.optimize(messages=True)

from IPython.display import display
display(m2)

fig2 = m2.plot(plot_density=True,)

GPy.plotting.show(fig2, filename='2')

I want both plots in one figure, in either matplotlib or plotly i.e. GPy.plotting.show(fig, filename='filename').

Thanks

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
hH1sG0n3
  • 121
  • 4
  • This was surprisingly less-trivial than I originally thought it'd be. Documentation across GPy seems to be under continuous construction, and it took awhile to ascertain that GPy models are integrated with the matplotlib interface. – Dave Liu May 28 '19 at 22:27

1 Answers1

1

Using matplotlib, you can define a subplot, and specify the subplot to be used using the same axes (specifically, param ax).

import matplotlib.plt as plt
fig, ax = plt.subplots()
m1.plot(plot_density=True, ax=ax)
m2.plot(plot_density=True, ax=ax)

I tested this out with a test data set:

# Random Test Data
import pods
data = pods.datasets.olympic_marathon_men()

# First X,Y Regression Model
kernel = GPy.kern.RBF(input_dim=1, variance=.1, lengthscale=1.)
m1 = GPy.models.GPRegression(data['X'], data['Y'], kernel)
m1.optimize_restarts(num_restarts = 10)
m1.optimize(messages=True)

# Second model; changed the X, Y slightly.
m2 = GPy.models.GPRegression(data['X'] + 5, data['Y'] + 3,kernel)
m2.optimize_restarts(num_restarts = 10)
m2.optimize(messages=True)

Related: What is the best way of combining two independent plots with matplotlib?

Plot of two densities w/data

Dave Liu
  • 906
  • 1
  • 11
  • 31