Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9981fe7

Browse filesBrowse files
committed
add plot_mpl function, docstrings, examples, tests
1 parent 00b6eb2 commit 9981fe7
Copy full SHA for 9981fe7

File tree

3 files changed

+182
-11
lines changed
Filter options

3 files changed

+182
-11
lines changed

‎plotly/offline/__init__.py

Copy file name to clipboardExpand all lines: plotly/offline/__init__.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
download_plotlyjs,
88
init_notebook_mode,
99
iplot,
10-
plot,
1110
iplot_mpl,
11+
plot,
12+
plot_mpl,
1213
plotly_takeover
1314
)

‎plotly/offline/offline.py

Copy file name to clipboardExpand all lines: plotly/offline/offline.py
+104-10Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ def plot(figure_or_data,
204204
from plotly.offline import plot
205205
import plotly.graph_objs as go
206206
207-
plot([
208-
go.Scatter(x=[1, 2, 3], y=[3, 2 6])
209-
], filename='my-graph.html')
207+
plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html')
210208
```
211209
More examples below.
212210
@@ -313,27 +311,123 @@ def plot(figure_or_data,
313311
return plot_html
314312

315313

316-
def iplot_mpl(mpl_fig,mpl_to_plotly_kw={},iplot_kw={}):
314+
def plot_mpl(mpl_fig, resize=False, strip_style=False,
315+
verbose=False, **kwargs):
317316
'''
318-
Convert a matplotlib figure to plotly dictionary plot inside an
319-
IPython notebook without connecting to an external server.
317+
Convert a matplotlib figure to a plotly graph locally as an HTML document
318+
or string
319+
320+
For more information on converting matplotlib visualizations to plotly
321+
graphs, call help(plotly.tools.mpl_to_plotly)
322+
323+
For more information on creating plotly charts locally as an HTML document
324+
or string, call help(plotly.offline.plot)
325+
326+
:param (matplotlib figure) mpl_fig: matplotlib figure to convert to a
327+
plotly graph
328+
:param (bool) resize: Default = False
329+
:param (bool) strip_style: Default = False
330+
:param (bool) verbose: Default = False
331+
:param kwargs: kwargs passed through `plotly.offline.plot`.
332+
For more information on valid kwargs call `help(plotly.offline.plot)`
333+
:return (None|string): if `output_type` is 'file' (default), then the graph
334+
is saved as a standalone HTML file and `plot_mpl` returns None.
335+
If `output_type` is 'div', then `plot` returns a string that contains
336+
the HTML <div> that contains the graph and the script to generate the
337+
graph. For more information about `output_type` call
338+
`help(plotly.offline.plot)`
339+
340+
Example:
341+
```
342+
from plotly.offline import init_notebook_mode, plot_mpl
343+
import matplotlib.pyplot as plt
344+
345+
init_notebook_mode()
346+
347+
fig = plt.figure()
348+
x = [10, 15, 20, 25, 30]
349+
y = [100, 250, 200, 150, 300]
350+
plt.plot(x, y, "o")
351+
352+
plot_mpl(fig)
353+
```
320354
'''
321-
plotly_plot = tools.mpl_to_plotly(mpl_fig,**mpl_to_plotly_kw)
322-
return iplot(plotly_plot,**iplot_kw)
355+
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
356+
return plot(plotly_plot, **kwargs)
357+
358+
359+
def iplot_mpl(mpl_fig, resize=False, strip_style=False,
360+
verbose=False, **kwargs):
361+
'''
362+
Convert a matplotlib figure to a plotly graph and plot inside an IPython
363+
notebook without connecting to an external server.
364+
365+
To save the chart to Plotly Cloud or Plotly Enterprise, use
366+
`plotly.tools.mpl_to_plotly`.
367+
368+
For more information on converting matplotlib visualizations to plotly
369+
graphs call `help(plotly.tools.mpl_to_plotly)`
370+
371+
For more information on plotting plotly charts offline in an Ipython
372+
notebook call `help(plotly.offline.iplot)`
373+
374+
:param (matplotlib figure) mpl_fig: matplotlib figure to convert to a
375+
plotly graph
376+
:param (bool) resize: Default = False
377+
:param (bool) strip_style: Default = False
378+
:param (bool) verbose: Default = False
379+
:param kwargs: kwargs passed through `plotly.offline.iplot`.
380+
For more information on valid kwargs call `help(plotly.offline.iplot)`
381+
:return: draws converted plotly figure in Ipython notebook
382+
383+
Example:
384+
```
385+
from plotly.offline import init_notebook_mode, iplot_mpl
386+
import matplotlib.pyplot as plt
387+
388+
init_notebook_mode()
389+
390+
fig = plt.figure()
391+
x = [10, 15, 20, 25, 30]
392+
y = [100, 250, 200, 150, 300]
393+
plt.plot(x, y, "o")
394+
395+
iplot_mpl(fig)
396+
```
397+
'''
398+
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
399+
return iplot(plotly_plot, **kwargs)
323400

324401

325402
def plotly_takeover(**kwargs):
326403
'''
327-
Enable the automatic display of figures in the IPython Notebook.
404+
Enable the automatic matplotlib to plotly conversion and display
405+
of figures in an IPython Notebook.
406+
328407
This function should be used with the inline Matplotlib backend
329408
that ships with IPython that can be enabled with `%pylab inline`
330409
or `%matplotlib inline`. This works by adding an HTML formatter
331410
for Figure objects; the existing SVG/PNG formatters will remain
332411
enabled.
333412
334413
(idea taken from `mpld3._display.enable_notebook`)
414+
415+
Example:
416+
```
417+
from plotly.offline import init_notebook_mode, plotly_takeover
418+
import matplotlib.pyplot as plt
419+
420+
init_notebook_mode
421+
plotly_takeover()
422+
423+
fig = plt.figure()
424+
x = [10, 15, 20, 25, 30]
425+
y = [100, 250, 200, 150, 300]
426+
plt.plot(x, y, "o")
427+
fig
428+
```
335429
'''
336-
if __PLOTLY_OFFLINE_INITIALIZED != True:
430+
if not __PLOTLY_OFFLINE_INITIALIZED:
337431
init_notebook_mode()
338432
ip = IPython.core.getipython.get_ipython()
339433
formatter = ip.display_formatter.formatters['text/html']

‎plotly/tests/test_optional/test_offline/test_offline.py

Copy file name to clipboardExpand all lines: plotly/tests/test_optional/test_offline/test_offline.py
+76Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99

1010
import plotly
1111

12+
# TODO: matplotlib-build-wip
13+
from plotly.tools import _matplotlylib_imported
14+
if _matplotlylib_imported:
15+
import matplotlib
16+
17+
# Force matplotlib to not use any Xwindows backend.
18+
matplotlib.use('Agg')
19+
import matplotlib.pyplot as plt
20+
21+
# Generate matplotlib plot for tests
22+
fig = plt.figure()
23+
24+
x = [10, 20, 30]
25+
y = [100, 200, 300]
26+
plt.plot(x, y, "o")
27+
28+
PLOTLYJS = plotly.offline.offline.get_plotlyjs()
29+
1230

1331
class PlotlyOfflineTestCase(TestCase):
1432
def setUp(self):
@@ -22,3 +40,61 @@ def test_iplot_works_after_you_call_init_notebook_mode(self):
2240
plotly.tools._ipython_imported = True
2341
plotly.offline.init_notebook_mode()
2442
plotly.offline.iplot([{}])
43+
44+
def test_iplot_mpl_works_after_you_call_init_notebook_mode(self):
45+
plotly.tools._ipython_imported = True
46+
plotly.offline.init_notebook_mode()
47+
plotly.offline.iplot_mpl(fig)
48+
49+
50+
class PlotlyOfflineMPLTestCase(TestCase):
51+
def setUp(self):
52+
pass
53+
54+
def _read_html(self, file_url):
55+
""" Read and return the HTML contents from a file_url in the
56+
form e.g. file:///Users/chriddyp/Repos/plotly.py/plotly-temp.html
57+
"""
58+
with open(file_url.replace('file://', '').replace(' ', '')) as f:
59+
return f.read()
60+
61+
def test_default_mpl_plot_generates_expected_html(self):
62+
data_json = ('[{"name": "_line0", "yaxis": "y1", "marker": {"color":' +
63+
' "#0000FF", "opacity": 1, "line": {"color": "#000000",' +
64+
' "width": 0.5}, "symbol": "dot", "size": 6.0}, "mode":' +
65+
' "markers", "xaxis": "x1", "y": [100.0, 200.0, 300.0],' +
66+
' "x": [10.0, 20.0, 30.0], "type": "scatter"}]')
67+
layout_json = ('{"autosize": false, "width": 640, "showlegend": ' +
68+
'false, "xaxis1": {"tickfont": {"size": 12.0}, ' +
69+
'"domain": [0.0, 1.0], "ticks": "inside", "showgrid":' +
70+
' false, "range": [10.0, 30.0], "mirror": "ticks", ' +
71+
'"zeroline": false, "showline": true, "nticks": 5, ' +
72+
'"type": "linear", "anchor": "y1", "side": "bottom"},' +
73+
' "height": 480, "yaxis1": {"tickfont": ' +
74+
'{"size": 12.0}, "domain": [0.0, 1.0], "ticks": ' +
75+
'"inside", "showgrid": false, "range": [100.0, 300.0]' +
76+
', "mirror": "ticks", "zeroline": false, "showline": ' +
77+
'true, "nticks": 5, "type": "linear", "anchor": "x1",' +
78+
' "side": "left"}, "hovermode": "closest", "margin":' +
79+
' {"b": 47, "r": 63, "pad": 0, "t": 47, "l": 80}}')
80+
html = self._read_html(plotly.offline.plot_mpl(fig))
81+
82+
# just make sure a few of the parts are in here
83+
# like PlotlyOfflineTestCase(TestCase) in test_core
84+
self.assertTrue('Plotly.newPlot' in html) # plot command is in there
85+
self.assertTrue(data_json in html) # data is in there
86+
self.assertTrue(layout_json in html) # layout is in there too
87+
self.assertTrue(PLOTLYJS in html) # and the source code
88+
# and it's an <html> doc
89+
self.assertTrue(html.startswith('<html>') and html.endswith('</html>'))
90+
91+
def test_including_plotlyjs(self):
92+
html = self._read_html(plotly.offline.plot_mpl(fig, include_plotlyjs=False))
93+
self.assertTrue(PLOTLYJS not in html)
94+
95+
def test_div_output(self):
96+
html = plotly.offline.plot_mpl(fig, output_type='div')
97+
98+
self.assertTrue('<html>' not in html and '</html>' not in html)
99+
self.assertTrue(html.startswith('<div>') and html.endswith('</div>'))
100+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.