open_atmos_jupyter_utils.show_plot
1# pylint: disable=missing-module-docstring 2 3from matplotlib import pyplot 4from ipywidgets import HBox 5from IPython.display import display 6from IPython.core.interactiveshell import InteractiveShell 7from IPython.core.pylabtools import select_figure_formats 8from open_atmos_jupyter_utils.temporary_file import TemporaryFile 9 10 11def show_plot(filename=None, fig=pyplot, inline_format='svg'): 12 """ the missing click-to-save-as-pdf-or-svg button for matplotlib/Jupyter 13 (use instead of *.show()) """ 14 link = save_and_make_link(fig, filename) 15 select_figure_formats(InteractiveShell.instance(), {inline_format}) 16 pyplot.show() 17 display(link) 18 19 20def save_and_make_link(fig, filename=None): 21 """ saves a figure as pdf and returns a Jupyter display()-able click-to-download widget """ 22 temporary_files = [ 23 TemporaryFile(suffix=suffix, filename=( 24 filename if filename is None else 25 ((filename if not filename.endswith('.pdf') else filename[:-4]) + suffix) 26 )) 27 for suffix in ('.pdf', '.svg') 28 ] 29 for temporary_file in temporary_files: 30 fig.savefig(temporary_file.absolute_path, bbox_inches='tight') 31 return HBox([temporary_file.make_link_widget() for temporary_file in temporary_files])
def
show_plot( filename=None, fig=<module 'matplotlib.pyplot' from '/opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/matplotlib/pyplot.py'>, inline_format='svg'):
12def show_plot(filename=None, fig=pyplot, inline_format='svg'): 13 """ the missing click-to-save-as-pdf-or-svg button for matplotlib/Jupyter 14 (use instead of *.show()) """ 15 link = save_and_make_link(fig, filename) 16 select_figure_formats(InteractiveShell.instance(), {inline_format}) 17 pyplot.show() 18 display(link)
the missing click-to-save-as-pdf-or-svg button for matplotlib/Jupyter (use instead of *.show())
def
save_and_make_link(fig, filename=None):
21def save_and_make_link(fig, filename=None): 22 """ saves a figure as pdf and returns a Jupyter display()-able click-to-download widget """ 23 temporary_files = [ 24 TemporaryFile(suffix=suffix, filename=( 25 filename if filename is None else 26 ((filename if not filename.endswith('.pdf') else filename[:-4]) + suffix) 27 )) 28 for suffix in ('.pdf', '.svg') 29 ] 30 for temporary_file in temporary_files: 31 fig.savefig(temporary_file.absolute_path, bbox_inches='tight') 32 return HBox([temporary_file.make_link_widget() for temporary_file in temporary_files])
saves a figure as pdf and returns a Jupyter display()-able click-to-download widget