1

I'm trying to use org-mode to knit a document with some text and plots produced with python. However, when I export the org file to html or pdf, it doesn't output the plots, only the text and code.

I even used the example code in https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html (for both pre 9.7 and post 9.7), but it produces only the code and not the plots:

* #+title:TEST
#+author: Author

#+begin_src python :session :results file link
import matplotlib
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(3,2))
plt.plot([1,3,2])
fig.tight_layout()

fname = 'images/myfig.pdf'
plt.savefig(fname)
fname # return this to org-mode
#+end_src

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

#+begin_src python :results file link
import matplotlib, numpy
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(4,2))
x=numpy.linspace(-15,15)
plt.plot(numpy.sin(x)/x)
fig.tight_layout()
plt.savefig('images/python-matplot-fig.png')
return 'images/python-matplot-fig.png' # return filename to org-mode
#+end_src

#+RESULTS:
[[file:images/python-matplot-fig.png]]

#+begin_src python :results graphics file output :file boxplot.svg
  import matplotlib.pyplot as plt
  import seaborn as sns
  plt.figure(figsize=(5, 5))
  tips = sns.load_dataset("tips")
  sns.boxplot(x="day", y="tip", data=tips)
#+end_src

#+RESULTS:
[[file:boxplot.svg]]

#+begin_src python :results graphics file value :file boxplot2.svg
  import matplotlib.pyplot as plt
  import seaborn as sns
  plt.figure(figsize=(5, 5))
  tips = sns.load_dataset("tips")
  sns.boxplot(x="day", y="tip", data=tips)
  return plt.gcf()
#+end_src

#+RESULTS:
[[file:boxplot2.svg]]

All the files are created in the images directory, and are OK. However, when exported to html, the output is this:

enter image description here

I have This in my .emacs:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((python . t)
   (R . t)
   (shell . t)))

I'm working with emacs 29.4 and M-x org-version 9.6.15, Python is 3.10.12, elpy 20240109.1445 and jedi 20191011.1750.

What am I missing?

Edit:

I checked Saving Python matplotlib figures with source-code blocks but couldn't solve the problem.

1 Answer 1

2

Try adding :exports results to your code blocks. E.g.

#+begin_src python :session :results file link :exports results
import matplotlib
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(3,2))
plt.plot([1,3,2])
fig.tight_layout()

fname = 'images/myfig.pdf'
plt.savefig(fname)
fname # return this to org-mode
#+end_src

The default is :exports code which is why the code is exported, but not the results. If you want to export both, say :exports both.

See Exporting Code Blocks in the manual.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.