Search code examples
pythonpython-2.7matplotlibx11x11-forwarding

Python 2.7.12 Matplotlib x11 forwarding not showing or throwing multiple errors


I am logging in to a remote linux machine from windows 7 via putty. In the settings I enabled the X11 forwarding option, and added the -X flag when loging in to the ssh server. On this server I run the following python code:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pyfits
a = raw_input("path: ") #filepath on the server, conected with filename
file = pyfits.open (a +'/file.fits', memap = 'True')
data = file[0].data
print data.shape #shape gets printed correctly

plt.figure(1)
plt.imshow(data[0,:,:], cmap = 'gray')
print 3
plt.show()
print 4

I get all the print values, with the output looking like this:

(300, 512, 512)
3
4

there is no error raised nor a x11 window opend. The comadoline goes back as if the program was at the end. Is there any possibility to get the plt.show() comand to actually show on the remote controlling windows machine?


Solution

  • I got it figured out:

    at first it is like "tcaswell" said, you can´t use the 'Agg' backend with interactive windows. This error gets fixed by just deleting the first two lines of code. The second problem is, that by the plt.figure(1) comand a new figure 1 is created but in the plt.show() comand there is no figure specified that should show up. So this error can be solved by either deleting the line that says plt.figure(1) or by putting the number of the figure to plot in the brackets behind the plt.show() comand: plt.show(1). By this way it is possible to create multiple figures in one file and being able to switch between them.