@@ -545,11 +545,26 @@ class image:
545
545
546
546
"""
547
547
@staticmethod
548
- def get (figure_or_data , format = 'png' , width = None , height = None ):
549
- """
550
- Return a static image of the plot described by `figure`.
548
+ def get (figure_or_data , format = 'png' , width = None , height = None , scale = None ):
549
+ """Return a static image of the plot described by `figure_or_data`.
551
550
552
- Valid formats: 'png', 'svg', 'jpeg', 'pdf'
551
+ positional arguments:
552
+ - figure_or_data: The figure dict-like or data list-like object that
553
+ describes a plotly figure.
554
+ Same argument used in `py.plot`, `py.iplot`,
555
+ see https://plot.ly/python for examples
556
+ - format: 'png', 'svg', 'jpeg', 'pdf'
557
+ - width: output width
558
+ - height: output height
559
+ - scale: Increase the resolution of the image by `scale` amount (e.g. `3`)
560
+ Only valid for PNG and JPEG images.
561
+
562
+ example:
563
+ ```
564
+ import plotly.plotly as py
565
+ fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
566
+ py.image.get(fig, 'png', scale=3)
567
+ ```
553
568
554
569
"""
555
570
# TODO: format is a built-in name... we shouldn't really use it
@@ -570,6 +585,13 @@ def get(figure_or_data, format='png', width=None, height=None):
570
585
"supported file types here: "
571
586
"https://plot.ly/python/static-image-export/"
572
587
)
588
+ if scale is not None :
589
+ try :
590
+ scale = float (scale )
591
+ except :
592
+ raise exceptions .PlotlyError (
593
+ "Invalid scale parameter. Scale must be a number."
594
+ )
573
595
574
596
headers = _api_v2 .headers ()
575
597
headers ['plotly_version' ] = version .__version__
@@ -580,7 +602,8 @@ def get(figure_or_data, format='png', width=None, height=None):
580
602
payload ['width' ] = width
581
603
if height is not None :
582
604
payload ['height' ] = height
583
-
605
+ if scale is not None :
606
+ payload ['scale' ] = scale
584
607
url = _api_v2 .api_url ('images/' )
585
608
586
609
res = requests .post (
@@ -614,21 +637,37 @@ def get(figure_or_data, format='png', width=None, height=None):
614
637
"not be translated." )
615
638
raise exceptions .PlotlyError (return_data ['error' ])
616
639
617
-
618
640
@classmethod
619
- def ishow (cls , figure_or_data , format = 'png' , width = None , height = None ):
620
- """
621
- Display a static image of the plot described by `figure `
641
+ def ishow (cls , figure_or_data , format = 'png' , width = None , height = None ,
642
+ scale = None ):
643
+ """ Display a static image of the plot described by `figure_or_data `
622
644
in an IPython Notebook.
623
645
646
+ positional arguments:
647
+ - figure_or_data: The figure dict-like or data list-like object that
648
+ describes a plotly figure.
649
+ Same argument used in `py.plot`, `py.iplot`,
650
+ see https://plot.ly/python for examples
651
+ - format: 'png', 'svg', 'jpeg', 'pdf'
652
+ - width: output width
653
+ - height: output height
654
+ - scale: Increase the resolution of the image by `scale` amount
655
+ Only valid for PNG and JPEG images.
656
+
657
+ example:
658
+ ```
659
+ import plotly.plotly as py
660
+ fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
661
+ py.image.ishow(fig, 'png', scale=3)
624
662
"""
625
663
if format == 'pdf' :
626
- raise exceptions .PlotlyError ("Aw, snap! "
664
+ raise exceptions .PlotlyError (
665
+ "Aw, snap! "
627
666
"It's not currently possible to embed a pdf into "
628
667
"an IPython notebook. You can save the pdf "
629
668
"with the `image.save_as` or you can "
630
669
"embed an png, jpeg, or svg." )
631
- img = cls .get (figure_or_data , format , width , height )
670
+ img = cls .get (figure_or_data , format , width , height , scale )
632
671
from IPython .display import display , Image , SVG
633
672
if format == 'svg' :
634
673
display (SVG (img ))
@@ -637,14 +676,32 @@ def ishow(cls, figure_or_data, format='png', width=None, height=None):
637
676
638
677
@classmethod
639
678
def save_as (cls , figure_or_data , filename , format = None , width = None ,
640
- height = None ):
641
- """
642
- Save a image of the plot described by `figure` locally as `filename`.
679
+ height = None , scale = None ):
680
+ """Save a image of the plot described by `figure_or_data` locally as
681
+ `filename`.
643
682
644
683
Valid image formats are 'png', 'svg', 'jpeg', and 'pdf'.
645
684
The format is taken as the extension of the filename or as the
646
685
supplied format.
647
686
687
+ positional arguments:
688
+ - figure_or_data: The figure dict-like or data list-like object that
689
+ describes a plotly figure.
690
+ Same argument used in `py.plot`, `py.iplot`,
691
+ see https://plot.ly/python for examples
692
+ - filename: The filepath to save the image to
693
+ - format: 'png', 'svg', 'jpeg', 'pdf'
694
+ - width: output width
695
+ - height: output height
696
+ - scale: Increase the resolution of the image by `scale` amount
697
+ Only valid for PNG and JPEG images.
698
+
699
+ example:
700
+ ```
701
+ import plotly.plotly as py
702
+ fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
703
+ py.image.save_as(fig, 'my_image.png', scale=3)
704
+ ```
648
705
"""
649
706
# todo: format shadows built-in name
650
707
(base , ext ) = os .path .splitext (filename )
@@ -653,11 +710,9 @@ def save_as(cls, figure_or_data, filename, format=None, width=None,
653
710
elif ext and not format :
654
711
format = ext [1 :]
655
712
elif not ext and format :
656
- filename += '.' + format
657
- else :
658
- filename += '.' + format
713
+ filename += '.' + format
659
714
660
- img = cls .get (figure_or_data , format , width , height )
715
+ img = cls .get (figure_or_data , format , width , height , scale )
661
716
662
717
f = open (filename , 'wb' )
663
718
f .write (img )
0 commit comments