Skip to content
This repository

Bad kwargs to savefig #1112

Closed
wants to merge 2 commits into from

2 participants

Phil Elson Benjamin Root
Phil Elson
Collaborator

This is an attempt to resolve #192.
I would like it to get some visibility before the 1.2.x cutoff, but in reality I do not expect it to make it into the freeze.

Phil Elson pelson commented on the diff
lib/matplotlib/figure.py
@@ -1218,10 +1216,10 @@ def savefig(self, *args, **kwargs):
1218 1216
                 patch.set_facecolor('none')
1219 1217
                 patch.set_edgecolor('none')
1220 1218
         else:
1221  
-            kwargs.setdefault('facecolor', rcParams['savefig.facecolor'])
1222  
-            kwargs.setdefault('edgecolor', rcParams['savefig.edgecolor'])
  1219
+            facecolor = facecolor or rcParams['savefig.facecolor']
  1220
+            edgecolor = edgecolor or rcParams['savefig.edgecolor']
1
Phil Elson Collaborator
pelson added a note

BUG: I intended to use the edgecolor & facecolor and then reset them at the savefig level so that the backends don't have to do this. Unfortunately this is half baked at the moment (writing a test for this might be interesting).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Phil Elson pelson commented on the diff
lib/matplotlib/backends/backend_emf.py
((6 lines not shown))
715 715
         width, height = self.figure.get_size_inches()
716  
-        renderer = RendererEMF(filename,width,height,dpi)
  716
+        renderer = RendererEMF(filename, width, height, self.figure.dpi)
1
Phil Elson Collaborator
pelson added a note

This doesn't look equivalent. I need to re-address this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Phil Elson pelson commented on the diff
lib/matplotlib/backends/backend_macosx.py
((10 lines not shown))
300 300
         # original dpi. Pick it up from the renderer.
301  
-        dpi = kwargs['dpi']
302  
-        old_dpi = self.figure.dpi
  301
+        dpi = self.figure.dpi
1
Phil Elson Collaborator
pelson added a note

Again, doesn't appear equivalent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Phil Elson pelson commented on the diff
lib/matplotlib/backends/backend_pdf.py
@@ -2259,20 +2259,20 @@ def draw(self):
2259 2259
     def get_default_filetype(self):
2260 2260
         return 'pdf'
2261 2261
 
2262  
-    def print_pdf(self, filename, **kwargs):
2263  
-        image_dpi = kwargs.get('dpi', 72) # dpi to use for images
  2262
+    def print_pdf(self, filename, bbox_inches_restore=None):
  2263
+        image_dpi = self.figure.dpi       # dpi to use for images
1
Phil Elson Collaborator
pelson added a note

Ah, ok. I wrote these changes a little while back so its like I'm coming to it new!

Again this is half baked, but the intention was to have the savefig manage the figures dpi rather than passing it through, that way, the savefig method can re-set it after calling the backend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Phil Elson
Collaborator

This PR is not for merging at this stage. There are several outstanding issues which would need addressing.
More importantly at this stage is to get some feedback on the way the PR is going. Is it better to be strict about the keywords which can be passed? It will result in more exceptions, but fewer cases where users get unexpected/no behaviour from certain keywords.

Benjamin Root
Collaborator

I definitely support the end-goal here, but I am not entirely sure if it ever would be feasible to achieve. I should have some time today to look over this and comment.

Phil Elson
Collaborator

Personally, I find this PR an improvement, but it does come with some costs (having to explicitly type out all of the keywords that are supported in a function). I would love to get some feedback on this, so that ultimately, I can either close this PR, or update it into a form that we can merge.

Please don't review it as if it were about to be merged - I suspect I will have to start from scratch on this once I have some general feedback and an agreement that this is, in principle, something we want to do.

Cheers,

Phil Elson
Collaborator

I'm not sure this is something we want to move towards at this stage - it improves error messages, but at the cost of freedom to pass through arbitrary keywords. If it were something we wanted to do then it would be a great task for a sprint at a conference etc.

Phil Elson pelson closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Aug 19, 2012
Phil Elson Changes to address #192 d385998
Phil Elson further simplification 79515df
This page is out of date. Refresh to see the latest.
33  lib/matplotlib/backend_bases.py
@@ -1869,7 +1869,7 @@ def print_svgz(self, *args, **kwargs):
1869 1869
 
