SheffieldML / GPy

Gaussian processes framework in python
BSD 3-Clause "New" or "Revised" License
2.04k stars 562 forks source link

No Color Bar #598

Closed yywe closed 6 years ago

yywe commented 6 years ago

When I fit a Gaussian Process Regression model by calling 'GPy.models.GPRegression'. then I want to plot the model so I call 'm.plot(property='3d'). I know the highlight area usually have a larger value, but how can I show the related color bar for the colours. any help? thanks so much.

mzwiessele commented 6 years ago

When you plot you get back the full dictionary of all matplotlib plots. Use the plot for the image and add a colorbar to the figure yourself.

fig.colorbar(cm, ax=ax)

Where cm is the image you want the colorbar for, ax is the axis you want the colorbar to be added and fig is the figure which contains cm.

yywe commented 6 years ago

The returned plot is fig = m.plot(). The problem is from the fig object, I did not find the image, and also what do I pass for the second parameter ax?

yywe commented 6 years ago

fig.figure.images is empty

lionfish0 commented 6 years ago

Solved: This wasn't obvious : )

Using help from this stackoverflow answer:

Immediately after you call m.plot(), call:

ax = plt.gca() #get current axes
mappable = ax.collections[0] #this is specific for what the surface call returns
plt.colorbar(mappable)

More notes/comments on GPy's code: Relevant code is line 214 in GPy/plotting/gpy_plot/gp_plots.py - an issue is this returns a dictionary, with gpmean set to a list of plots, which is what we really need for setting the colorbar properly. However, this method's plots list is fed into the add_to_canvas method by plot (line 351). The add_to_canvas method doesn't use the plots argument, so its content is, I guess, not accessible later. Hence why you're having trouble I guess finding it.

lionfish0 commented 6 years ago

Addendum: This doesn't work if you've set the plot_data parameter to False.