本文整理汇总了Python中matplotlib.cbook.open_file_cm方法的典型用法代码示例。如果您正苦于以下问题:Python cbook.open_file_cm方法的具体用法?Python cbook.open_file_cm怎么用?Python cbook.open_file_cm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.cbook
的用法示例。
在下文中一共展示了cbook.open_file_cm方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_svg
# 需要导入模块: from matplotlib import cbook [as 别名]
# 或者: from matplotlib.cbook import open_file_cm [as 别名]
def print_svg(self, filename, *args, **kwargs):
with cbook.open_file_cm(filename, "w", encoding="utf-8") as fh:
filename = getattr(fh, 'name', '')
if not isinstance(filename, str):
filename = ''
if cbook.file_requires_unicode(fh):
detach = False
else:
fh = io.TextIOWrapper(fh, 'utf-8')
detach = True
result = self._print_svg(filename, fh, **kwargs)
# Detach underlying stream from wrapper so that it remains open in
# the caller.
if detach:
fh.detach()
return result
示例2: print_png
# 需要导入模块: from matplotlib import cbook [as 别名]
# 或者: from matplotlib.cbook import open_file_cm [as 别名]
def print_png(self, filename_or_obj, *args, **kwargs):
FigureCanvasAgg.draw(self)
renderer = self.get_renderer()
original_dpi = renderer.dpi
renderer.dpi = self.figure.dpi
version_str = 'matplotlib version ' + __version__ + \
', http://matplotlib.org/'
metadata = OrderedDict({'Software': version_str})
user_metadata = kwargs.pop("metadata", None)
if user_metadata is not None:
metadata.update(user_metadata)
try:
with cbook.open_file_cm(filename_or_obj, "wb") as fh:
_png.write_png(renderer._renderer, fh,
self.figure.dpi, metadata=metadata)
finally:
renderer.dpi = original_dpi
示例3: print_raw
# 需要导入模块: from matplotlib import cbook [as 别名]
# 或者: from matplotlib.cbook import open_file_cm [as 别名]
def print_raw(self, filename_or_obj, *args, **kwargs):
FigureCanvasAgg.draw(self)
renderer = self.get_renderer()
with cbook._setattr_cm(renderer, dpi=self.figure.dpi), \
cbook.open_file_cm(filename_or_obj, "wb") as fh:
fh.write(renderer._renderer.buffer_rgba())
示例4: print_svgz
# 需要导入模块: from matplotlib import cbook [as 别名]
# 或者: from matplotlib.cbook import open_file_cm [as 别名]
def print_svgz(self, filename, *args, **kwargs):
with cbook.open_file_cm(filename, "wb") as fh, \
gzip.GzipFile(mode='w', fileobj=fh) as gzipwriter:
return self.print_svg(gzipwriter)
示例5: print_pgf
# 需要导入模块: from matplotlib import cbook [as 别名]
# 或者: from matplotlib.cbook import open_file_cm [as 别名]
def print_pgf(self, fname_or_fh, *args, **kwargs):
"""
Output pgf commands for drawing the figure so it can be included and
rendered in latex documents.
"""
if kwargs.get("dryrun", False):
self._print_pgf_to_fh(None, *args, **kwargs)
return
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)
示例6: print_pdf
# 需要导入模块: from matplotlib import cbook [as 别名]
# 或者: from matplotlib.cbook import open_file_cm [as 别名]
def print_pdf(self, fname_or_fh, *args, **kwargs):
"""Use LaTeX to compile a Pgf generated figure to PDF."""
if kwargs.get("dryrun", False):
self._print_pgf_to_fh(None, *args, **kwargs)
return
with cbook.open_file_cm(fname_or_fh, "wb") as file:
self._print_pdf_to_fh(file, *args, **kwargs)
示例7: print_svg
# 需要导入模块: from matplotlib import cbook [as 别名]
# 或者: from matplotlib.cbook import open_file_cm [as 别名]
def print_svg(self, filename, *args, **kwargs):
with cbook.open_file_cm(filename, "w", encoding="utf-8") as fh:
filename = getattr(fh, 'name', '')
if not isinstance(filename, six.string_types):
filename = ''
if cbook.file_requires_unicode(fh):
detach = False
else:
if six.PY3:
fh = io.TextIOWrapper(fh, 'utf-8')
else:
fh = codecs.getwriter('utf-8')(fh)
detach = True
result = self._print_svg(filename, fh, **kwargs)
# Detach underlying stream from wrapper so that it remains open in
# the caller.
if detach:
if six.PY3:
fh.detach()
else:
fh.reset()
fh.stream = io.BytesIO()
return result
示例8: print_png
# 需要导入模块: from matplotlib import cbook [as 别名]
# 或者: from matplotlib.cbook import open_file_cm [as 别名]
def print_png(self, filename_or_obj, *args, **kwargs):
"""
Write the figure to a PNG file.
Parameters
----------
filename_or_obj : str or PathLike or file-like object
The file to write to.
metadata : dict, optional
Metadata in the PNG file as key-value pairs of bytes or latin-1
encodable strings.
According to the PNG specification, keys must be shorter than 79
chars.
The `PNG specification`_ defines some common keywords that may be
used as appropriate:
- Title: Short (one line) title or caption for image.
- Author: Name of image's creator.
- Description: Description of image (possibly long).
- Copyright: Copyright notice.
- Creation Time: Time of original image creation
(usually RFC 1123 format).
- Software: Software used to create the image.
- Disclaimer: Legal disclaimer.
- Warning: Warning of nature of content.
- Source: Device used to create the image.
- Comment: Miscellaneous comment;
conversion from other image format.
Other keywords may be invented for other purposes.
If 'Software' is not given, an autogenerated value for matplotlib
will be used.
For more details see the `PNG specification`_.
.. _PNG specification: \
https://www.w3.org/TR/2003/REC-PNG-20031110/#11keywords
"""
FigureCanvasAgg.draw(self)
renderer = self.get_renderer()
version_str = (
'matplotlib version ' + __version__ + ', http://matplotlib.org/')
metadata = OrderedDict({'Software': version_str})
user_metadata = kwargs.pop("metadata", None)
if user_metadata is not None:
metadata.update(user_metadata)
with cbook._setattr_cm(renderer, dpi=self.figure.dpi), \
cbook.open_file_cm(filename_or_obj, "wb") as fh:
_png.write_png(renderer._renderer, fh,
self.figure.dpi, metadata=metadata)