| 
16 | 16 | from plotly.exceptions import PlotlyError  | 
17 | 17 | 
 
  | 
18 | 18 | 
 
  | 
 | 19 | +try:  | 
 | 20 | +    import IPython  | 
 | 21 | +    _ipython_imported = True  | 
 | 22 | +except ImportError:  | 
 | 23 | +    _ipython_imported = False  | 
 | 24 | + | 
 | 25 | +try:  | 
 | 26 | +    import matplotlib  | 
 | 27 | +    _matplotlib_imported = True  | 
 | 28 | +except ImportError:  | 
 | 29 | +    _matplotlib_imported = False  | 
 | 30 | + | 
 | 31 | + | 
19 | 32 | __PLOTLY_OFFLINE_INITIALIZED = False  | 
20 | 33 | 
 
  | 
21 | 34 | 
 
  | 
@@ -171,6 +184,34 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',  | 
171 | 184 |                            height=height, width=width)))  | 
172 | 185 | 
 
  | 
173 | 186 | 
 
  | 
 | 187 | +def iplot_mpl(mpl_fig,mpl_to_plotly_kw={},iplot_kw={}):  | 
 | 188 | +    '''  | 
 | 189 | +    Convert a matplotlib figure to plotly dictionary  plot inside an   | 
 | 190 | +    IPython notebook without connecting to an external server.  | 
 | 191 | +    '''  | 
 | 192 | +    plotly_plot = tools.mpl_to_plotly(mpl_fig,**mpl_to_plotly_kw)  | 
 | 193 | +    return iplot(plotly_plot,**iplot_kw)  | 
 | 194 | + | 
 | 195 | +def plotly_takeover(**kwargs):  | 
 | 196 | +    '''  | 
 | 197 | +    Enable the automatic display of figures in the IPython Notebook.  | 
 | 198 | +    This function should be used with the inline Matplotlib backend  | 
 | 199 | +    that ships with IPython that can be enabled with `%pylab inline`  | 
 | 200 | +    or `%matplotlib inline`. This works by adding an HTML formatter  | 
 | 201 | +    for Figure objects; the existing SVG/PNG formatters will remain  | 
 | 202 | +    enabled.  | 
 | 203 | +      | 
 | 204 | +    (idea taken from `mpld3._display.enable_notebook`)  | 
 | 205 | +    '''  | 
 | 206 | +    if __PLOTLY_OFFLINE_INITIALIZED != True:  | 
 | 207 | +        init_notebook_mode()  | 
 | 208 | +    ip = IPython.core.getipython.get_ipython()  | 
 | 209 | +    formatter = ip.display_formatter.formatters['text/html']  | 
 | 210 | +    formatter.for_type(matplotlib.figure.Figure,  | 
 | 211 | +                       lambda fig, kwds=kwargs: iplot_mpl(fig, **kwds))  | 
 | 212 | + | 
 | 213 | +      | 
 | 214 | + | 
174 | 215 | def plot():  | 
175 | 216 |     """ Configured to work with localhost Plotly graph viewer  | 
176 | 217 |     """  | 
 | 
0 commit comments