1870 1870
     if _has_pil:
1871 1871
         filetypes['jpg'] = filetypes['jpeg'] = 'Joint Photographic Experts Group'
1872  
-        def print_jpg(self, filename_or_obj, *args, **kwargs):
  1872
+        def print_jpg(self, filename_or_obj, dryrun=False, bbox_inches_restore=None, **kwargs):
1873 1873
             """
1874 1874
             Supported kwargs:
1875 1875
 
@@ -1888,19 +1888,22 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
1888 1888
             from backends.backend_agg import FigureCanvasAgg # lazy import
1889 1889
             agg = self.switch_backends(FigureCanvasAgg)
1890 1890
             buf, size = agg.print_to_buffer()
1891  
-            if kwargs.pop("dryrun", False): return
  1891
+            if dryrun: return
1892 1892
             image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
1893  
-            options = cbook.restrict_dict(kwargs, ['quality', 'optimize',
1894  
-                                                   'progressive'])
1895  
-            return image.save(filename_or_obj, format='jpeg', **options)
  1893
+            allowed_kwargs = ['quality', 'optimize', 'progressive']
  1894
+            for key in kwargs.keys():
  1895
+                if key not in allowed_kwargs:
  1896
+                    raise TypeError('The keyword %s is not allowed to the jpg '
  1897
+                                    'figure saver.' % key)
  1898
+            return image.save(filename_or_obj, format='jpeg', **kwargs)
1896 1899
         print_jpeg = print_jpg
1897 1900
 
1898 1901
         filetypes['tif'] = filetypes['tiff'] = 'Tagged Image File Format'
1899  
-        def print_tif(self, filename_or_obj, *args, **kwargs):
  1902
+        def print_tif(self, filename_or_obj, dryrun=False, bbox_inches_restore=None):
1900 1903
             from backends.backend_agg import FigureCanvasAgg # lazy import
1901 1904
             agg = self.switch_backends(FigureCanvasAgg)
1902 1905
             buf, size = agg.print_to_buffer()
1903  
-            if kwargs.pop("dryrun", False): return
  1906
+            if dryrun: return
1904 1907
             image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
1905 1908
             return image.save(filename_or_obj, format='tiff')
1906 1909
         print_tiff = print_tif
@@ -1945,7 +1948,7 @@ def _print_method(*args, **kwargs):
1945 1948
         return getattr(self, method_name)
1946 1949
 
1947 1950
     def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
1948  
-                     orientation='portrait', format=None, **kwargs):
  1951
+                     format=None, **kwargs):
1949 1952
         """
1950 1953
         Render the figure to hardcopy. Set the figure patch face and edge
1951 1954
         colors.  This is useful because some of the GUIs have a gray figure
@@ -1957,9 +1960,6 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
1957 1960
         *filename*
1958 1961
             can also be a file object on image backends
1959 1962
 
1960  
-        *orientation*
1961  
-            only currently applies to PostScript printing.
1962  
-
1963 1963
         *dpi*
1964 1964
             the dots per inch to save the figure in; if None, use savefig.dpi
1965 1965
 
@@ -1969,9 +1969,6 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
1969 1969
         *edgecolor*
1970 1970
             the edgecolor of the figure
1971 1971
 
1972  
-        *orientation*
1973  
-            landscape' | 'portrait' (not supported on all backends)
1974  
-
1975 1972
         *format*
1976 1973
             when set, forcibly set the file format to save to
1977 1974
 
@@ -2034,10 +2031,6 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
2034 2031
                 #result = getattr(self, method_name)
2035 2032
                 result = print_method(
2036 2033
                     io.BytesIO(),
2037  
-                    dpi=dpi,
2038  
-                    facecolor=facecolor,
2039  
-                    edgecolor=edgecolor,
2040  
-                    orientation=orientation,
2041 2034
                     dryrun=True,
2042 2035
                     **kwargs)
2043 2036
                 renderer = self.figure._cachedRenderer
@@ -2075,10 +2068,6 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
2075 2068
             #result = getattr(self, method_name)(
