本文整理匯總了Python中matplotlib.backends.backend_agg.FigureCanvasAgg方法的典型用法代碼示例。如果您正苦於以下問題:Python backend_agg.FigureCanvasAgg方法的具體用法?Python backend_agg.FigureCanvasAgg怎麽用?Python backend_agg.FigureCanvasAgg使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類matplotlib.backends.backend_agg
的用法示例。
在下文中一共展示了backend_agg.FigureCanvasAgg方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: FigureToSummary
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def FigureToSummary(name, fig):
"""Create tf.Summary proto from matplotlib.figure.Figure.
Args:
name: Summary name.
fig: A matplotlib figure object.
Returns:
A `tf.Summary` proto containing the figure rendered to an image.
"""
canvas = backend_agg.FigureCanvasAgg(fig)
fig.canvas.draw()
ncols, nrows = fig.canvas.get_width_height()
png_file = six.BytesIO()
canvas.print_figure(png_file)
png_str = png_file.getvalue()
return tf.Summary(value=[
tf.Summary.Value(
tag='%s/image' % name,
image=tf.Summary.Image(
height=nrows,
width=ncols,
colorspace=3,
encoded_image_string=png_str))
])
示例2: get_renderer
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def get_renderer(fig):
if fig._cachedRenderer:
renderer = fig._cachedRenderer
else:
canvas = fig.canvas
if canvas and hasattr(canvas, "get_renderer"):
renderer = canvas.get_renderer()
else:
# not sure if this can happen
warnings.warn("tight_layout : falling back to Agg renderer")
from matplotlib.backends.backend_agg import FigureCanvasAgg
canvas = FigureCanvasAgg(fig)
renderer = canvas.get_renderer()
return renderer
示例3: __init__
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def __init__(self, *args, **kwargs):
backend_agg.FigureCanvasAgg.__init__(self, *args, **kwargs)
# A buffer to hold the PNG data for the last frame. This is
# retained so it can be resent to each client without
# regenerating it.
self._png_buffer = io.BytesIO()
# Set to True when the renderer contains data that is newer
# than the PNG buffer.
self._png_is_old = True
# Set to True by the `refresh` message so that the next frame
# sent to the clients will be a full frame.
self._force_full = True
# Set to True when a drawing is in progress to prevent redraw
# messages from piling up.
self._pending_draw = None
示例4: get_renderer
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def get_renderer(fig):
if fig._cachedRenderer:
renderer = fig._cachedRenderer
else:
canvas = fig.canvas
if canvas and hasattr(canvas, "get_renderer"):
renderer = canvas.get_renderer()
else:
# not sure if this can happen
cbook._warn_external("tight_layout : falling back to Agg renderer")
from matplotlib.backends.backend_agg import FigureCanvasAgg
canvas = FigureCanvasAgg(fig)
renderer = canvas.get_renderer()
return renderer
示例5: __init__
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def __init__(self, *args, **kwargs):
backend_agg.FigureCanvasAgg.__init__(self, *args, **kwargs)
# Set to True when the renderer contains data that is newer
# than the PNG buffer.
self._png_is_old = True
# Set to True by the `refresh` message so that the next frame
# sent to the clients will be a full frame.
self._force_full = True
# Store the current image mode so that at any point, clients can
# request the information. This should be changed by calling
# self.set_image_mode(mode) so that the notification can be given
# to the connected clients.
self._current_image_mode = 'full'
# Store the DPI ratio of the browser. This is the scaling that
# occurs automatically for all images on a HiDPI display.
self._dpi_ratio = 1
示例6: run
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def run(self, fig):
"""
Run the exporter on the given figure
Parmeters
---------
fig : matplotlib.Figure instance
The figure to export
"""
# Calling savefig executes the draw() command, putting elements
# in the correct place.
if fig.canvas is None:
canvas = FigureCanvasAgg(fig)
fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi)
if self.close_mpl:
import matplotlib.pyplot as plt
plt.close(fig)
self.crawl_fig(fig)
示例7: __init__
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def __init__(self, *args, **kwargs):
backend_agg.FigureCanvasAgg.__init__(self, *args, **kwargs)
# A buffer to hold the PNG data for the last frame. This is
# retained so it can be resent to each client without
# regenerating it.
self._png_buffer = io.BytesIO()
# Set to True when the renderer contains data that is newer
# than the PNG buffer.
self._png_is_old = True
# Set to True by the `refresh` message so that the next frame
# sent to the clients will be a full frame.
self._force_full = True
# Store the current image mode so that at any point, clients can
# request the information. This should be changed by calling
# self.set_image_mode(mode) so that the notification can be given
# to the connected clients.
self._current_image_mode = 'full'
示例8: get_renderer
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def get_renderer(fig):
if fig._cachedRenderer:
renderer = fig._cachedRenderer
else:
canvas = fig.canvas
if canvas and hasattr(canvas, "get_renderer"):
renderer = canvas.get_renderer()
else:
# not sure if this can happen
# seems to with PDF...
_log.info("constrained_layout : falling back to Agg renderer")
from matplotlib.backends.backend_agg import FigureCanvasAgg
canvas = FigureCanvasAgg(fig)
renderer = canvas.get_renderer()
return renderer
示例9: plot_buf
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def plot_buf(y):
def _plot_buf(y):
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import io
fig = Figure(figsize=(3, 3))
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot(y)
ax.grid(axis='y')
fig.tight_layout(pad=0)
buf = io.BytesIO()
fig.savefig(buf, format='png')
buf.seek(0)
return buf.getvalue()
s = tf.py_func(_plot_buf, [y], tf.string)
return s
示例10: plot_generated_images
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def plot_generated_images(images, fname):
"""Save a synthetic image as a PNG file.
Args:
images: samples of synthetic images generated by the generative network.
fname: Python `str`, filename to save the plot to.
"""
fig = figure.Figure(figsize=(4, 4))
canvas = backend_agg.FigureCanvasAgg(fig)
for i, image in enumerate(images):
ax = fig.add_subplot(4, 4, i + 1)
ax.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.imshow(image.reshape(IMAGE_SHAPE[:-1]), cmap='Greys_r')
fig.tight_layout()
fig.subplots_adjust(wspace=0.05, hspace=0.05)
canvas.print_figure(fname, format='png')
示例11: save_imgs
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def save_imgs(x, fname):
"""Helper method to save a grid of images to a PNG file.
Args:
x: A numpy array of shape [n_images, height, width].
fname: The filename to write to (including extension).
"""
n = x.shape[0]
fig = figure.Figure(figsize=(n, 1), frameon=False)
canvas = backend_agg.FigureCanvasAgg(fig)
for i in range(n):
ax = fig.add_subplot(1, n, i+1)
ax.imshow(x[i].squeeze(),
interpolation="none",
cmap=cm.get_cmap("binary"))
ax.axis("off")
canvas.print_figure(fname, format="png")
print("saved %s" % fname)
示例12: plot_dsc
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def plot_dsc(dsc_dist):
y_positions = np.arange(len(dsc_dist))
dsc_dist = sorted(dsc_dist.items(), key=lambda x: x[1])
values = [x[1] for x in dsc_dist]
labels = [x[0] for x in dsc_dist]
labels = ["_".join(l.split("_")[1:-1]) for l in labels]
fig = plt.figure(figsize=(12, 8))
canvas = FigureCanvasAgg(fig)
plt.barh(y_positions, values, align="center", color="skyblue")
plt.yticks(y_positions, labels)
plt.xticks(np.arange(0.0, 1.0, 0.1))
plt.xlim([0.0, 1.0])
plt.gca().axvline(np.mean(values), color="tomato", linewidth=2)
plt.gca().axvline(np.median(values), color="forestgreen", linewidth=2)
plt.xlabel("Dice coefficient", fontsize="x-large")
plt.gca().xaxis.grid(color="silver", alpha=0.5, linestyle="--", linewidth=1)
plt.tight_layout()
canvas.draw()
plt.close()
s, (width, height) = canvas.print_to_buffer()
return np.fromstring(s, np.uint8).reshape((height, width, 4))
示例13: redraw
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def redraw(self):
self.figure = spc(data=self.data) + rules()
for chart in self.layer:
self.figure + chart()
self.figure.make(figsize=(8, 6))
canv = FigureCanvasAgg(self.figure.fig)
buf = BytesIO()
canv.print_figure(buf, format='png')
with self._buflock:
if self._buf is not None:
self._buf.close()
self._buf = buf
i = int(time.time() * 1e6)
self.attributes['src'] = "/%s/get_image_data?update_index=%d" % (id(self), i)
super(MatplotImage, self).redraw()
示例14: create_visualization_context
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def create_visualization_context(self, image_bgr: Image):
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
context = {}
context["image_bgr"] = image_bgr
dpi = 100
height_inches = float(image_bgr.shape[0]) / dpi
width_inches = float(image_bgr.shape[1]) / dpi
fig = plt.figure(figsize=(width_inches, height_inches), dpi=dpi)
plt.axes([0, 0, 1, 1])
plt.axis("off")
context["fig"] = fig
canvas = FigureCanvas(fig)
context["canvas"] = canvas
extent = (0, image_bgr.shape[1], image_bgr.shape[0], 0)
plt.imshow(image_bgr[:, :, ::-1], extent=extent)
return context
示例15: figure_to_image
# 需要導入模塊: from matplotlib.backends import backend_agg [as 別名]
# 或者: from matplotlib.backends.backend_agg import FigureCanvasAgg [as 別名]
def figure_to_image(figure, close=True):
"""Render matplotlib figure to numpy format.
Returns:
numpy.array: image in [CHW] order
"""
if not np:
logger.warning(NUMPY_ERROR_MESSAGE)
try:
import matplotlib.pyplot as plt
import matplotlib.backends.backend_agg as plt_backend_agg
except ImportError:
logger.warning(MATPLOTLIB_ERROR_MESSAGE)
canvas = plt_backend_agg.FigureCanvasAgg(figure)
canvas.draw()
data = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
w, h = figure.canvas.get_width_height()
image_hwc = data.reshape([h, w, 4])[:, :, 0:3]
image_chw = np.moveaxis(image_hwc, source=2, destination=0)
if close:
plt.close(figure)
return image_chw