matplotlib 是什么
# pip install matplotlib==2.2.2
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
FigureCanvasKivyAgg
class FigureCanvasKivyAgg(FigureCanvasKivy, FigureCanvasAgg):
'''FigureCanvasKivyAgg class. See module documentation for more
information.
'''
def __init__(self, figure, **kwargs):
self.figure = figure
self.bind(size=self._on_size_changed)
super(FigureCanvasKivyAgg, self).__init__(figure=self.figure, **kwargs)
self.img_texture = None
self.img_rect = None
self.blit()
def draw(self):
'''
Draw the figure using the agg renderer
'''
self.canvas.clear()
FigureCanvasAgg.draw(self)
if self.blitbox is None:
l, b, w, h = self.figure.bbox.bounds
w, h = int(w), int(h)
buf_rgba = self.get_renderer().buffer_rgba()
else:
bbox = self.blitbox
l, b, r, t = bbox.extents
w = int(r) - int(l)
h = int(t) - int(b)
t = int(b) + h
reg = self.copy_from_bbox(bbox)
buf_rgba = reg.to_string()
texture = Texture.create(size=(w, h))
texture.flip_vertical()
color = self.figure.get_facecolor()
with self.canvas:
Color(*color)
Rectangle(pos=self.pos, size=(w, h))
Color(1.0, 1.0, 1.0, 1.0)
self.img_rect = Rectangle(texture=texture, pos=self.pos,
size=(w, h))
texture.blit_buffer(bytes(buf_rgba), colorfmt='rgba', bufferfmt='ubyte')
self.img_texture = texture
filetypes = FigureCanvasKivy.filetypes.copy()
filetypes['png'] = 'Portable Network Graphics'
def _on_pos_changed(self, *args):
if self.img_rect is not None:
self.img_rect.pos = self.pos
def _print_image(self, filename, *args, **kwargs):
'''Write out format png. The image is saved with the filename given.
'''
l, b, w, h = self.figure.bbox.bounds
img = None
if self.img_texture is None:
texture = Texture.create(size=(w, h))
texture.blit_buffer(bytes(self.get_renderer().buffer_rgba()),
colorfmt='rgba', bufferfmt='ubyte')
texture.flip_vertical()
img = Image(texture)
else:
img = Image(self.img_texture)
img.save(filename)