2076 2069
             result = print_method(
2077 2070
                 filename,
2078  
-                dpi=dpi,
2079  
-                facecolor=facecolor,
2080  
-                edgecolor=edgecolor,
2081  
-                orientation=orientation,
2082 2071
                 bbox_inches_restore=_bbox_inches_restore,
2083 2072
                 **kwargs)
2084 2073
         finally:
6  lib/matplotlib/backends/backend_agg.py
@@ -434,8 +434,6 @@ def draw(self):
434 434
         finally:
435 435
             RendererAgg.lock.release()
436 436
 
437  
-
438  
-
439 437
     def get_renderer(self):
440 438
         l, b, w, h = self.figure.bbox.bounds
441 439
         key = w, h, self.figure.dpi
@@ -463,7 +461,7 @@ def buffer_rgba(self):
463 461
                                      'debug-annoying')
464 462
         return self.renderer.buffer_rgba()
465 463
 
466  
-    def print_raw(self, filename_or_obj, *args, **kwargs):
  464
+    def print_raw(self, filename_or_obj, bbox_inches_restore=None):
467 465
         FigureCanvasAgg.draw(self)
468 466
         renderer = self.get_renderer()
469 467
         original_dpi = renderer.dpi
@@ -481,7 +479,7 @@ def print_raw(self, filename_or_obj, *args, **kwargs):
481 479
         renderer.dpi = original_dpi
482 480
     print_rgba = print_raw
483 481
 
484  
-    def print_png(self, filename_or_obj, *args, **kwargs):
  482
+    def print_png(self, filename_or_obj, bbox_inches_restore=None):
485 483
         FigureCanvasAgg.draw(self)
486 484
         renderer = self.get_renderer()
487 485
         original_dpi = renderer.dpi
9  lib/matplotlib/backends/backend_cairo.py
@@ -429,10 +429,9 @@ def print_svg(self, fobj, *args, **kwargs):
429 429
     def print_svgz(self, fobj, *args, **kwargs):
430 430
         return self._save(fobj, 'svgz', *args, **kwargs)
431 431
 
432  
-    def _save (self, fo, format, **kwargs):
  432
+    def _save(self, fo, format, orientation='portrait', bbox_inches_restore=None):
433 433
         # save PDF/PS/SVG
434  
-        orientation = kwargs.get('orientation', 'portrait')
435  
-
  434
+        
436 435
         dpi = 72
437 436
         self.figure.dpi = dpi
438 437
         w_in, h_in = self.figure.get_size_inches()
@@ -470,8 +469,8 @@ def _save (self, fo, format, **kwargs):
470 469
                         fo.close()
471 470
             surface = cairo.SVGSurface (fo, width_in_points, height_in_points)
472 471
         else:
473  
-           warnings.warn ("unknown format: %s" % format)
474  
-           return
  472
+            warnings.warn ("unknown format: %s" % format)
  473
+            return
475 474
 
476 475
         # surface.set_dpi() can be used
477 476
         renderer = RendererCairo (self.figure.dpi)
4  lib/matplotlib/backends/backend_emf.py
@@ -711,9 +711,9 @@ def draw(self):
711 711
 
712 712
     filetypes = {'emf': 'Enhanced Metafile'}
713 713
 
714  
-    def print_emf(self, filename, dpi=300, **kwargs):
  714
+    def print_emf(self, filename, bbox_inches_restore=None):
715 715
         width, height = self.figure.get_size_inches()
716  
-        renderer = RendererEMF(filename,width,height,dpi)
  716
+        renderer = RendererEMF(filename, width, height, self.figure.dpi)
717 717
         self.figure.draw(renderer)
718 718
         renderer.save()
719 719
 
8  lib/matplotlib/backends/backend_gdk.py
@@ -448,16 +448,16 @@ def _render_figure(self, pixmap, width, height):
448 448
     filetypes['jpg'] = 'JPEG'
449 449
     filetypes['jpeg'] = 'JPEG'
450 450
 
451  
-    def print_jpeg(self, filename, *args, **kwargs):
  451
+    def print_jpeg(self, filename, bbox_inches_restore=None):
452 452
         return self._print_image(filename, 'jpeg')
453 453
     print_jpg = print_jpeg
454 454
 
455  
-    def print_png(self, filename, *args, **kwargs):
  455
