Skip to content

Commit 2e66361

Browse files
committed
Make backend_pgf more flexible when saving to file-handles or streams.
1 parent c91589c commit 2e66361

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/matplotlib/backends/backend_pgf.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import codecs
1313
import atexit
1414
import weakref
15+
import warnings
1516

1617
import matplotlib as mpl
1718
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
@@ -411,12 +412,16 @@ def __init__(self, figure, fh, dummy=False):
411412
# get LatexManager instance
412413
self.latexManager = LatexManagerFactory.get_latex_manager()
413414

414-
# dummy==True deactivate all methods
415415
if dummy:
416+
# dummy==True deactivate all methods
416417
nop = lambda *args, **kwargs: None
417418
for m in RendererPgf.__dict__.keys():
418419
if m.startswith("draw_"):
419420
self.__dict__[m] = nop
421+
else:
422+
# if fh does not belong to a filename, deactivate draw_image
423+
if not os.path.exists(fh.name):
424+
self.__dict__["draw_image"] = lambda *args, **kwargs: None
420425

421426
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
422427
writeln(self.fh, r"\begin{pgfscope}")
@@ -820,8 +825,11 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
820825
with codecs.open(fname_or_fh, "w", encoding="utf-8") as fh:
821826
self._print_pgf_to_fh(fh, *args, **kwargs)
822827
elif is_writable_file_like(fname_or_fh):
823-
raise ValueError("saving pgf to a stream is not supported, " +
824-
"consider using the pdf option of the pgf-backend")
828+
if not os.path.exists(fname_or_fh.name):
829+
warnings.warn("streamed pgf-code does not support raster "
830+
"graphics, consider using the pgf-to-pdf option",
831+
UserWarning)
832+
self._print_pgf_to_fh(fname_or_fh, *args, **kwargs)
825833
else:
826834
raise ValueError("filename must be a path")
827835

0 commit comments

Comments
 (0)