1

on OSX 10.13.4 with emacs 25.3 and org-mode 9.1.12. Python is 3.5.2 with matplotlib 2.2.2

I am trying to find a way to have org-mode babel for python to slow down in some of the points of my code otherwise some matplotlib plot do not always get inserted in the final produced pdf. I have tried with time.sleep(nb_seconds) in my python code but it does not solve the problem.

Minimal example:

* import necessary modules and make plot with data

#+BEGIN_SRC python :session jk :exports results 
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

# make the data
x = np.linspace(-2*np.pi, 2*np.pi, 50)
y = np.sin(x)
'x is \n{0}\n\n y is \n{1}'.format(x[:5], y[:5])

# make the plots
plt.figure(figsize=(3,3))
plt.plot(x,y)
plt.savefig("myfig.pdf")
#+END_SRC

* Finally, show the plot

#+BEGIN_SRC python :session jk :exports results :results file
"myfig.pdf"
#+END_SRC

Is there anyway I can set this up?

  • Rather than looking for a lo-tech solution of waiting a certain period of time for a process to finish, you really should consider looking for a more sophisticated approach that waits for a process to complete successfully before proceeding to the next part of the function ... This is normally done with a set-process-sentinel ... Alternatively, you may be interested in run-with-idle-timer rather than a sleep per se. – lawlist May 5 '18 at 4:24
  • This question is unclear to me. Can you please add an example? – phils May 5 '18 at 4:53
  • @phils I think this is a continuation of emacs.stackexchange.com/questions/41336/… There is an example there. Pityingly that example does not show the problem for me. But that may depend on the computer where it is executed. – Tobias May 5 '18 at 23:09
  • 1
    @Melioratus - Good question: If i have the code in one single block then, I don't have the plot at all even when doing C-c C-c in the code block, so I resolved by doing 2 separate blocks... On plt.draw(), it does not do anything more. It looks like the issue may be that by the time the plot saves the pdf is already created. The reason I say this is that I don't even see the \includegraphics[width=.9\linewidth]{myfig.pdf} in the .tex file. So I am guessing it is due to the fact that the pdflatex doesnot find the file and gives up waiting or looking for it. – Cy Bu May 8 '18 at 1:35
  • 1
    A walk around is by running twice the C-c C-e will do the trick because the files will be already there from the first run, ready to be picked up on the second one... Won't be too efficient if the code is long to run, but that's all i have for now – Cy Bu May 8 '18 at 14:35

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.