+    def print_png(self, filename, bbox_inches_restore=None):
456 456
         return self._print_image(filename, 'png')
457 457
 
458  
-    def _print_image(self, filename, format, *args, **kwargs):
  458
+    def _print_image(self, filename, format, bbox_inches_restore=None):
459 459
         width, height = self.get_width_height()
460  
-        pixmap = gtk.gdk.Pixmap (None, width, height, depth=24)
  460
+        pixmap = gtk.gdk.Pixmap(None, width, height, depth=24)
461 461
         self._render_figure(pixmap, width, height)
462 462
 
463 463
         # jpg colors don't match the display very well, png colors match
13  lib/matplotlib/backends/backend_macosx.py
@@ -294,18 +294,17 @@ def resize(self, width, height):
294 294
         height /= dpi
295 295
         self.figure.set_size_inches(width, height)
296 296
 
297  
-    def _print_bitmap(self, filename, *args, **kwargs):
298  
-        # In backend_bases.py, print_figure changes the dpi of the figure.
299  
-        # But since we are essentially redrawing the picture, we need the
  297
+    def _print_bitmap(self, filename, bbox_inches_restore=None):
  298
+        # In backend_bases.py, print_figure changes the dpi of the figure,
  299
+        # but since we are essentially redrawing the picture, we need the
300 300
         # original dpi. Pick it up from the renderer.
301  
-        dpi = kwargs['dpi']
302  
-        old_dpi = self.figure.dpi
  301
+        dpi = self.figure.dpi
303 302
         self.figure.dpi = self.renderer.dpi
304 303
         width, height = self.figure.get_size_inches()
305  
-        width, height = width*dpi, height*dpi
  304
+        width, height = width * dpi, height * dpi
306 305
         filename = unicode(filename)
307 306
         self.write_bitmap(filename, width, height)
308  
-        self.figure.dpi = old_dpi
  307
+        self.figure.dpi = dpi
309 308
 
310 309
     def print_bmp(self, filename, *args, **kwargs):
311 310
         self._print_bitmap(filename, *args, **kwargs)
8  lib/matplotlib/backends/backend_pdf.py
@@ -2259,20 +2259,20 @@ def draw(self):
2259 2259
     def get_default_filetype(self):
2260 2260
         return 'pdf'
2261 2261
 
2262  
-    def print_pdf(self, filename, **kwargs):
2263  
-        image_dpi = kwargs.get('dpi', 72) # dpi to use for images
  2262
+    def print_pdf(self, filename, bbox_inches_restore=None):
  2263
+        image_dpi = self.figure.dpi       # dpi to use for images
2264 2264
         self.figure.set_dpi(72)           # there are 72 pdf points to an inch
2265 2265
         width, height = self.figure.get_size_inches()
2266 2266
         if isinstance(filename, PdfPages):
2267 2267
             file = filename._file
2268 2268
         else:
2269 2269
             file = PdfFile(filename)
  2270
+            
2270 2271
         try:
2271 2272
             file.newPage(width, height)
2272  
-            _bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
2273 2273
             renderer = MixedModeRenderer(self.figure,
2274 2274
                                          width, height, image_dpi, RendererPdf(file, image_dpi),
2275  
-                                         bbox_inches_restore=_bbox_inches_restore)
  2275
+                                         bbox_inches_restore=bbox_inches_restore)
2276 2276
             self.figure.draw(renderer)
2277 2277
             renderer.finalize()
2278 2278
         finally:
69  lib/matplotlib/backends/backend_ps.py
@@ -960,48 +960,38 @@ def draw(self):
960 960
     def get_default_filetype(self):
961 961
         return 'ps'
962 962
 
963  
-    def print_ps(self, outfile, *args, **kwargs):
964  
-        return self._print_ps(outfile, 'ps', *args, **kwargs)
  963
+    def print_ps(self, outfile, **kwargs):
  964
+        return self._print_ps(outfile, 'ps', **kwargs)
965 965
 
966  
-    def print_eps(self, outfile, *args, **kwargs):
967  
-        return self._print_ps(outfile, 'eps', *args, **kwargs)
  966
+    def print_eps(self, outfile, **kwargs):
  967
+        return self._print_ps(outfile, 'eps', **kwargs)
968 968
 
