.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/saving_and_loading_interactive_plots.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_saving_and_loading_interactive_plots.py: Saving and Loading Interactive Plots ==================================== This example shows how to save and reopen interactive plots created with ``tsunami_ip_utils``. This is useful for sharing plots with collaborators or saving plots for later use. .. GENERATED FROM PYTHON SOURCE LINES 9-25 Non-Custom Interactive Plots ---------------------------- The interactive plots generated in :ref:`sphx_glr_auto_examples_visualizing_contributions.py` can all be saved using the ``write_html`` method of the plotly figure object. This method writes the plot to an HTML file, which can be opened in a web browser to view the interactive plot. Even for :class:`tsunami_ip_utils.viz.pie_plot.InteractivePieLegend` plots (which aren't plotly figures, but rather a custom wrapper around a plotly figure), the same ``write_html`` method can be used. Saving Plots ------------ However, for plots that implement interactive python callbacks, such as :class:`tsunami_ip_utils.viz.scatter_plot.InteractiveScatterLegend` plots, the interactivity cannot fully be saved to html because the callbacks are implemented in python (though the current static html state of the plot can be written to html with the :meth:`tsunami_ip_utils.viz.scatter_plot.InteractiveScatterLegend.write_html` method). So, to save these plots, a different approach is taken. These plots are serialized as python pickle objects, which can be reloaded later by anyone with ``tsunami_ip_utils`` installed. To serialize an interactive scatter plot, the :meth:`tsunami_ip_utils.viz.scatter_plot.InteractiveScatterLegend.save_state` method is used. This method just takes a path to the output pickle file to save the plot state to. .. GENERATED FROM PYTHON SOURCE LINES 27-29 First we need to get some data to create an interactive scatter plot. We'll use the same code as in :ref:`sphx_glr_auto_examples_correlation_plot_contribution_correlation_plot.py` .. GENERATED FROM PYTHON SOURCE LINES 29-56 .. code-block:: Python from tsunami_ip_utils.viz.viz import correlation_plot from tsunami_ip_utils.integral_indices import get_uncertainty_contributions from paths import EXAMPLES import matplotlib.pyplot as plt application_files = [ EXAMPLES / 'data' / 'example_sdfs' / 'MCT' / f'MIX-COMP-THERM-001-001.sdf' ] experiment_files = [ EXAMPLES / 'data' / 'example_sdfs' / 'MCT' / f'MIX-COMP-THERM-004-009.sdf' ] contributions_nuclide, contributions_nuclide_reaction = get_uncertainty_contributions( application_files, experiment_files, ) # Now get the application and experiment contributions application_contributions = contributions_nuclide_reaction['application'][0] experiment_contributions = contributions_nuclide_reaction['experiment'][0] fig = correlation_plot( application_contributions, experiment_contributions, integral_index_name='%(Δk/k)^2', # Note that Plotly plots don't support pylatex in labels, so we just use a plain string plot_type='interactive_scatter', interactive_legend=True, ) fig.save_state( EXAMPLES / 'scatter_plot.pkl' ) .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 57-61 Loading Plots ------------- Now, that we've serialized the plot to a ``.pkl`` file, we can reload it using the :func:`tsunami_ip_utils.viz.scatter_plot.load_interactive_scatter_plot` function. .. GENERATED FROM PYTHON SOURCE LINES 61-67 .. code-block:: Python from tsunami_ip_utils.viz.scatter_plot import load_interactive_scatter_plot fig = load_interactive_scatter_plot( EXAMPLES / 'scatter_plot.pkl' ) fig.show() .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 68-70 and as the above shows, the full interactivity of the scatter plot (including the legend updates with the recalculated summary statistics and linear regression) is retained. .. GENERATED FROM PYTHON SOURCE LINES 72-73 We can also save the plot to an image .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: Python fig = load_interactive_scatter_plot( EXAMPLES / 'scatter_plot.pkl' ) fig.to_image( EXAMPLES / '_static' / 'loaded_scatter_plot.png' ) # sphinx_gallery_thumbnail_path = '../../examples/_static/loaded_scatter_plot.png' .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 80-87 Other Interactive Plots ----------------------- The only other polts that have to be serialized like above to retain their interactivity are matrix plots (i.e. those created by :func:`tsunami_ip_utils.viz.viz.matrix_plot``). These figure objects also have a ``save state`` method (i.e. :meth:`tsunami_ip_utils.viz.matrix_plot.InteractiveMatrixPlot.save_state`), which can be used to save the plot state to a pickle file. And these plots can be reloaded, likewise, using the :func:`tsunami_ip_utils.viz.matrix_plot.load_interactive_matrix_plot` function. An example is shown below for a matrix that is `very` time consuming to generate. .. GENERATED FROM PYTHON SOURCE LINES 87-91 .. code-block:: Python from tsunami_ip_utils.viz.matrix_plot import load_interactive_matrix_plot fig = load_interactive_matrix_plot( EXAMPLES / 'data' / 'hmf_contribution_comparisons_nuclide_reaction.pkl' ) fig.show() .. raw:: html
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 53.237 seconds) .. _sphx_glr_download_auto_examples_saving_and_loading_interactive_plots.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: saving_and_loading_interactive_plots.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: saving_and_loading_interactive_plots.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: saving_and_loading_interactive_plots.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_