3

In the following link you can find the following simple example:

#+begin_src python :session :results file
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(3,2))
plt.plot([1,3,2])
fig.tight_layout()
plt.savefig('myfig.pdf')
'myfig.pdf' # return this to org-mode
#+end_src

I have the problem that the string that contains the file name is not returned. Thus, I don't get something like:

#+RESULTS:
[[file:images/myfig.pdf]]

Here is what I can see in the session's buffer:

Process Python finished

Python 3.4.3 (default, Feb 27 2015, 00:22:26) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> >>> >>> r/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/py6499_X0''', 'exec'));
import matplotlib
>>> import matplotlib
matplotlib.use('Agg')
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(3,2))
fig=plt.figure(figsize=(3,2))
plt.plot([1,3,2])
plt.plot([1,3,2])
fig.tight_layout()
fig.tight_layout()
plt.savefig('myfig.pdf')
plt.savefig('myfig.pdf')
'myfig.pdf' # return this to org-mode
'myfig.pdf' # return this to org-mode


open('/var/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/babel-64998Up/python-6499xhD', 'w').write(str(_))
open('/var/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/babel-64998Up/python-6499xhD', 'w').write(str(_))




'org_babel_python_eoe'
'org_babel_python_eoe'
>>> [<matplotlib.lines.Line2D object at 0x10bc987f0>]
>>> >>> 'myfig.pdf'
>>> >>> 9
>>> >>> >>> 'org_babel_python_eoe'
>>> 

However, the following works as expected:

#+begin_src python :session :results file
'./myfig.pdf' # return this to org-mode
#+end_src

#+RESULTS:
[[file:./myfig.pdf]]

What is the problem here? How can I debug it? It seems like matplotlib causes some problem.

Edit:

Same org file behaves as expected on a Linux box. The problem described above happens on Mac. So, probably it has something to do with the installed python versions. Still, I don't know how to debug it...

Edit 2 (Debugging steps):

Ok, I'm trying to break the steps. Consider the complete example:

#+BEGIN_SRC python :session :results file
import matplotlib             #1
matplotlib.use('Agg')         #2
import matplotlib.pyplot      #3
fig=plt.figure(figsize=(3,2)) #4
plt.plot([1,3,2])             #5
fig.tight_layout()            #6
plt.savefig('myfig.png')      #7
'myfig.png'                   #8 return this to org-mode
#+END_SRC

If I comment lines 1-7, then the results block is correct:

#+RESULTS:
[[file:myfig.png]]

Now, I start to add the lines in their order. Each time I exit the interpreter, so it will be a "fresh" run.

With lines 1,8: The result is correct (as can be found above) With lines 1-2,8: Still correct With lines 1-3,8: Result is incorrect!

#+RESULTS:
[[file:]]

Therefore, if I understand correctly, this is not a problem of the interpreter, but something else. The version of python I use is (as indicated by import sys; sys.version invoked from the interactive interpreter that was started by babel):

'3.4.3 (default, Feb 27 2015, 00:22:26) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]'

Furthermore, matplotlib.__version__ yields: `1.4.3'

M-x version returns: GNU Emacs 24.4.51.1 (x86_64-apple-darwin14.1.0, NS apple-appkit-1344.72) of 2015-02-02 on Boxname and it runs in OS X 10.10.

Maybe this additional information can help someone solve this issue?

  • If you run the first block and examine the :session buffer (probably named *Python*?), do you see an error? – mankoff May 1 '15 at 23:28
  • @mankoff: No, I don't see any error. See the addition in the update. – Dror Atariah May 2 '15 at 7:07
  • Actually, from the log, it looks like it worked properly... _ would refer to 'myfig.pdf', which is what gets written into the temporary file: python-6499xhD. What if instead you'd do :results output file and do print 'myfig.pdf', will that change anything? – wvxvw May 2 '15 at 8:35
  • @wvxvw: did not help... :( Seems like the temp file holds [<matplotlib.lines.Line2D object at 0x10c0cff28>] – Dror Atariah May 2 '15 at 10:46
  • Images folder exists? – mankoff May 2 '15 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.