def plot_to_figure(plot, reset_nclicks=0): """ Convert a HoloViews plotly plot to a plotly.py Figure. Args: plot: A HoloViews plotly plot object reset_nclicks: Number of times a reset button associated with the plot has been clicked Returns: A plotly.py Figure """ fig_dict = plot.state clean_internal_figure_properties(fig_dict) # Enable uirevision to preserve user-interaction state # Don't use reset_nclicks directly because 0 is treated as no revision fig_dict['layout']['uirevision'] = "reset-" + str(reset_nclicks) # Remove range specification so plotly.js autorange + uirevision is in control for k in fig_dict['layout']: if k.startswith('xaxis') or k.startswith('yaxis'): fig_dict['layout'][k].pop('range', None) # Remove figure width height, let container decide fig_dict['layout'].pop('width', None) fig_dict['layout'].pop('height', None) # Pass to figure constructor to expand magic underscore notation return go.Figure(fig_dict)
def plot_to_figure(plot, reset_nclicks=0, layout_ranges=None, responsive=True, use_ranges=True): """ Convert a HoloViews plotly plot to a plotly.py Figure. Args: plot: A HoloViews plotly plot object reset_nclicks: Number of times a reset button associated with the plot has been clicked Returns: A plotly.py Figure """ fig_dict = plot.state clean_internal_figure_properties(fig_dict) # Enable uirevision to preserve user-interaction state # Don't use reset_nclicks directly because 0 is treated as no revision fig_dict['layout']['uirevision'] = "reset-" + str(reset_nclicks) # Remove range specification so plotly.js autorange + uirevision is in control if layout_ranges and use_ranges: for k in fig_dict['layout']: if k.startswith('xaxis') or k.startswith('yaxis'): fig_dict['layout'][k].pop('range', None) if k.startswith('mapbox'): fig_dict['layout'][k].pop('zoom', None) fig_dict['layout'][k].pop('center', None) # Remove figure width height, let container decide if responsive: fig_dict['layout'].pop('autosize', None) if responsive is True or responsive == "width": fig_dict['layout'].pop('width', None) if responsive is True or responsive == "height": fig_dict['layout'].pop('height', None) # Pass to figure constructor to expand magic underscore notation fig = go.Figure(fig_dict) if layout_ranges and use_ranges: fig.update_layout(layout_ranges) return fig