From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: Output result of source block to a file Date: Thu, 6 Jun 2019 17:12:40 -0400 Message-ID: References: <877ea041ee.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000004ac8f6058aae2d7b" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:51595) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hYzuB-0005bg-D8 for emacs-orgmode@gnu.org; Thu, 06 Jun 2019 17:26:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hYzgt-0006dZ-7Z for emacs-orgmode@gnu.org; Thu, 06 Jun 2019 17:12:56 -0400 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]:36502) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hYzgs-0006bk-MA for emacs-orgmode@gnu.org; Thu, 06 Jun 2019 17:12:55 -0400 Received: by mail-wm1-x336.google.com with SMTP id u8so1332067wmm.1 for ; Thu, 06 Jun 2019 14:12:54 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Roger Mason Cc: org-mode-email --0000000000004ac8f6058aae2d7b Content-Type: text/plain; charset="UTF-8" I think you can use something like this: #+BEGIN_SRC python :results output file :var fname="test.png" import matplotlib.pyplot as plt import io f = io.StringIO() plt.plot([1, 2, 3, 17]) plt.savefig(fname) print(fname, end='') #+END_SRC It is in Python3. John ----------------------------------- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu On Thu, Jun 6, 2019 at 2:27 PM Roger Mason wrote: > Hello again, > > stardiviner writes: > > > Roger Mason writes: > > > >> Hello, > >> > >> I want to output the result of the evaluation of a (python) source block > >> to a (graphics) file and have a link to the file inserted in the buffer. > > > > For this, you should try updated header arguments like ":results > graphics link" > > or ":results file link". > > I updated to Org mode version 9.2.3 (release_9.2.3-390-gfb5091 @ > /home/rmason/.emacs.d/org-git/lisp/) > > This works but sets the results to verbatim, hence the exported & > compiled code does not include the graphic. > > #+name: scaleplot03 > #+begin_src python :results output graphics link :exports results :var > data=test :var file="scaleplot03.pdf" > import matplotlib.pyplot as plt > import csv > > scale = [] > lescale = [] > cdscale = [] > > energy = [] > leenergy = [] > cdenergy = [] > > for row in data: > scale.append(float(row[0])) > energy.append(float(row[1])) > if int(row[2]) != 0: > lescale.append(float(row[0])) > leenergy.append(float(row[1])) > if int(row[3]) != 0: > cdscale.append(float(row[0])) > cdenergy.append(float(row[1])) > > plt.plot(scale,energy, 'r+') > plt.plot(lescale,leenergy, 'go') > plt.plot(cdscale,cdenergy, 'bo') > plt.xlabel('scale') > plt.ylabel('energy (Ha)') > plt.title('Energy vs scale') > plt.legend() > plt.savefig(file) > print ("./%s" % file) > #+end_src > > #+RESULTS: scaleplot03 > : ./scaleplot03.pdf > > This header does not work either: > #+name: scaleplot04 > #+begin_src python :results file link :exports results :var data=test :var > file="scaleplot04.pdf" > ( same pythone code here) > > #+RESULTS: scaleplot04 > [[file:None]] > ]] > #+end_src > > Thank you for your help. > > Roger > > > > --0000000000004ac8f6058aae2d7b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I think you can use something like this:

#+BEGIN_SRC python :results output file :var fname=3D"test.png"=
import matplotlib.pyplot as plt
import io

f =3D io.StringIO()=
plt.plot([1, 2, 3, 17])
plt.savefig(fname)
print(fname, end=3D= 9;')
#+END_SRC

It is in Python3.
=
John

-= ----------------------------------
Professor John Kitchin=C2=A0
Doher= ty Hall A207F
Department of Chemical Engineering
Carnegie Mellon Univ= ersity
Pittsburgh, PA 15213
412-268-7803
<= br>

On Thu, Jun 6, 2019 at 2:27 PM Roger Mason <rmason@mun.ca> wrote:
Hello again,

stardiviner <nu= mbchild@gmail.com> writes:

> Roger Mason <rma= son@mun.ca> writes:
>
>> Hello,
>>
>> I want to output the result of the evaluation of a (python) source= block
>> to a (graphics) file and have a link to the file inserted in the b= uffer.
>
> For this, you should try updated header arguments like ":results = graphics link"
> or ":results file link".

I updated to Org mode version 9.2.3 (release_9.2.3-390-gfb5091 @ /home/rmas= on/.emacs.d/org-git/lisp/)

This works but sets the results to verbatim, hence the exported &
compiled code does not include the graphic.

#+name: scaleplot03
#+begin_src python :results output graphics link :exports results :var data= =3Dtest :var file=3D"scaleplot03.pdf"
=C2=A0 import matplotlib.pyplot as plt
=C2=A0 import csv

=C2=A0 scale =3D []
=C2=A0 lescale =3D []
=C2=A0 cdscale =3D []

=C2=A0 energy =3D []
=C2=A0 leenergy =3D []
=C2=A0 cdenergy =3D []

=C2=A0 for row in data:
=C2=A0 =C2=A0 =C2=A0 scale.append(float(row[0]))
=C2=A0 =C2=A0 =C2=A0 energy.append(float(row[1]))
=C2=A0 =C2=A0 =C2=A0 if int(row[2]) !=3D 0:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0lescale.append(float(row[0]= ))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0leenergy.append(float(row[1= ]))
=C2=A0 =C2=A0 =C2=A0 if int(row[3]) !=3D 0:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0cdscale.append(float(row[0]= ))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0cdenergy.append(float(row[1= ]))=C2=A0 =C2=A0 =C2=A0 =C2=A0

=C2=A0 plt.plot(scale,energy, 'r+')
=C2=A0 plt.plot(lescale,leenergy, 'go')
=C2=A0 plt.plot(cdscale,cdenergy, 'bo')
=C2=A0 plt.xlabel('scale')
=C2=A0 plt.ylabel('energy (Ha)')
=C2=A0 plt.title('Energy vs scale')
=C2=A0 plt.legend()
=C2=A0 plt.savefig(file)
=C2=A0 print ("./%s" % file)
#+end_src

#+RESULTS: scaleplot03
: ./scaleplot03.pdf

This header does not work either:
#+name: scaleplot04
#+begin_src python :results file link :exports results :var data=3Dtest :va= r file=3D"scaleplot04.pdf"
( same pythone code here)

#+RESULTS: scaleplot04
[[file:None]]
]]
#+end_src

Thank you for your help.

Roger



--0000000000004ac8f6058aae2d7b--