969  
-
970  
-
971  
-
972  
-
973  
-
974  
-    def _print_ps(self, outfile, format, *args, **kwargs):
975  
-        papertype = kwargs.pop("papertype", rcParams['ps.papersize'])
976  
-        papertype = papertype.lower()
977  
-        if papertype == 'auto':
978  
-            pass
979  
-        elif papertype not in papersize:
  969
+    def _print_ps(self, outfile, format, orientation='portrait', papertype=None, **kwargs):
  970
+        papertype = (papertype or rcParams['ps.papersize']).lower()
  971
+        
  972
+        if papertype not in papersize:
980 973
             raise RuntimeError( '%s is not a valid papertype. Use one \
981 974
                     of %s'% (papertype, ', '.join( papersize.iterkeys() )) )
982 975
 
983  
-        orientation = kwargs.pop("orientation", "portrait").lower()
  976
+        orientation = orientation.lower()
984 977
         if orientation == 'landscape': isLandscape = True
985 978
         elif orientation == 'portrait': isLandscape = False
986  
-        else: raise RuntimeError('Orientation must be "portrait" or "landscape"')
  979
+        else: raise ValueError('Orientation must be "portrait" or "landscape"')
987 980
 
988  
-        self.figure.set_dpi(72) # Override the dpi kwarg
989  
-        imagedpi = kwargs.pop("dpi", 72)
990  
-        facecolor = kwargs.pop("facecolor", "w")
991  
-        edgecolor = kwargs.pop("edgecolor", "w")
  981
+        imagedpi = self.figure.get_dpi()
992 982
 
993 983
         if rcParams['text.usetex']:
994  
-            self._print_figure_tex(outfile, format, imagedpi, facecolor, edgecolor,
  984
+            self._print_figure_tex(outfile, format, imagedpi, 
995 985
                                    orientation, isLandscape, papertype,
996 986
                                    **kwargs)
997 987
         else:
998  
-            self._print_figure(outfile, format, imagedpi, facecolor, edgecolor,
  988
+            self._print_figure(outfile, format, imagedpi, 
999 989
                                orientation, isLandscape, papertype,
1000 990
                                **kwargs)
1001 991
 
1002  
-    def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
  992
+    def _print_figure(self, outfile, format, dpi=72, 
1003 993
                       orientation='portrait', isLandscape=False, papertype=None,
1004  
-                      **kwargs):
  994
+                      dryrun=False, bbox_inches_restore=None):
1005 995
         """
1006 996
         Render the figure to hardcopy.  Set the figure patch face and
1007 997
         edge colors.  This is useful because some of the GUIs have a
@@ -1061,14 +1051,6 @@ def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
1061 1051
             rotation = 90
1062 1052
         bbox = (llx, lly, urx, ury)
1063 1053
 
1064  
-        # generate PostScript code for the figure and store it in a string
1065  
-        origfacecolor = self.figure.get_facecolor()
1066  
-        origedgecolor = self.figure.get_edgecolor()
1067  
-        self.figure.set_facecolor(facecolor)
1068  
-        self.figure.set_edgecolor(edgecolor)
1069  
-
1070  
-
1071  
-        dryrun = kwargs.get("dryrun", False)
1072 1054
         if dryrun:
1073 1055
             class NullWriter(object):
1074 1056
                 def write(self, *kl, **kwargs):
@@ -1083,21 +1065,17 @@ def write(self, *kl, **kwargs):
1083 1065
 
1084 1066
 
1085 1067
         # mixed mode rendering
1086  
-        _bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
1087 1068
         ps_renderer = self._renderer_class(width, height, self._pswriter,
1088 1069
                                            imagedpi=dpi)
1089 1070
         renderer = MixedModeRenderer(self.figure,
1090 1071
             width, height, dpi, ps_renderer,
1091  
-            bbox_inches_restore=_bbox_inches_restore)
  1072
+            bbox_inches_restore=bbox_inches_restore)
1092 1073
 
1093 1074
         self.figure.draw(renderer)
1094 1075
 
1095 1076
         if dryrun: # return immediately if dryrun (tightbbox=True)
1096 1077
             return
1097 1078
 
1098  
-        self.figure.set_facecolor(origfacecolor)
1099  
-        self.figure.set_edgecolor(origedgecolor)
1100  
-
1101 1079
         fd, tmpfile = mkstemp()
1102 1080
         with io.open(fd, 'wb') as raw_fh:
1103 1081
             if sys.version_info[0] >= 3:
@@ -1150,7 +1128,9 @@ def write(self, *kl, **kwargs):
1150 1128
                         # STIX fonts).  This will simply turn that off to avoid
1151 1129
                         # errors.
1152 1130
                         if is_opentype_cff_font(font_filename):
1153  
-                            raise RuntimeError("OpenType CFF fonts can not be saved using the internal Postscript backend at this time.\nConsider using the Cairo backend.")
  1131
+                            raise RuntimeError("OpenType CFF fonts can not be "
  1132
+                            "saved using the internal Postscript backend at this "
  1133
+                            "time.\nConsider using the Cairo backend.")
1154 1134
                         else:
1155 1135
                             fh.flush()
1156 1136
                             convert_ttf_to_ps(font_filename, raw_fh, fonttype, glyph_ids)
@@ -1188,7 +1168,7 @@ def write(self, *kl, **kwargs):
1188 1168
             shutil.move(tmpfile, outfile)
1189 1169
             os.chmod(outfile, mode)
1190 1170
 
1191  
-    def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
  1171
+    def _print_figure_tex(self, outfile, format, dpi, 
1192 1172
                           orientation, isLandscape, papertype,
1193 1173
                           **kwargs):
1194 1174
         """
@@ -1211,12 +1191,6 @@ def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
1211 1191
         ury = lly + h
1212 1192
         bbox = (llx, lly, urx, ury)
1213 1193
 
1214  
-        # generate PostScript code for the figure and store it in a string
1215  
-        origfacecolor = self.figure.get_facecolor()
1216  
-        origedgecolor = self.figure.get_edgecolor()
1217  
-        self.figure.set_facecolor(facecolor)
1218  
-        self.figure.set_edgecolor(edgecolor)
1219  
-
1220 1194
         dryrun = kwargs.get("dryrun", False)
1221 1195
         if dryrun:
1222 1196
             class NullWriter(object):
@@ -1244,9 +1218,6 @@ def write(self, *kl, **kwargs):
1244 1218
         if dryrun: # return immediately if dryrun (tightbbox=True)
1245 1219
             return
1246 1220
 
1247  
-        self.figure.set_facecolor(origfacecolor)
1248  
-        self.figure.set_edgecolor(origedgecolor)
1249  
-
1250 1221
         # write to a temp file, we'll move it to outfile when done
1251 1222
         fd, tmpfile = mkstemp()
1252 1223
         if sys.version_info[0] >= 3:
13  lib/matplotlib/backends/backend_svg.py
@@ -1086,7 +1086,7 @@ class FigureCanvasSVG(FigureCanvasBase):
1086 1086
     filetypes = {'svg': 'Scalable Vector Graphics',
1087 1087
                  'svgz': 'Scalable Vector Graphics'}
1088 1088
 
1089  
-    def print_svg(self, filename, *args, **kwargs):
  1089
+    def print_svg(self, filename, bbox_inches_restore=None):
1090 1090
         if is_string_like(filename):
1091 1091
             fh_to_close = svgwriter = io.open(filename, 'w', encoding='utf-8')
1092 1092
         elif is_writable_file_like(filename):
@@ -1098,9 +1098,9 @@ def print_svg(self, filename, *args, **kwargs):
1098 1098
             fh_to_close = None
1099 1099
         else:
1100 1100
             raise ValueError("filename must be a path or a file-like object")
1101  
-        return self._print_svg(filename, svgwriter, fh_to_close, **kwargs)
  1101
+        return self._print_svg(filename, svgwriter, fh_to_close, bbox_inches_restore)
1102 1102
 
1103  
-    def print_svgz(self, filename, *args, **kwargs):
  1103
+    def print_svgz(self, filename, bbox_inches_restore=None):
1104 1104
         if is_string_like(filename):
1105 1105
             fh_to_close = gzipwriter = gzip.GzipFile(filename, 'w')
1106 1106
             svgwriter = io.TextIOWrapper(gzipwriter, 'utf-8')
@@ -1109,9 +1109,9 @@ def print_svgz(self, filename, *args, **kwargs):
1109 1109
             svgwriter = io.TextIOWrapper(gzipwriter, 'utf-8')
1110 1110
         else:
1111 1111
             raise ValueError("filename must be a path or a file-like object")
1112  
-        return self._print_svg(filename, svgwriter, fh_to_close)
  1112
+        return self._print_svg(filename, svgwriter, fh_to_close, bbox_inches_restore)
1113 1113
 
1114  
-    def _print_svg(self, filename, svgwriter, fh_to_close=None, **kwargs):
  1114
+    def _print_svg(self, filename, svgwriter, fh_to_close=None, bbox_inches_restore=None):
1115 1115
         try:
1116 1116
             self.figure.set_dpi(72.0)
1117 1117
             width, height = self.figure.get_size_inches()
@@ -1129,10 +1129,9 @@ def _print_svg(self, filename, svgwriter, fh_to_close=None, **kwargs):
1129 1129
 
1130 1130
                 #image_dpi = kwargs.pop("dpi", 72)
1131 1131
                 image_dpi = 72
1132  
-                _bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
1133 1132
                 renderer = MixedModeRenderer(self.figure,
1134 1133
                     width, height, image_dpi, RendererSVG(w, h, svgwriter, filename),
1135  
-                    bbox_inches_restore=_bbox_inches_restore)
  1134
+                    bbox_inches_restore=bbox_inches_restore)
1136 1135
 
1137 1136
             self.figure.draw(renderer)
1138 1137
             renderer.finalize()
5  lib/matplotlib/backends/backend_template.py
@@ -226,11 +226,14 @@ def draw(self):
226 226
     filetypes = FigureCanvasBase.filetypes.copy()
227 227
     filetypes['foo'] = 'My magic Foo format'
228 228
 
229  
-    def print_foo(self, filename, *args, **kwargs):
  229
+    def print_foo(self, filename, bbox_inches_restore=None, **kwargs):
230 230
         """
231 231
         Write out format foo.  The dpi, facecolor and edgecolor are restored
232 232
         to their original values after this call, so you don't need to
233 233
         save and restore them.
  234
+        
  235
+        The kwargs should be specified in order to make error handling of bad
  236
+        keywords consistent across backends.
234 237
         """
235 238
         pass
236 239
 
26  lib/matplotlib/backends/backend_wx.py
@@ -1132,27 +1132,27 @@ def print_figure(self, filename, *args, **kwargs):
1132 1132
         if self._isDrawn:
1133 1133
             self.draw()
1134 1134
 
1135  
-    def print_bmp(self, filename, *args, **kwargs):
1136  
-        return self._print_image(filename, wx.BITMAP_TYPE_BMP, *args, **kwargs)
  1135
+    def print_bmp(self, filename, bbox_inches_restore=None):
  1136
+        return self._print_image(filename, wx.BITMAP_TYPE_BMP)
1137 1137
 
1138  
-    def print_jpeg(self, filename, *args, **kwargs):
1139  
-        return self._print_image(filename, wx.BITMAP_TYPE_JPEG, *args, **kwargs)
  1138
+    def print_jpeg(self, filename, bbox_inches_restore=None):
  1139
+        return self._print_image(filename, wx.BITMAP_TYPE_JPEG)
1140 1140
     print_jpg = print_jpeg
1141 1141
 
1142  
-    def print_pcx(self, filename, *args, **kwargs):
1143  
-        return self._print_image(filename, wx.BITMAP_TYPE_PCX, *args, **kwargs)
  1142
+    def print_pcx(self, filename, bbox_inches_restore=None):
  1143
+        return self._print_image(filename, wx.BITMAP_TYPE_PCX)
1144 1144
 
1145  
-    def print_png(self, filename, *args, **kwargs):
1146  
-        return self._print_image(filename, wx.BITMAP_TYPE_PNG, *args, **kwargs)
  1145
+    def print_png(self, filename, bbox_inches_restore=None):
  1146
+        return self._print_image(filename, wx.BITMAP_TYPE_PNG)
1147 1147
 
1148  
-    def print_tiff(self, filename, *args, **kwargs):
1149  
-        return self._print_image(filename, wx.BITMAP_TYPE_TIF, *args, **kwargs)
  1148
+    def print_tiff(self, filename, bbox_inches_restore=None):
  1149
+        return self._print_image(filename, wx.BITMAP_TYPE_TIF)
1150 1150
     print_tif = print_tiff
1151 1151
 
1152  
-    def print_xpm(self, filename, *args, **kwargs):
1153  
-        return self._print_image(filename, wx.BITMAP_TYPE_XPM, *args, **kwargs)
  1152
+    def print_xpm(self, filename, bbox_inches_restore=None):
  1153
+        return self._print_image(filename, wx.BITMAP_TYPE_XPM)
1154 1154
 
1155  
-    def _print_image(self, filename, filetype, *args, **kwargs):
  1155
+    def _print_image(self, filename, filetype):
1156 1156
         origBitmap   = self.bitmap
1157 1157
 
1158 1158
         l,b,width,height = self.figure.bbox.bounds
22  lib/matplotlib/figure.py
@@ -1131,14 +1131,13 @@ def add_axobserver(self, func):
1131 1131
         'whenever the axes state change, ``func(self)`` will be called'
1132 1132
         self._axobservers.append(func)
1133 1133
 
1134  
-
1135  
-    def savefig(self, *args, **kwargs):
  1134
+    def savefig(self, fname, dpi=None, facecolor=None, edgecolor=None,
  1135
+                      format=None, transparent=False, **kwargs):
1136 1136
         """
1137 1137
         Call signature::
1138 1138
 
1139 1139
           savefig(fname, dpi=None, facecolor='w', edgecolor='w',
1140  
-                  orientation='portrait', papertype=None, format=None,
1141  
-                  transparent=False, bbox_inches=None, pad_inches=0.1)
  1140
+                  format=None, transparent=False, bbox_inches=None, pad_inches=0.1)
1142 1141
 
1143 1142
         Save the current figure.
1144 1143
 
@@ -1203,13 +1202,12 @@ def savefig(self, *args, **kwargs):
1203 1202
             tight bbox is calculated.
1204 1203
 
1205 1204
         """
  1205
+        if dpi is None:
  1206
+            dpi = rcParams['savefig.dpi']
1206 1207
 
1207  
-        kwargs.setdefault('dpi', rcParams['savefig.dpi'])
1208  
-
1209  
-        transparent = kwargs.pop('transparent', False)
1210 1208
         if transparent:
1211  
-            kwargs.setdefault('facecolor', 'none')
1212  
-            kwargs.setdefault('edgecolor', 'none')
  1209
+            if edgecolor is not None or facecolor is not None:
  1210
+                raise ValueError('facecolor and edgecolor have no effect when transparent is True.')
1213 1211
             original_axes_colors = []
1214 1212
             for ax in self.axes:
1215 1213
                 patch = ax.patch
@@ -1218,10 +1216,10 @@ def savefig(self, *args, **kwargs):
1218 1216
                 patch.set_facecolor('none')
1219 1217
                 patch.set_edgecolor('none')
1220 1218
         else:
1221  
-            kwargs.setdefault('facecolor', rcParams['savefig.facecolor'])
1222  
-            kwargs.setdefault('edgecolor', rcParams['savefig.edgecolor'])
  1219
+            facecolor = facecolor or rcParams['savefig.facecolor']
  1220
+            edgecolor = edgecolor or rcParams['savefig.edgecolor']
1223 1221
 
1224  
-        self.canvas.print_figure(*args, **kwargs)
  1222
+        self.canvas.print_figure(fname, dpi=dpi, format=format, **kwargs)
1225 1223
 
1226 1224
         if transparent:
1227 1225
             for ax, cc in zip(self.axes, original_axes_colors):
4  lib/matplotlib/pyplot.py
@@ -467,9 +467,9 @@ def draw():
467 467
     get_current_fig_manager().canvas.draw()
468 468
 
469 469
 @docstring.copy_dedent(Figure.savefig)
470  
-def savefig(*args, **kwargs):
  470
+def savefig(fname, **kwargs):
471 471
     fig = gcf()
472  
-    return fig.savefig(*args, **kwargs)
  472
+    return fig.savefig(fname, **kwargs)
473 473
 
474 474
 @docstring.copy_dedent(Figure.ginput)
475 475
 def ginput(*args, **kwargs):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.