2

I have a simple script to test a plot in matplotlib but no window showing the figure appears. On reading other questions on stackoverflow, I've done the following to resolve this:

  • installed PySide using these instructions.
  • edited matplotlibrc file with these two lines:

     backend      : Qt4Agg
     #backend.qt4 : PySide        # PyQt4 | PySide
    

    so that the command python -c 'import matplotlib; import matplotlib.pyplot; print(matplotlib.backends.backend)' now yields Qt4Agg whereas before it gave agg

  • included the pylab.show() command. So the set of commands that I now tried in the python interpreter after installing Pyside, and editing the matplotlibrc file look like this:

      import pylab
      pylab.ion()
      import matplotlib.pyplot as plt
      import numpy as np
      x = np.arange(0,5,0.1)
      y = np.sin(x)
      plt.plot(x,y)
     [<matplotlib.lines.Line2D object at 0x7fcef627cdd0>]
     pylab.show()
    

    However, the plot still doesn't show. Could anyone please help me with this? I am using Ubuntu 14.04 in VirtualBox with python2.7.

QPTR
  • 1,620
  • 7
  • 26
  • 47

1 Answers1

2

When I use your code the plot actually flashes on the screen, but closes immediately. Placing an input() function at the end might help you with debugging it:

import pylab
import matplotlib.pyplot as plt
import numpy as np


pylab.ion()
x = np.arange(0,5,0.1)
y = np.sin(x)
plt.plot(x,y)
pylab.show()
tin = input("Test Input: ")

And removing the pylab.ion() actually keep the plot on the screen. This gives you another hint. There are already some good answers why this is happening. E.g.:

Community
  • 1
  • 1
tobias47n9e
  • 2,233
  • 3
  • 28
  • 54
  • Thanks for the answer. I don't know what just happened, but I tried the commands in the order you posted above, that is imported all the libraries first, and as soon as I typed plt.plot(x,y) the plot popped up. And its staying on the screen. Hmm. So, its working now. – QPTR Aug 22 '15 at 16:44
  • Actually, it works outside the virtualenv, but inside virtualenv its still causing problems. Will investigate further. – QPTR Aug 22 '15 at 17:03
  • 1
    I guess if you need a pause after each plot. The information in the link I posted says that a `plt.pause(1)` is needed between plots. At least a millisecond apparently. – tobias47n9e Aug 22 '15 at 17:18
  • Yes, I just tried that. It works! :) Thanks so much! – QPTR Aug 22 '15 at 17:52
  • 1
    Great. Glad i could help. – tobias47n9e Aug 22 '15 at 18:31