From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: Output result of source block to a file Date: Fri, 07 Jun 2019 11:01:58 -0400 Message-ID: References: <877ea041ee.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:42738) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hZGNv-0002VU-VV for emacs-orgmode@gnu.org; Fri, 07 Jun 2019 11:02:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hZGNm-0007aT-Ei for emacs-orgmode@gnu.org; Fri, 07 Jun 2019 11:02:22 -0400 Received: from mail-qt1-x82e.google.com ([2607:f8b0:4864:20::82e]:35588) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hZGNh-0007M0-FS for emacs-orgmode@gnu.org; Fri, 07 Jun 2019 11:02:15 -0400 Received: by mail-qt1-x82e.google.com with SMTP id d23so2623474qto.2 for ; Fri, 07 Jun 2019 08:02:03 -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 you probably figured out the "import io" and "f = io..." line are not necessary here. I couldn't figure out a reasonable way to use :results graphics link that didn't result in repeating the filename more than desired. These also both work, but seem to both require repeating the filename twice. #+BEGIN_SRC python :results graphics link :var fname="test.png" :file "test.png" import matplotlib.pyplot as plt plt.plot([1, 2, 3, 1]) plt.savefig(fname) #+END_SRC #+BEGIN_SRC python :results graphics link :file "test.png" import matplotlib.pyplot as plt plt.plot([1, 2, 3]) plt.savefig("test.png") #+END_SRC Something like this should work, but there seem to be some extra bytes getting put in the png file from the decoding, and latin-1 is the only one I can get to work. If anyone knows how to get this to work, I am interested in seeing it! #+BEGIN_SRC python :results value :file "io.png" import matplotlib.pyplot as plt import io buf = io.BytesIO() plt.plot([1, 2, 3]) plt.savefig(buf, format='png') s = buf.getvalue() return s.decode('latin-1') #+END_SRC In general though, all of these are much more work than using ob-ipython, which just puts images in the buffer for you. Roger Mason writes: > Hello John, > > John Kitchin writes: > >> 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. > > Thank you. That works. > > Best wishes, > Roger -- 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