Skip to content

Commit dbbad22

Browse files
committed
merged image and format into one parameter: image
1 parent f5baa3e commit dbbad22

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

plotly/offline/offline.py

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,9 @@ def _plot_html(figure_or_data, show_link, link_text, validate,
185185

186186
return plotly_html_div, plotdivid, width, height
187187

188-
189188
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
190-
validate=True, image=False, format='png',
191-
_width=800, _height=600, filename='plot'):
189+
validate=True, image=None, filename='plot_image', image_width=800,
190+
image_height=600):
192191
"""
193192
Draw plotly graphs inside an IPython notebook without
194193
connecting to an external server.
@@ -211,22 +210,23 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
211210
has become outdated with your version of
212211
graph_reference.json or if you need to include
213212
extra, unnecessary keys in your figure.
214-
image (default=False) -- If True, the image of the current plot
215-
will be downloaded.
216-
format (default='png') -- Specifies the format of the image to be
217-
downloaded if `downlowad_image` is True.
213+
image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
214+
the format of the image to be downloaded, if we choose to download an
215+
image. This parameter has a default value of None indicating that no
216+
image should be downloaded.
218217
filename (default='plot') -- Sets the name of the file your image
219218
will be saved to. The extension should not be included.
220-
_height (default=600) -- Specifies the height of the image in `px`.
221-
_width (default=800) -- Specifies the width of the image in `px`.
219+
image_height (default=600) -- Specifies the height of the image in `px`.
220+
image_width (default=800) -- Specifies the width of the image in `px`.
222221
223222
Example:
224223
```
225224
from plotly.offline import init_notebook_mode, iplot
226225
init_notebook_mode()
227226
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
228-
# We can also download an image of the plot by setting `image=True`
229-
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image=True)
227+
# We can also download an image of the plot by setting the image to the
228+
format you want ie: `image='png'`
229+
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png')
230230
```
231231
"""
232232
if not __PLOTLY_OFFLINE_INITIALIZED:
@@ -264,11 +264,12 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
264264
'downloadimage(\'{format}\', {height}, {width}, '
265265
'\'{filename}\');}}'
266266
'</script>'
267-
).format(format=format, width=_width, height=_height,
268-
filename=filename, plot_id=plotdivid)
267+
).format(format=format, width=image_width,
268+
height=image_height, filename=filename,
269+
plot_id=plotdivid)
269270
# allow time for the plot to draw
270271
import time
271-
time.sleep(1)
272+
time.sleep(0.1)
272273
# inject code to download an image of the plot
273274
display(HTML(script))
274275

