question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving a GPy plot as an image file

See original GitHub issue

How can I save a GPy plot/visualization to an image file? I am on a Linux system with no attached display, so this is my only option for viewing visualizations.

This is my current attempt:

import GPy
import numpy as np

# sample inputs and outputs
X = np.random.uniform(-3.,3.,(50,2))
Y = np.sin(X[:,0:1]) * np.sin(X[:,1:2])+np.random.randn(50,1)*0.05

# define kernel
ker = GPy.kern.Matern52(2,ARD=True) + GPy.kern.White(2)

# create simple GP model
m = GPy.models.GPRegression(X,Y,ker)

# optimize and plot
m.optimize(messages=True,max_f_eval = 1000)

GPy.plotting.change_plotting_library("matplotlib")
fig = m.plot()
fig.savefig("model.png")

I get an error on the fig = m.plot() line:

Unable to access the X Display, is $DISPLAY set properly?

It seems like m.plot() is trying to show the figure immediately. How can I avoid this and only write to an image file?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
timothy-shieldscommented, Apr 7, 2016

I have resolved the issue. Here is the code in case anyone else encounters the same problem:

import matplotlib
import GPy
import numpy as np

# Configure GPy -> matplotlib -> Agg
matplotlib.use("Agg")
GPy.plotting.change_plotting_library("matplotlib")

X = np.random.uniform(-3., 3., (50, 2))
Y = np.sin(X[:, 0:1]) * np.sin(X[:, 1:2]) + np.random.randn(50, 1) * 0.05
kernel = GPy.kern.Matern52(2, ARD=True) + GPy.kern.White(2)
model = GPy.models.GPRegression(X, Y, kernel)
model.optimize(messages=True, max_f_eval = 1000)

ax = model.plot()
ax.figure.savefig("model.pdf")

where the key lines are

# Configure GPy -> matplotlib -> Agg
matplotlib.use("Agg")
GPy.plotting.change_plotting_library("matplotlib")
1reaction
sguglercommented, Jan 19, 2021

I found a workaround: If you print out the dictionary from ax = m.plot(plot_limits=[-15, 15], samples=5), so print(ax), we see that it contains a dictionary,

{'dataplot': [<matplotlib.collections.PathCollection object at 0x7f3d9f6f2f60>], 'gpmean': [[<matplotlib.lines.Line2D object at 0x7f3d9f6f2eb8>]], 'gpconfidence': [<matplotlib.collections.PolyCollection object at 0x7f3d9f6f27b8>]}

the first entry of which is the matplotlib plot we want. So the following worked for me:

dataplot = ax['dataplot'][0] 
dataplot.figure.savefig("gp-test.pdf")
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Save Plots To Image Files Using Matplotlib
pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.
Read more >
How to Save a Plot to a File Using Matplotlib | Tutorial by Chartio
Learn how to save a plot to a file using Matplotlib, a plotting library for Python. In this tutorial, we'll show you to...
Read more >
Saving Plots in R | Department of Statistics
A General Method​​ Choose the format that you want to use. In this example, I'll save a plot as a JPG file, so...
Read more >
Saving Plots in R as .JPEG / .PNG files (Data ... - YouTube
Today we will be looking at how to save your ggplot2 and other plot results as .PNG and .JPEG files in R.For similar...
Read more >
Specifying and saving a figure with exact size in pixels
where the filename has the "png" extension in this example (but you must still specify the format with format='png' anyway as far as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found