Skip to content

Instantly share code, notes, and snippets.

@anosatsuk124
Last active November 29, 2021 16:44
Show Gist options
  • Save anosatsuk124/4e7acba63f322586afe18e9b2063d5f7 to your computer and use it in GitHub Desktop.
Save anosatsuk124/4e7acba63f322586afe18e9b2063d5f7 to your computer and use it in GitHub Desktop.
; Copyright (C) 2021 Viterum(Satsuki Akiba). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
(import os base64 [IPython.display [HTML]])
(defn show [&optional fig [file_ex "svg"]]
(setv filename_n 0)
(if (= (os.path.isdir "temp") False)
(os.mkdir "temp"))
(while (os.path.isfile (+ (+ "temp/" (str filename_n)) (.format ".{file_ex}" :file_ex file_ex)))
(setv filename_n (+ filename_n 1))
)
(setv filename (+ (+ "temp/" (str filename_n)) (.format ".{file_ex}" :file_ex file_ex)))
(fig.savefig filename)
(with [file (open filename "rb")]
(setv data (.b64encode base64 (.read file)))
)
(setv tag (.format (+(+ "<img src='data:image/{file_ex};base64," (.decode data "utf-8")) "'>") :file_ex file_ex))
(if (= file_ex "svg")
(setv tag (+(+ "<img src='data:image/svg+xml;base64," (.decode data "utf-8")) "'>"))
)
(.remove os (+ "temp/" (+ (str filename_n) (.format ".{file_ex}" :file_ex file_ex))))
(.rmdir os "temp")
(return (HTML tag))
)
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

matplotlib_inline.hy

It makes you able to preview and make matplotlib inline in Calysto_hy in your Jupyter and any viewers.

How to use (e.g.)

Download in your project's directory.

curl -O https://gist.githubusercontent.com/anosatsuk124/4e7acba63f322586afe18e9b2063d5f7/raw/70c00d6a31c86942c239519df1eff9e175a58492/matplotlib_inline.hy

or

wget https://gist.githubusercontent.com/anosatsuk124/4e7acba63f322586afe18e9b2063d5f7/raw/70c00d6a31c86942c239519df1eff9e175a58492/matplotlib_inline.hy

Import and Use in your project.

SVG(default)

(import [matplotlib_inline [show]]
        [matplotlib.pyplot :as plt]
        [numpy :as np]
        )

(setv fig (plt.figure))

(setv ax (fig.add_subplot 111))

(setv y (+ (* 2 x) 1))

(.plot ax x y)

(show fig)

PNG

(import [matplotlib_inline [show]]
        [matplotlib.pyplot :as plt]
        [numpy :as np]
        )

(setv fig (plt.figure))

(setv ax (fig.add_subplot 111))

(setv y (+ (* 2 x) 1))

(.plot ax x y)

(show fig "png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment