72

Plots are normally shown when I run files from the ipython shell or from an ipython notebook, but they don't show up when I run the file from a bash terminal -- everything else works fine when is run from a bash terminal.

Sample python script (trial.py):

import matplotlib.pyplot as plt

print 'please, show my graph'

plt.plot([1,2,3], [1,2,3])

plt.show()

This is what I get (plot doesn't show up):

[~/Desktop]$ python trial.py
please, show my graph
[~/Desktop]$

If I do

import matplotlib
matplotlib.use('TkAgg')

before importing pyplot, then a window opens and closes immediately when I run it from the terminal.

I've tried different ways of importing modules without success:

import matplotlib.pyplot as plt
import matplotlib.pylab as plt
from matplotlib import pyplot as plt
from matplotlib import pylab as plt

I have the plt.show() function in my file.

Do you know how I can fix it?

Some info about versions and installation:

I'm on a mac OSX 10.11.3.

In [61]: print matplotlib.__file__
/usr/local/lib/python2.7/site-packages/matplotlib/__init__.pyc

In [62]: print matplotlib.__version__
1.4.2

In [64]: print sys.version
2.7.9 (default, Apr  7 2015, 07:58:25)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]

In [65]: matplotlib.get_backend()
Out[65]: u'MacOSX'
drake
  • 1,012
  • 2
  • 9
  • 17
  • 1
    Can you provide a sample complete script that will not output anything? Even as simple as `import matplotlib.pyplot as plt; plt.plot([1,2,3], [1,2,3]); plt.show()`. Also, what matplotlib backend are you using? – wflynny Mar 28 '16 at 19:50
  • Thanks, @wflynny. matplotlib.get_backend() outputs u'MacOSX' . That code doesn't display anything at all. – drake Mar 28 '16 at 19:58
  • 1
    Try `plt.show(block=True)` just to explicitly block code after display. Your edit seems to suggest that window is not blocking the code, so this might help. I'll edit my answer if it works. Also, just to confirm `plt.show()` is the last line in your code, right? – Munir Mar 29 '16 at 01:58
  • 1
    That did work, @Munir! Thanks so much. That was indeed the last line. I'll accept your answer and will ask how to set the keyword "block" as True by default as a separate question – drake Mar 29 '16 at 02:15

1 Answers1

111

You need to add matplotlib.pyplot.show() in your code to show plots in non-interactive mode. See docs at http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.show

EDIT:

After further info from OP, blocking had to be enabled explicitly using plt.show(block=True).

Munir
  • 3,442
  • 3
  • 19
  • 29
  • Thanks. I have that. I have edited my question to include some additional information. – drake Mar 28 '16 at 19:45
  • Here is a related question http://stackoverflow.com/questions/36274351/how-does-one-set-keyword-block-in-plt-show-equal-to-true-by-default . I want to set that keyword to True by default, so that I can run scripts without stating "block=True" . – drake Mar 29 '16 at 02:30