Skip to content

added iplot_mpl() and plotly_takeover() #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion plotly/offline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
from . offline import (
download_plotlyjs,
init_notebook_mode,
iplot
iplot,
iplot_mpl,
plotly_takeover,
)
41 changes: 41 additions & 0 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
from plotly.exceptions import PlotlyError


try:
import IPython
_ipython_imported = True
except ImportError:
_ipython_imported = False

try:
import matplotlib
_matplotlib_imported = True
except ImportError:
_matplotlib_imported = False


__PLOTLY_OFFLINE_INITIALIZED = False


Expand Down Expand Up @@ -171,6 +184,34 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
height=height, width=width)))


def iplot_mpl(mpl_fig,mpl_to_plotly_kw={},iplot_kw={}):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add spacing between the args
def iplot_mpl(mpl_fig, mpl_to_plotly_kw={}, iplot_kw={}):

'''
Convert a matplotlib figure to plotly dictionary plot inside an
IPython notebook without connecting to an external server.
'''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arsenovic thanks for adding the docstrings! It might be good if they're expanded a little bit more (like https://github.com/arsenovic/plotly.py/blob/plotly_notebook_takeover/plotly/offline/offline.py#L82-L104) to include keyword argument descriptions and an example if applicable.

plotly_plot = tools.mpl_to_plotly(mpl_fig,**mpl_to_plotly_kw)
return iplot(plotly_plot,**iplot_kw)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

little 🐄 to keep with the consistency of this file: add a line here (so there are 2 lines between defs)

def plotly_takeover(**kwargs):
'''
Enable the automatic display of figures in the IPython Notebook.
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`)
'''
if __PLOTLY_OFFLINE_INITIALIZED != True:
init_notebook_mode()
ip = IPython.core.getipython.get_ipython()
formatter = ip.display_formatter.formatters['text/html']
formatter.for_type(matplotlib.figure.Figure,
lambda fig, kwds=kwargs: iplot_mpl(fig, **kwds))



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and rm one of these lines

def plot():
""" Configured to work with localhost Plotly graph viewer
"""
Expand Down