MCPcopy Index your code
hub / github.com/ipython/ipython / print_figure

Function print_figure

IPython/core/pylabtools.py:129–176  ·  view source on GitHub ↗

Print a figure to an image, and return the resulting file data Returned data will be bytes unless ``fmt='svg'``, in which case it will be unicode. Any keyword args are passed to fig.canvas.print_figure, such as ``quality`` or ``bbox_inches``. If `base64` is True, return base64

(fig, fmt="png", bbox_inches="tight", base64=False, **kwargs)

Source from the content-addressed store, hash-verified

127
128
129def print_figure(fig, fmt="png", bbox_inches="tight", base64=False, **kwargs):
130 """Print a figure to an image, and return the resulting file data
131
132 Returned data will be bytes unless ``fmt='svg'``,
133 in which case it will be unicode.
134
135 Any keyword args are passed to fig.canvas.print_figure,
136 such as ``quality`` or ``bbox_inches``.
137
138 If `base64` is True, return base64-encoded str instead of raw bytes
139 for binary-encoded image formats
140
141 .. versionadded:: 7.29
142 base64 argument
143 """
144 # When there's an empty figure, we shouldn't return anything, otherwise we
145 # get big blank areas in the qt console.
146 if not fig.axes and not fig.lines:
147 return
148
149 dpi = fig.dpi
150 if fmt == 'retina':
151 dpi = dpi * 2
152 fmt = 'png'
153
154 # build keyword args
155 kw = {
156 "format":fmt,
157 "facecolor":fig.get_facecolor(),
158 "edgecolor":fig.get_edgecolor(),
159 "dpi":dpi,
160 "bbox_inches":bbox_inches,
161 }
162 # **kwargs get higher priority
163 kw.update(kwargs)
164
165 bytes_io = BytesIO()
166 if fig.canvas is None:
167 from matplotlib.backend_bases import FigureCanvasBase
168 FigureCanvasBase(fig)
169
170 fig.canvas.print_figure(bytes_io, **kw)
171 data = bytes_io.getvalue()
172 if fmt == 'svg':
173 data = data.decode('utf-8')
174 elif base64:
175 data = b2a_base64(data, newline=False).decode("ascii")
176 return data
177
178def retina_figure(fig, base64=False, **kwargs):
179 """format a figure as a pixel-doubled (retina) PNG

Callers 1

retina_figureFunction · 0.85

Calls 1

updateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…