Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interactive mode doesn't display images with standard python interpreter #5561

Closed
wlnirvana opened this issue Nov 25, 2015 · 6 comments
Closed

Comments

@wlnirvana
Copy link

All the codes won't display images, and there is a warning when I try to exit from python shell, which I don't know if is relevant.

############################################################
# first try using pylab
############################################################
>>> from pylab import *
>>> matplotlib.interactive(True)
>>> matplotlib.is_interactive()
True
>>> plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x7ff821558290>]
>>>                                            # <--------------- press Ctrl-D here
/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3.py:219: Warning: Source ID 8 was not found when attempting to remove it
  GLib.source_remove(self._idle_draw_id)

############################################################
# second try using ion()
############################################################
>>> import matplotlib.pyplot as plt
>>> plt.ion()
>>> plt.plot([1,2,3])                          # No image displayed
[<matplotlib.lines.Line2D object at 0x7f940c91b790>]
>>> plt.show()                                 # still no image displayed
>>>                                            # <--------------- press Ctrl-D here
/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3.py:219: Warning: Source ID 8 was not found when attempting to remove it
  GLib.source_remove(self._idle_draw_id)

############################################################
# third try using show() with interactive mode
############################################################
>>> import matplotlib.pyplot as plt
>>> plt.ion()
>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x7efe545b2790>]
>>> plt.show()                                 # no image displayed
>>>                                            # <--------------- press Ctrl-D here
/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3.py:219: Warning: Source ID 8 was not found when attempting to remove it
  GLib.source_remove(self._idle_draw_id)

However if interactive mode is not used, everything is fine. After I close the image window and exit python shell later on, there is no warning.

>>> import matplotlib.pyplot as plt
p>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x7f534d7da7d0>]
>>> plt.show()                                 # image pops up

Below is my environment information:

$ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt17-1 (2015-09-26) x86_64 GNU/Linux
$ python
Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'1.5.0'
>>> matplotlib.get_backend()
u'GTK3Agg'
@tacaswell
Copy link
Member

Please try using the IPython shell and the %matplotlib magic.

@tacaswell
Copy link
Member

This needs to be verified, but my theory of what is going on here:

For interactive figures to work (that is you have both the prompt and can pan/zoom) the figure the gui event loop needs to be integrated into the event loop running the python prompt. This is done through the PyOs_InputHook which is called while the shell is waiting for you to type and allows the GUI mainloop to spin and process it's events (ex, draw its self, respond to the mouse/keyboard). Some gui toolkits install this hook for you (tk, qt), some do not (apparently gtk). IPython has the code to manage installing the proper hooks for all of the backends across all of the major OSs.

The reason that this used to 'work' (that is you would get a figure, but it would not be live) and now does not is that in 1.5.0 as part of getting the auto-redraw to work we converted a number of synchronous draw calls to asynchronous draw_idle calls.

To get this to work I think you can

a) just use IPython for interactive use
b) install the proper callback for gtk
c) use a different backend which auto-installs (tk or qt)

Calling plt.pause(30) should make the figure show up and be 'live' for 30s.

@wlnirvana
Copy link
Author

@tacaswell
a) iPython does work. Sorry that I should have mentioned it in previous post.
b) What callback should I install? (or any reference about how can I find the proper callback?)

plt.pause(30) works exactly as you described. The image pops up and I can zoom in or move the image interactively for 30s. After that the image is kind of dead and I can't even close the image window unless I exit python.

@tacaswell
Copy link
Member

regarding the hook to install, I am not a gtk expert (as I am not sure I even have it installed on my computer), but have a look at inputhook.py and inputhookgtk*.py in https://github.com/ipython/ipython/tree/master/IPython/lib

@wlnirvana
Copy link
Author

@tacaswell I see. Thank you very much. By the way, I did get a warning thrown by matplotlib after calling plt.pause, which leaves me in doubt if I should close this ticket now.

>>> plt.pause(30)
/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py:2435: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
  warnings.warn(str, mplDeprecation)

@tacaswell
Copy link
Member

That warning is known and old, don't worry about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants