From 5c73f4c4838669c091c41659d79f9582730a3e87 Mon Sep 17 00:00:00 2001 From: arsenovic Date: Thu, 3 Dec 2015 14:55:34 -0500 Subject: [PATCH 1/6] added iplot_mpl() and plotly_takeover() `iplot_mpl()` allows offline matplotlib figure conversion for ipython notebook inline mode. `plotly_takeover()` is a compound command that allows an entire notebook session that was written using matplotlib to use offline plotly without any modification. the later idea was taken from https://github.com/jakevdp/mpld3/blob/202792bcddbfe957ade08a5085691ab176a4563d/mpld3/_display.py#L363 --- plotly/offline/__init__.py | 4 +++- plotly/offline/offline.py | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/plotly/offline/__init__.py b/plotly/offline/__init__.py index df04b62cc01..416105dab7e 100644 --- a/plotly/offline/__init__.py +++ b/plotly/offline/__init__.py @@ -6,5 +6,7 @@ from . offline import ( download_plotlyjs, init_notebook_mode, - iplot + iplot, + iplot_mpl, + plotly_takeover, ) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index cb9e3762bed..44cecd347f7 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -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. + ''' + plotly_plot = tools.mpl_to_plotly(mpl_fig,**mpl_to_plotly_kw) + return iplot(plotly_plot,**iplot_kw) + +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)) + + + def plot(): """ Configured to work with localhost Plotly graph viewer """ From 9981fe7ecc9ad149345b025d81f06cf67b12f774 Mon Sep 17 00:00:00 2001 From: Chelsea Date: Fri, 15 Jan 2016 09:45:08 -0500 Subject: [PATCH 2/6] add `plot_mpl` function, docstrings, examples, tests --- plotly/offline/__init__.py | 3 +- plotly/offline/offline.py | 114 ++++++++++++++++-- .../test_offline/test_offline.py | 76 ++++++++++++ 3 files changed, 182 insertions(+), 11 deletions(-) diff --git a/plotly/offline/__init__.py b/plotly/offline/__init__.py index f54d6677acc..deeeb069d86 100644 --- a/plotly/offline/__init__.py +++ b/plotly/offline/__init__.py @@ -7,7 +7,8 @@ download_plotlyjs, init_notebook_mode, iplot, - plot, iplot_mpl, + plot, + plot_mpl, plotly_takeover ) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index f03593d28d8..2f17f9eca2a 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -204,9 +204,7 @@ def plot(figure_or_data, from plotly.offline import plot import plotly.graph_objs as go - plot([ - go.Scatter(x=[1, 2, 3], y=[3, 2 6]) - ], filename='my-graph.html') + plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html') ``` More examples below. @@ -313,18 +311,99 @@ def plot(figure_or_data, return plot_html -def iplot_mpl(mpl_fig,mpl_to_plotly_kw={},iplot_kw={}): +def plot_mpl(mpl_fig, resize=False, strip_style=False, + verbose=False, **kwargs): ''' - Convert a matplotlib figure to plotly dictionary plot inside an - IPython notebook without connecting to an external server. + Convert a matplotlib figure to a plotly graph locally as an HTML document + or string + + For more information on converting matplotlib visualizations to plotly + graphs, call help(plotly.tools.mpl_to_plotly) + + For more information on creating plotly charts locally as an HTML document + or string, call help(plotly.offline.plot) + + :param (matplotlib figure) mpl_fig: matplotlib figure to convert to a + plotly graph + :param (bool) resize: Default = False + :param (bool) strip_style: Default = False + :param (bool) verbose: Default = False + :param kwargs: kwargs passed through `plotly.offline.plot`. + For more information on valid kwargs call `help(plotly.offline.plot)` + :return (None|string): if `output_type` is 'file' (default), then the graph + is saved as a standalone HTML file and `plot_mpl` returns None. + If `output_type` is 'div', then `plot` returns a string that contains + the HTML
that contains the graph and the script to generate the + graph. For more information about `output_type` call + `help(plotly.offline.plot)` + + Example: + ``` + from plotly.offline import init_notebook_mode, plot_mpl + import matplotlib.pyplot as plt + + init_notebook_mode() + + fig = plt.figure() + x = [10, 15, 20, 25, 30] + y = [100, 250, 200, 150, 300] + plt.plot(x, y, "o") + + plot_mpl(fig) + ``` ''' - plotly_plot = tools.mpl_to_plotly(mpl_fig,**mpl_to_plotly_kw) - return iplot(plotly_plot,**iplot_kw) + plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose) + return plot(plotly_plot, **kwargs) + + +def iplot_mpl(mpl_fig, resize=False, strip_style=False, + verbose=False, **kwargs): + ''' + Convert a matplotlib figure to a plotly graph and plot inside an IPython + notebook without connecting to an external server. + + To save the chart to Plotly Cloud or Plotly Enterprise, use + `plotly.tools.mpl_to_plotly`. + + For more information on converting matplotlib visualizations to plotly + graphs call `help(plotly.tools.mpl_to_plotly)` + + For more information on plotting plotly charts offline in an Ipython + notebook call `help(plotly.offline.iplot)` + + :param (matplotlib figure) mpl_fig: matplotlib figure to convert to a + plotly graph + :param (bool) resize: Default = False + :param (bool) strip_style: Default = False + :param (bool) verbose: Default = False + :param kwargs: kwargs passed through `plotly.offline.iplot`. + For more information on valid kwargs call `help(plotly.offline.iplot)` + :return: draws converted plotly figure in Ipython notebook + + Example: + ``` + from plotly.offline import init_notebook_mode, iplot_mpl + import matplotlib.pyplot as plt + + init_notebook_mode() + + fig = plt.figure() + x = [10, 15, 20, 25, 30] + y = [100, 250, 200, 150, 300] + plt.plot(x, y, "o") + + iplot_mpl(fig) + ``` + ''' + plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose) + return iplot(plotly_plot, **kwargs) def plotly_takeover(**kwargs): ''' - Enable the automatic display of figures in the IPython Notebook. + Enable the automatic matplotlib to plotly conversion and display + of figures in an 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 @@ -332,8 +411,23 @@ def plotly_takeover(**kwargs): enabled. (idea taken from `mpld3._display.enable_notebook`) + + Example: + ``` + from plotly.offline import init_notebook_mode, plotly_takeover + import matplotlib.pyplot as plt + + init_notebook_mode + plotly_takeover() + + fig = plt.figure() + x = [10, 15, 20, 25, 30] + y = [100, 250, 200, 150, 300] + plt.plot(x, y, "o") + fig + ``` ''' - if __PLOTLY_OFFLINE_INITIALIZED != True: + if not __PLOTLY_OFFLINE_INITIALIZED: init_notebook_mode() ip = IPython.core.getipython.get_ipython() formatter = ip.display_formatter.formatters['text/html'] diff --git a/plotly/tests/test_optional/test_offline/test_offline.py b/plotly/tests/test_optional/test_offline/test_offline.py index 16fb70ebc7e..5cb88c1707b 100644 --- a/plotly/tests/test_optional/test_offline/test_offline.py +++ b/plotly/tests/test_optional/test_offline/test_offline.py @@ -9,6 +9,24 @@ import plotly +# TODO: matplotlib-build-wip +from plotly.tools import _matplotlylib_imported +if _matplotlylib_imported: + import matplotlib + + # Force matplotlib to not use any Xwindows backend. + matplotlib.use('Agg') + import matplotlib.pyplot as plt + +# Generate matplotlib plot for tests +fig = plt.figure() + +x = [10, 20, 30] +y = [100, 200, 300] +plt.plot(x, y, "o") + +PLOTLYJS = plotly.offline.offline.get_plotlyjs() + class PlotlyOfflineTestCase(TestCase): def setUp(self): @@ -22,3 +40,61 @@ def test_iplot_works_after_you_call_init_notebook_mode(self): plotly.tools._ipython_imported = True plotly.offline.init_notebook_mode() plotly.offline.iplot([{}]) + + def test_iplot_mpl_works_after_you_call_init_notebook_mode(self): + plotly.tools._ipython_imported = True + plotly.offline.init_notebook_mode() + plotly.offline.iplot_mpl(fig) + + +class PlotlyOfflineMPLTestCase(TestCase): + def setUp(self): + pass + + def _read_html(self, file_url): + """ Read and return the HTML contents from a file_url in the + form e.g. file:///Users/chriddyp/Repos/plotly.py/plotly-temp.html + """ + with open(file_url.replace('file://', '').replace(' ', '')) as f: + return f.read() + + def test_default_mpl_plot_generates_expected_html(self): + data_json = ('[{"name": "_line0", "yaxis": "y1", "marker": {"color":' + + ' "#0000FF", "opacity": 1, "line": {"color": "#000000",' + + ' "width": 0.5}, "symbol": "dot", "size": 6.0}, "mode":' + + ' "markers", "xaxis": "x1", "y": [100.0, 200.0, 300.0],' + + ' "x": [10.0, 20.0, 30.0], "type": "scatter"}]') + layout_json = ('{"autosize": false, "width": 640, "showlegend": ' + + 'false, "xaxis1": {"tickfont": {"size": 12.0}, ' + + '"domain": [0.0, 1.0], "ticks": "inside", "showgrid":' + + ' false, "range": [10.0, 30.0], "mirror": "ticks", ' + + '"zeroline": false, "showline": true, "nticks": 5, ' + + '"type": "linear", "anchor": "y1", "side": "bottom"},' + + ' "height": 480, "yaxis1": {"tickfont": ' + + '{"size": 12.0}, "domain": [0.0, 1.0], "ticks": ' + + '"inside", "showgrid": false, "range": [100.0, 300.0]' + + ', "mirror": "ticks", "zeroline": false, "showline": ' + + 'true, "nticks": 5, "type": "linear", "anchor": "x1",' + + ' "side": "left"}, "hovermode": "closest", "margin":' + + ' {"b": 47, "r": 63, "pad": 0, "t": 47, "l": 80}}') + html = self._read_html(plotly.offline.plot_mpl(fig)) + + # just make sure a few of the parts are in here + # like PlotlyOfflineTestCase(TestCase) in test_core + self.assertTrue('Plotly.newPlot' in html) # plot command is in there + self.assertTrue(data_json in html) # data is in there + self.assertTrue(layout_json in html) # layout is in there too + self.assertTrue(PLOTLYJS in html) # and the source code + # and it's an doc + self.assertTrue(html.startswith('') and html.endswith('')) + + def test_including_plotlyjs(self): + html = self._read_html(plotly.offline.plot_mpl(fig, include_plotlyjs=False)) + self.assertTrue(PLOTLYJS not in html) + + def test_div_output(self): + html = plotly.offline.plot_mpl(fig, output_type='div') + + self.assertTrue('' not in html and '' not in html) + self.assertTrue(html.startswith('
') and html.endswith('
')) + From 6dd6ed8057e679ab8bd5d912bebf3c49f57fc02d Mon Sep 17 00:00:00 2001 From: Chelsea Date: Fri, 15 Jan 2016 19:04:36 -0500 Subject: [PATCH 3/6] - :hocho: `*kwargs*` -update docstring with keyword arguments --- plotly/graph_reference/default-schema.json | 4 +- plotly/offline/__init__.py | 4 +- plotly/offline/offline.py | 116 ++++++++++++++------- 3 files changed, 81 insertions(+), 43 deletions(-) diff --git a/plotly/graph_reference/default-schema.json b/plotly/graph_reference/default-schema.json index f238807bf48..adff88bc8f5 100644 --- a/plotly/graph_reference/default-schema.json +++ b/plotly/graph_reference/default-schema.json @@ -426,12 +426,14 @@ ] }, "dragmode": { - "description": "Determines the mode of drag interactions.", + "description": "Determines the mode of drag interactions. *select* and *lasso* apply only to scatter traces with markers or text. *orbit* and *turntable* apply only to 3D scenes.", "role": "info", "valType": "enumerated", "values": [ "zoom", "pan", + "select", + "lasso", "orbit", "turntable" ] diff --git a/plotly/offline/__init__.py b/plotly/offline/__init__.py index deeeb069d86..f4a5fc2d2b6 100644 --- a/plotly/offline/__init__.py +++ b/plotly/offline/__init__.py @@ -5,10 +5,10 @@ """ from . offline import ( download_plotlyjs, + enable_mpl_offline, init_notebook_mode, iplot, iplot_mpl, plot, - plot_mpl, - plotly_takeover + plot_mpl ) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index 2f17f9eca2a..7d50c74505e 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -312,30 +312,52 @@ def plot(figure_or_data, def plot_mpl(mpl_fig, resize=False, strip_style=False, - verbose=False, **kwargs): - ''' - Convert a matplotlib figure to a plotly graph locally as an HTML document - or string + verbose=False, show_link=True, link_text='Export to plot.ly', + validate=True, output_type='file', include_plotlyjs=True, + filename='temp-plot.html', auto_open=True): + """ + Convert a matplotlib figure to a Plotly graph stored locally as HTML. For more information on converting matplotlib visualizations to plotly - graphs, call help(plotly.tools.mpl_to_plotly) + graphs, call help(plotly.plot_mpl) For more information on creating plotly charts locally as an HTML document or string, call help(plotly.offline.plot) - :param (matplotlib figure) mpl_fig: matplotlib figure to convert to a - plotly graph - :param (bool) resize: Default = False - :param (bool) strip_style: Default = False - :param (bool) verbose: Default = False - :param kwargs: kwargs passed through `plotly.offline.plot`. - For more information on valid kwargs call `help(plotly.offline.plot)` - :return (None|string): if `output_type` is 'file' (default), then the graph - is saved as a standalone HTML file and `plot_mpl` returns None. - If `output_type` is 'div', then `plot` returns a string that contains - the HTML
that contains the graph and the script to generate the - graph. For more information about `output_type` call - `help(plotly.offline.plot)` + mpl_fig -- a matplotlib figure object to convert to a plotly graph + + Keyword arguments: + resize (default=False) -- allow plotly to choose the figure size. + strip_style (default=False) -- allow plotly to choose style options. + verbose (default=False) -- print message. + show_link (default=True) -- display a link in the bottom-right corner of + of the chart that will export the chart to Plotly Cloud or + Plotly Enterprise + link_text (default='Export to plot.ly') -- the text of export link + validate (default=True) -- validate that all of the keys in the figure + are valid? omit if your version of plotly.js has become outdated + with your version of graph_reference.json or if you need to include + extra, unnecessary keys in your figure. + output_type ('file' | 'div' - default 'file') -- if 'file', then + the graph is saved as a standalone HTML file and `plot` + returns None. + If 'div', then `plot` returns a string that just contains the + HTML
that contains the graph and the script to generate the + graph. + Use 'file' if you want to save and view a single graph at a time + in a standalone HTML file. + Use 'div' if you are embedding these graphs in an HTML file with + other graphs or HTML markup, like a HTML report or an website. + include_plotlyjs (default=True) -- If True, include the plotly.js + source code in the output file or string. + Set as False if your HTML file already contains a copy of the plotly.js + library. + filename (default='temp-plot.html') -- The local filename to save the + outputted chart to. If the filename already exists, it will be + overwritten. This argument only applies if `output_type` is 'file'. + auto_open (default=True) -- If True, open the saved file in a + web browser after saving. + This argument only applies if `output_type` is 'file'. Example: ``` @@ -351,14 +373,16 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False, plot_mpl(fig) ``` - ''' + """ plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose) - return plot(plotly_plot, **kwargs) + return plot(plotly_plot, show_link, link_text, validate, output_type, + include_plotlyjs, filename, auto_open) def iplot_mpl(mpl_fig, resize=False, strip_style=False, - verbose=False, **kwargs): - ''' + verbose=False, show_link=True, + link_text='Export to plot.ly', validate=True): + """ Convert a matplotlib figure to a plotly graph and plot inside an IPython notebook without connecting to an external server. @@ -371,14 +395,24 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False, For more information on plotting plotly charts offline in an Ipython notebook call `help(plotly.offline.iplot)` - :param (matplotlib figure) mpl_fig: matplotlib figure to convert to a - plotly graph - :param (bool) resize: Default = False - :param (bool) strip_style: Default = False - :param (bool) verbose: Default = False - :param kwargs: kwargs passed through `plotly.offline.iplot`. - For more information on valid kwargs call `help(plotly.offline.iplot)` - :return: draws converted plotly figure in Ipython notebook + mpl_fig -- a matplotlib.figure to convert to a plotly graph + + Keyword arguments: + resize (default=False) -- allow plotly to choose the figure size. + strip_style (default=False) -- allow plotly to choose style options. + verbose (default=False) -- print message. + show_link (default=True) -- display a link in the bottom-right corner of + of the chart that will export the chart to Plotly Cloud or + Plotly Enterprise + show_link (default=True) -- display a link in the bottom-right corner of + of the chart that will export the chart to + Plotly Cloud or Plotly Enterprise + link_text (default='Export to plot.ly') -- the text of export link + validate (default=True) -- validate that all of the keys in the figure + are valid? omit if your version of plotly.js + has become outdated with your version of + graph_reference.json or if you need to include + extra, unnecessary keys in your figure. Example: ``` @@ -394,17 +428,18 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False, iplot_mpl(fig) ``` - ''' + """ plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose) - return iplot(plotly_plot, **kwargs) + return iplot(plotly_plot, show_link, link_text, validate) -def plotly_takeover(**kwargs): - ''' - Enable the automatic matplotlib to plotly conversion and display - of figures in an IPython Notebook. +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 + 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 @@ -417,7 +452,7 @@ def plotly_takeover(**kwargs): from plotly.offline import init_notebook_mode, plotly_takeover import matplotlib.pyplot as plt - init_notebook_mode + init_notebook_mode() plotly_takeover() fig = plt.figure() @@ -426,11 +461,12 @@ def plotly_takeover(**kwargs): plt.plot(x, y, "o") fig ``` - ''' + """ if not __PLOTLY_OFFLINE_INITIALIZED: 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)) + lambda fig: iplot_mpl(fig, resize, strip_style, verbose, + show_link, link_text, validate)) From 2b57d3d7992ee2b7ef4af44e92993b00b4286b4c Mon Sep 17 00:00:00 2001 From: Chelsea Date: Sun, 17 Jan 2016 17:17:25 -0500 Subject: [PATCH 4/6] make offline plot_mpl test more readable and update version --- plotly/offline/offline.py | 6 ++--- .../test_offline/test_offline.py | 27 ++++++------------- plotly/version.py | 2 +- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py index 7d50c74505e..53ebd853c6b 100644 --- a/plotly/offline/offline.py +++ b/plotly/offline/offline.py @@ -319,7 +319,7 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False, Convert a matplotlib figure to a Plotly graph stored locally as HTML. For more information on converting matplotlib visualizations to plotly - graphs, call help(plotly.plot_mpl) + graphs, call help(plotly.tools.mpl_to_plotly) For more information on creating plotly charts locally as an HTML document or string, call help(plotly.offline.plot) @@ -387,7 +387,7 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False, notebook without connecting to an external server. To save the chart to Plotly Cloud or Plotly Enterprise, use - `plotly.tools.mpl_to_plotly`. + `plotly.plotly.plot_mpl`. For more information on converting matplotlib visualizations to plotly graphs call `help(plotly.tools.mpl_to_plotly)` @@ -453,7 +453,7 @@ def enable_mpl_offline(resize=False, strip_style=False, import matplotlib.pyplot as plt init_notebook_mode() - plotly_takeover() + enable_mpl_offline() fig = plt.figure() x = [10, 15, 20, 25, 30] diff --git a/plotly/tests/test_optional/test_offline/test_offline.py b/plotly/tests/test_optional/test_offline/test_offline.py index 5cb88c1707b..d02c7c94e3e 100644 --- a/plotly/tests/test_optional/test_offline/test_offline.py +++ b/plotly/tests/test_optional/test_offline/test_offline.py @@ -6,18 +6,20 @@ from nose.tools import raises from unittest import TestCase +import json import plotly # TODO: matplotlib-build-wip from plotly.tools import _matplotlylib_imported + if _matplotlylib_imported: import matplotlib - # Force matplotlib to not use any Xwindows backend. matplotlib.use('Agg') import matplotlib.pyplot as plt + # Generate matplotlib plot for tests fig = plt.figure() @@ -59,24 +61,11 @@ def _read_html(self, file_url): return f.read() def test_default_mpl_plot_generates_expected_html(self): - data_json = ('[{"name": "_line0", "yaxis": "y1", "marker": {"color":' + - ' "#0000FF", "opacity": 1, "line": {"color": "#000000",' + - ' "width": 0.5}, "symbol": "dot", "size": 6.0}, "mode":' + - ' "markers", "xaxis": "x1", "y": [100.0, 200.0, 300.0],' + - ' "x": [10.0, 20.0, 30.0], "type": "scatter"}]') - layout_json = ('{"autosize": false, "width": 640, "showlegend": ' + - 'false, "xaxis1": {"tickfont": {"size": 12.0}, ' + - '"domain": [0.0, 1.0], "ticks": "inside", "showgrid":' + - ' false, "range": [10.0, 30.0], "mirror": "ticks", ' + - '"zeroline": false, "showline": true, "nticks": 5, ' + - '"type": "linear", "anchor": "y1", "side": "bottom"},' + - ' "height": 480, "yaxis1": {"tickfont": ' + - '{"size": 12.0}, "domain": [0.0, 1.0], "ticks": ' + - '"inside", "showgrid": false, "range": [100.0, 300.0]' + - ', "mirror": "ticks", "zeroline": false, "showline": ' + - 'true, "nticks": 5, "type": "linear", "anchor": "x1",' + - ' "side": "left"}, "hovermode": "closest", "margin":' + - ' {"b": 47, "r": 63, "pad": 0, "t": 47, "l": 80}}') + figure = plotly.tools.mpl_to_plotly(fig) + data = figure['data'] + layout = figure['layout'] + data_json = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder) + layout_json = json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder) html = self._read_html(plotly.offline.plot_mpl(fig)) # just make sure a few of the parts are in here diff --git a/plotly/version.py b/plotly/version.py index 6c08d428eb4..a4f8693ffb7 100644 --- a/plotly/version.py +++ b/plotly/version.py @@ -1 +1 @@ -__version__ = '1.9.4' +__version__ = '1.9.5' From 60a9025f92c58e224edc6f59820dc37fcd66c5c1 Mon Sep 17 00:00:00 2001 From: Chelsea Date: Sun, 17 Jan 2016 17:44:07 -0500 Subject: [PATCH 5/6] add matplotlib attr tag to matplotlib tests --- .../test_offline/test_offline.py | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/plotly/tests/test_optional/test_offline/test_offline.py b/plotly/tests/test_optional/test_offline/test_offline.py index d02c7c94e3e..632fe9fc7eb 100644 --- a/plotly/tests/test_optional/test_offline/test_offline.py +++ b/plotly/tests/test_optional/test_offline/test_offline.py @@ -5,6 +5,8 @@ from __future__ import absolute_import from nose.tools import raises +from nose.plugins.attrib import attr + from unittest import TestCase import json @@ -20,13 +22,6 @@ import matplotlib.pyplot as plt -# Generate matplotlib plot for tests -fig = plt.figure() - -x = [10, 20, 30] -y = [100, 200, 300] -plt.plot(x, y, "o") - PLOTLYJS = plotly.offline.offline.get_plotlyjs() @@ -43,13 +38,29 @@ def test_iplot_works_after_you_call_init_notebook_mode(self): plotly.offline.init_notebook_mode() plotly.offline.iplot([{}]) + @attr('matplotlib') def test_iplot_mpl_works_after_you_call_init_notebook_mode(self): + # Generate matplotlib plot for tests + fig = plt.figure() + + x = [10, 20, 30] + y = [100, 200, 300] + plt.plot(x, y, "o") + plotly.tools._ipython_imported = True plotly.offline.init_notebook_mode() plotly.offline.iplot_mpl(fig) +@attr('matplotlib') class PlotlyOfflineMPLTestCase(TestCase): + # Generate matplotlib plot for tests + fig = plt.figure() + + x = [10, 20, 30] + y = [100, 200, 300] + plt.plot(x, y, "o") + def setUp(self): pass From 7f6298a940afd0c9d3ee9fead09725c5053fe846 Mon Sep 17 00:00:00 2001 From: Chelsea Date: Sun, 17 Jan 2016 18:00:48 -0500 Subject: [PATCH 6/6] add matplotlib attr tag --- .../test_offline/test_offline.py | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/plotly/tests/test_optional/test_offline/test_offline.py b/plotly/tests/test_optional/test_offline/test_offline.py index 632fe9fc7eb..5f4944b8c27 100644 --- a/plotly/tests/test_optional/test_offline/test_offline.py +++ b/plotly/tests/test_optional/test_offline/test_offline.py @@ -52,15 +52,7 @@ def test_iplot_mpl_works_after_you_call_init_notebook_mode(self): plotly.offline.iplot_mpl(fig) -@attr('matplotlib') class PlotlyOfflineMPLTestCase(TestCase): - # Generate matplotlib plot for tests - fig = plt.figure() - - x = [10, 20, 30] - y = [100, 200, 300] - plt.plot(x, y, "o") - def setUp(self): pass @@ -71,7 +63,15 @@ def _read_html(self, file_url): with open(file_url.replace('file://', '').replace(' ', '')) as f: return f.read() + @attr('matplotlib') def test_default_mpl_plot_generates_expected_html(self): + # Generate matplotlib plot for tests + fig = plt.figure() + + x = [10, 20, 30] + y = [100, 200, 300] + plt.plot(x, y, "o") + figure = plotly.tools.mpl_to_plotly(fig) data = figure['data'] layout = figure['layout'] @@ -88,13 +88,3 @@ def test_default_mpl_plot_generates_expected_html(self): # and it's an doc self.assertTrue(html.startswith('') and html.endswith('')) - def test_including_plotlyjs(self): - html = self._read_html(plotly.offline.plot_mpl(fig, include_plotlyjs=False)) - self.assertTrue(PLOTLYJS not in html) - - def test_div_output(self): - html = plotly.offline.plot_mpl(fig, output_type='div') - - self.assertTrue('' not in html and '' not in html) - self.assertTrue(html.startswith('
') and html.endswith('
')) -