Python plotly.offline.init_notebook_mode() Examples

The following are 4 code examples of plotly.offline.init_notebook_mode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module plotly.offline , or try the search function .
Example #1
Source File: explorer.py    From lens with Apache License 2.0 6 votes vote down vote up
def _render(fig, showlegend=None):
    """Plot a matploltib or plotly figure"""
    if isinstance(fig, plt.Figure):
        fig = plotly.tools.mpl_to_plotly(fig, **PLOTLY_TO_MPL_KWS)

    if showlegend is not None:
        fig.layout["showlegend"] = showlegend

    if not IN_NOTEBOOK:
        message = "Lens explorer can only plot in a Jupyter notebook"
        logger.error(message)
        raise ValueError(message)
    else:
        if not py.offline.__PLOTLY_OFFLINE_INITIALIZED:
            py.init_notebook_mode()
        return py.iplot(fig, **PLOTLY_KWS) 
Example #2
Source File: utils.py    From word-mesh with MIT License 6 votes vote down vote up
def save_wordmesh_as_html(self, coordinates, filename='temp-plot.html', 
                              animate=False, autozoom=True, notebook_mode=False):

        zoom = 1
        labels = ['default label']
        traces = []
        if animate:
            for i in range(coordinates.shape[0]):
                
                traces.append(self._get_trace(coordinates[i]))
                labels = list(map(str,range(coordinates.shape[0])))
                
        else:

            if autozoom:
                zoom = self._get_zoom(coordinates)
            traces = [self._get_trace(coordinates, zoom=zoom)]
            
        layout = self._get_layout(labels, zoom=zoom)
            
        fig = self.generate_figure(traces, labels, layout)
        
        if notebook_mode:
            py.init_notebook_mode(connected=True)
            py.iplot(fig, filename=filename, show_link=False)
        else:
            py.plot(fig, filename=filename, auto_open=False, show_link=False) 
Example #3
Source File: offline.py    From lddmm-ot with MIT License 5 votes vote down vote up
def enable_mpl_offline(resize=False, strip_style=False,
                       verbose=False, show_link=True,
                       link_text='Export to plot.ly', validate=True):
    """
    Convert mpl plots to locally hosted HTML documents.

    This function should be used with the inline matplotlib backend
    that ships with IPython that can be enabled with `%pylab inline`
    or `%matplotlib inline`. This works by adding an HTML formatter
    for Figure objects; the existing SVG/PNG formatters will remain
    enabled.

    (idea taken from `mpld3._display.enable_notebook`)

    Example:
    ```
    from plotly.offline import enable_mpl_offline
    import matplotlib.pyplot as plt

    enable_mpl_offline()

    fig = plt.figure()
    x = [10, 15, 20, 25, 30]
    y = [100, 250, 200, 150, 300]
    plt.plot(x, y, "o")
    fig
    ```
    """
    init_notebook_mode()

    ip = IPython.core.getipython.get_ipython()
    formatter = ip.display_formatter.formatters['text/html']
    formatter.for_type(matplotlib.figure.Figure,
                       lambda fig: iplot_mpl(fig, resize, strip_style, verbose,
                                             show_link, link_text, validate)) 
Example #4
Source File: plot.py    From sound_field_analysis-py with MIT License 4 votes vote down vote up
def showTrace(trace, layout=None, title=None):
    """ Wrapper around Plotly's offline .plot() function

    Parameters
    ----------
    trace : plotly_trace
        Plotly generated trace to be displayed offline
    layout : plotly.graph_objs.Layout, optional
        Layout of plot to be displayed offline
    title : str, optional
        Title of plot to be displayed offline
    # colorize : bool, optional
    #     Toggles bw / colored plot [Default: True]

    Returns
    -------
    fig : plotly_fig_handle
        JSON representation of generated figure
    """
    if layout is None:
        layout = go.Layout(
            scene=dict(
                xaxis=dict(range=[-1, 1]),
                yaxis=dict(range=[-1, 1]),
                zaxis=dict(range=[-1, 1]),
                aspectmode='cube'
            )
        )
    # Wrap trace in array if needed
    if not isinstance(trace, list):
        trace = [trace]

    fig = go.Figure(
        data=trace,
        layout=layout
    )

    if title is not None:
        fig.layout.update(title=title)
        filename = f'{title}.html'
    else:
        try:
            filename = f'{fig.layout.title}.html'
        except TypeError:
            filename = f'{current_time()}.html'

    # if colorize:
    #    data[0].autocolorscale = False
    #    data[0].surfacecolor = [0, 0.5, 1]
    if env_info() == 'jupyter_notebook':
        plotly_off.init_notebook_mode()
        plotly_off.iplot(fig)
    else:
        plotly_off.plot(fig, filename=filename)

    return fig