From a5ffabd04e60d869ec8c1595b3d8ff2b954c8df3 Mon Sep 17 00:00:00 2001 From: Roger Wolf <roger.wolf@kit.edu> Date: Mon, 9 Oct 2023 15:38:32 +0200 Subject: [PATCH] add a small tool to allow for adding figures directly into Jupyter-notebook --- tools/add_figure.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 tools/add_figure.py diff --git a/tools/add_figure.py b/tools/add_figure.py new file mode 100755 index 0000000..5dc2b2b --- /dev/null +++ b/tools/add_figure.py @@ -0,0 +1,41 @@ +#!/usr/bin/python3 +from os import getcwd +import matplotlib.pyplot as plt +import matplotlib.image as mpimg + +def add_figure(RELPATH, WIDTH=10.0, HEIGHT=10.0): + """ + Display figure located at RELPATH through matplotlib rendering machine. + + WIDTH and HEIGHT can be given as parameters of type float to determine the + measures of the figure. Nominally both are given in inches. Axes will be + suppressed. + """ + PREFIX=getcwd()+"/" + print(PREFIX+RELPATH) + print(RELPATH[:RELPATH.rfind(".")].replace("/", "_")) + img = plt.figure(RELPATH[:RELPATH.rfind(".")].replace("/", "_"), figsize=(WIDTH, HEIGHT)) + img = mpimg.imread(PREFIX+RELPATH) + # Render image in background + imgplot = plt.imshow(img) + # Hide axes + imgplot.axes.get_xaxis().set_visible(False) + imgplot.axes.get_yaxis().set_visible(False) + # Show image + plt.show() + +import argparse + +parser = argparse.ArgumentParser( + prog="PROG", + description="Add figure to code cell in Jupyter-notebook." + ) +parser.add_argument('filename', type=str, nargs=1, + help='Filename with relative path statement.') +parser.add_argument('--witdh', type=float, dest='WIDTH', nargs=1, default=10.0, + help='Width of file (default 10.0 inches).') +parser.add_argument('--height', type=float, dest='HEIGHT', nargs=1, default=10.0, + help='Height of file (default 10.0 inches).') +args = parser.parse_args() +add_figure(args.filename[0], args.WIDTH, args.HEIGHT) + -- GitLab