diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index fa5e520b617f..9b7f0906eb4a 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -18,7 +18,6 @@ _Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase, RendererBase) from matplotlib.backends.backend_mixed import MixedModeRenderer -from matplotlib.cbook import is_writable_file_like from matplotlib.path import Path from matplotlib.figure import Figure from matplotlib._pylab_helpers import Gcf @@ -385,8 +384,8 @@ def __init__(self, figure, fh, dummy=False): Matplotlib figure to initialize height, width and dpi from. fh : file-like File handle for the output of the drawing commands. - """ + RendererBase.__init__(self) self.dpi = figure.dpi self.fh = fh @@ -842,16 +841,10 @@ def print_pgf(self, fname_or_fh, *args, **kwargs): if kwargs.get("dryrun", False): self._print_pgf_to_fh(None, *args, **kwargs) return - - # figure out where the pgf is to be written to - if isinstance(fname_or_fh, str): - with open(fname_or_fh, "w", encoding="utf-8") as fh: - self._print_pgf_to_fh(fh, *args, **kwargs) - elif is_writable_file_like(fname_or_fh): - fh = codecs.getwriter("utf-8")(fname_or_fh) - self._print_pgf_to_fh(fh, *args, **kwargs) - else: - raise ValueError("filename must be a path") + with cbook.open_file_cm(fname_or_fh, "w", encoding="utf-8") as file: + if not cbook.file_requires_unicode(file): + file = codecs.getwriter("utf-8")(file) + self._print_pgf_to_fh(file, *args, **kwargs) def _print_pdf_to_fh(self, fh, *args, **kwargs): w, h = self.figure.get_figwidth(), self.figure.get_figheight() @@ -896,21 +889,12 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs): TmpDirCleaner.add(tmpdir) def print_pdf(self, fname_or_fh, *args, **kwargs): - """ - Use LaTeX to compile a Pgf generated figure to PDF. - """ + """Use LaTeX to compile a Pgf generated figure to PDF.""" if kwargs.get("dryrun", False): self._print_pgf_to_fh(None, *args, **kwargs) return - - # figure out where the pdf is to be written to - if isinstance(fname_or_fh, str): - with open(fname_or_fh, "wb") as fh: - self._print_pdf_to_fh(fh, *args, **kwargs) - elif is_writable_file_like(fname_or_fh): - self._print_pdf_to_fh(fname_or_fh, *args, **kwargs) - else: - raise ValueError("filename must be a path or a file-like object") + with cbook.open_file_cm(fname_or_fh, "wb") as file: + self._print_pdf_to_fh(file, *args, **kwargs) def _print_png_to_fh(self, fh, *args, **kwargs): converter = make_pdf_to_png_converter() @@ -933,20 +917,12 @@ def _print_png_to_fh(self, fh, *args, **kwargs): TmpDirCleaner.add(tmpdir) def print_png(self, fname_or_fh, *args, **kwargs): - """ - Use LaTeX to compile a pgf figure to pdf and convert it to png. - """ + """Use LaTeX to compile a pgf figure to pdf and convert it to png.""" if kwargs.get("dryrun", False): self._print_pgf_to_fh(None, *args, **kwargs) return - - if isinstance(fname_or_fh, str): - with open(fname_or_fh, "wb") as fh: - self._print_png_to_fh(fh, *args, **kwargs) - elif is_writable_file_like(fname_or_fh): - self._print_png_to_fh(fname_or_fh, *args, **kwargs) - else: - raise ValueError("filename must be a path or a file-like object") + with cbook.open_file_cm(fname_or_fh, "wb") as file: + self._print_png_to_fh(file, *args, **kwargs) def get_renderer(self): return RendererPgf(self.figure, None, dummy=True) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index e935ceed2bad..258884157b27 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -360,14 +360,14 @@ def is_hashable(obj): def is_writable_file_like(obj): - """return true if *obj* looks like a file object with a *write* method""" + """Return whether *obj* looks like a file object with a *write* method.""" return callable(getattr(obj, 'write', None)) def file_requires_unicode(x): """ - Returns `True` if the given writable file-like object requires Unicode - to be written to it. + Returns whether the given writable file-like object requires Unicode to be + written to it. """ try: x.write(b'') diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 65fa77ea9070..b6491ba3f41f 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -730,9 +730,7 @@ def set_interpolation(self, s): self.stale = True def can_composite(self): - """ - Returns `True` if the image can be composited with its neighbors. - """ + """Return whether the image can be composited with its neighbors.""" trans = self.get_transform() return ( self._interpolation != 'none' and @@ -741,11 +739,12 @@ def can_composite(self): def set_resample(self, v): """ - Set whether or not image resampling is used. + Set whether image resampling is used. Parameters ---------- - v : bool + v : bool or None + If None, use :rc:`image.resample` = True. """ if v is None: v = rcParams['image.resample'] @@ -753,7 +752,7 @@ def set_resample(self, v): self.stale = True def get_resample(self): - """Return the image resample boolean.""" + """Return whether image resampling is used.""" return self._resample def set_filternorm(self, filternorm):