-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,7 @@ | |
from . offline import ( | ||
download_plotlyjs, | ||
init_notebook_mode, | ||
iplot | ||
iplot, | ||
iplot_mpl, | ||
plotly_takeover, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
||
|
@@ -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={}): | ||
''' | ||
Convert a matplotlib figure to plotly dictionary plot inside an | ||
IPython notebook without connecting to an external server. | ||
''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
""" | ||
|
There was a problem hiding this comment.
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={}):