Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 174b126

Browse files
committed
Merge pull request #3982 from pwuertz/fix_3982
pgf can not write to `BytesIO` [back port to 1.4.x]
2 parents 580d990 + 9fcd026 commit 174b126

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,10 @@ def __init__(self, figure, fh, dummy=False):
431431
self.__dict__[m] = nop
432432
else:
433433
# if fh does not belong to a filename, deactivate draw_image
434-
if not os.path.exists(fh.name):
434+
if not hasattr(fh, 'name') or not os.path.exists(fh.name):
435+
warnings.warn("streamed pgf-code does not support raster "
436+
"graphics, consider using the pgf-to-pdf option",
437+
UserWarning)
435438
self.__dict__["draw_image"] = lambda *args, **kwargs: None
436439

437440
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
@@ -835,11 +838,8 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
835838
with codecs.open(fname_or_fh, "w", encoding="utf-8") as fh:
836839
self._print_pgf_to_fh(fh, *args, **kwargs)
837840
elif is_writable_file_like(fname_or_fh):
838-
if not os.path.exists(fname_or_fh.name):
839-
warnings.warn("streamed pgf-code does not support raster "
840-
"graphics, consider using the pgf-to-pdf option",
841-
UserWarning)
842-
self._print_pgf_to_fh(fname_or_fh, *args, **kwargs)
841+
fh = codecs.getwriter("utf-8")(fname_or_fh)
842+
self._print_pgf_to_fh(fh, *args, **kwargs)
843843
else:
844844
raise ValueError("filename must be a path")
845845

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,7 @@ def get_javascript(cls, stream=None):
403403
for filetype, ext in sorted(FigureCanvasWebAggCore.
404404
get_supported_filetypes_grouped().
405405
items()):
406-
if not ext[0] == 'pgf': # pgf does not support BytesIO
407-
extensions.append(ext[0])
406+
extensions.append(ext[0])
408407
output.write("mpl.extensions = {0};\n\n".format(
409408
json.dumps(extensions)))
410409

0 commit comments

Comments
 (0)