@@ -278,8 +279,8 @@ def plot(figure_or_data,
278279
validate=True, output_type='file',
279280
include_plotlyjs=True,
280281
filename='temp-plot.html', auto_open=True,
281-
image=False, image_filename='plot_image', format='png',
282-
_width=800, _height=600):
282+
image=None, image_filename='plot_image',
283+
image_width=800, image_height=600):
283284
""" Create a plotly graph locally as an HTML document or string.
284285
285286
Example:
@@ -288,9 +289,10 @@ def plot(figure_or_data,
288289
import plotly.graph_objs as go
289290
290291
plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html')
291-
# We can also download an image of the plot by setting `image=True`
292+
# We can also download an image of the plot by setting the image parameter
293+
# to the image format we want
292294
plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html'
293-
image=True)
295+
image='jpeg')
294296
```
295297
More examples below.
296298
@@ -328,14 +330,14 @@ def plot(figure_or_data,
328330
auto_open (default=True) -- If True, open the saved file in a
329331
web browser after saving.
330332
This argument only applies if `output_type` is 'file'.
331-
image (default=False) -- If True, the image of the current plot
332-
will be downloaded.
333-
format (default='png') -- Specifies the format of the image to be
334-
downloaded if `downlowad_image` is True.
333+
image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
334+
the format of the image to be downloaded, if we choose to download an
335+
image. This parameter has a default value of None indicating that no
336+
image should be downloaded.
335337
image_filename (default='plot_image') -- Sets the name of the file your image
336338
will be saved to. The extension should not be included.
337-
_height (default=600) -- Specifies the height of the image in `px`.
338-
_width (default=800) -- Specifies the width of the image in `px`.
339+
image_height (default=600) -- Specifies the height of the image in `px`.
340+
image_width (default=800) -- Specifies the width of the image in `px`.
339341
"""
340342
if output_type not in ['div', 'file']:
341343
raise ValueError(
@@ -392,7 +394,8 @@ def plot(figure_or_data,
392394
'downloadimage(\'{format}\', {height}, {width}, '
393395
'\'{filename}\');}}'
394396
'</script>'
395-
).format(format=format, width=_width, height=_height,
397+
).format(format=format, width=image_width,
398+
height=image_height,
396399
filename=image_filename, plot_id=plotdivid)
397400
else:
398401
script = ''
@@ -432,8 +435,8 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False,
432435
verbose=False, show_link=True, link_text='Export to plot.ly',
433436
validate=True, output_type='file', include_plotlyjs=True,
434437
filename='temp-plot.html', auto_open=True,
435-
image=False, image_filename='plot_image', format='png',
436-
_height=600, _width=800):
438+
image=None, image_filename='plot_image',
439+
image_height=600, image_width=800):
437440
"""
438441
Convert a matplotlib figure to a Plotly graph stored locally as HTML.
439442
@@ -477,14 +480,16 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False,
477480
auto_open (default=True) -- If True, open the saved file in a
478481
web browser after saving.
479482
This argument only applies if `output_type` is 'file'.
480-
image (default=False) -- If True, the image of the current plot
481-
will be downloaded.
483+
image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
484+
the format of the image to be downloaded, if we choose to download an
485+
image. This parameter has a default value of None indicating that no
486+
image should be downloaded.
482487
format (default='png') -- Specifies the format of the image to be
483488
downloaded if `downlowad_image` is True.
484489
image_filename (default='plot_image') -- Sets the name of the file your
485490
image will be saved to. The extension should not be included.
486-
_height (default=600) -- Specifies the height of the image in `px`.
487-
_width (default=800) -- Specifies the width of the image in `px`.
491+
image_height (default=600) -- Specifies the height of the image in `px`.
492+
image_width (default=800) -- Specifies the width of the image in `px`.
488493
489494
Example:
490495
```
@@ -500,21 +505,22 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False,
500505
501506
plot_mpl(fig)
502507
# If you want to to download an image of the figure as well
503-
plot_mpl(fig, image=True)
508+
plot_mpl(fig, image='png')
504509
```
505510
"""
506511
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
507512
return plot(plotly_plot, show_link, link_text, validate, output_type,
508513
include_plotlyjs, filename, auto_open,
509514
image=image, image_filename=image_filename,
510-
format=format, _height=_height, _width=_width)
515+
format=format, image_height=image_height,
516+
image_width=image_width)
511517

512518

513519
def iplot_mpl(mpl_fig, resize=False, strip_style=False,
514520
verbose=False, show_link=True,
515521
link_text='Export to plot.ly', validate=True,
516-
image=False, image_filename='plot_image', _format='png',
517-
_height=600, _width=800):
522+
image=None, image_filename='plot_image',
523+
image_height=600, image_width=800):
518524
"""
519525
Convert a matplotlib figure to a plotly graph and plot inside an IPython
520526
notebook without connecting to an external server.
@@ -543,14 +549,14 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
543549
has become outdated with your version of
544550
graph_reference.json or if you need to include
545551
extra, unnecessary keys in your figure.
546-
image (default=False) -- If True, the image of the current plot will be
547-
downloaded.
548-
_format (default='png') -- Specifies the format of the image to be
549-
downloaded if `downlowad_image` is True.
552+
image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
553+
the format of the image to be downloaded, if we choose to download an
554+
image. This parameter has a default value of None indicating that no
555+
image should be downloaded.
550556
image_filename (default='plot_image') -- Sets the name of the file your
551557
image will be saved to. The extension should not be included.
552-
_height (default=600) -- Specifies the height of the image in `px`.
553-
_width (default=800) -- Specifies the width of the image in `px`.
558+
image_height (default=600) -- Specifies the height of the image in `px`.
559+
image_width (default=800) -- Specifies the width of the image in `px`.
554560
555561
Example:
556562
```
@@ -565,13 +571,13 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
565571
init_notebook_mode()
566572
iplot_mpl(fig)
567573
# and if you want to download an image of the figure as well
568-
iplot_mpl(fig, image=True)
574+
iplot_mpl(fig, image='jpeg')
569575
```
570576
"""
571577
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
572578
return iplot(plotly_plot, show_link, link_text, validate,
573579
image=image, filename=image_filename,
574-
format=_format, _height=_height, _width=_width)
580+
image_height=image_height, image_width=image_width)
575581

576582

577583
def enable_mpl_offline(resize=False, strip_style=False,

0 commit comments

Comments
 (0)