4

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?

13
  • If you run the first block and examine the :session buffer (probably named *Python*?), do you see an error?
    – mankoff
    Commented May 1, 2015 at 23:28
  • @mankoff: No, I don't see any error. See the addition in the update. Commented May 2, 2015 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
    Commented May 2, 2015 at 8:35
  • @wvxvw: did not help... :( Seems like the temp file holds [<matplotlib.lines.Line2D object at 0x10c0cff28>] Commented May 2, 2015 at 10:46
  • 1
    @DrorAtariah - Were you able to resolve this issue?
    – Melioratus
    Commented Nov 26, 2016 at 18:14

1 Answer 1

0

I had the same issue, so I landed here to find about your case. After inspecting my session I found that it does not like a return outside a function. So this worked for me.

#+begin_src python  :results file  :exports both  :python ~/.venvs/pedpy/bin/python :session namesession
import matplotlib.pyplot as plt
import numpy as np

def plot_function(x, y, figname):
    fig, ax = plt.subplots()
    ax.plot(x,y)
    plt.grid()
    fig.savefig(figname)
    return figname

x = np.arange(1,10)
y=np.sin(x)

plot_function(x,y,    figname="ff.png"  )

#+end_src

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.