1010import numpy as np
1111from matplotlib .transforms import Affine2D
1212import pygame
13+ import pygame .image
1314from pygame import gfxdraw
1415from matplotlib ._pylab_helpers import Gcf
1516from matplotlib .backend_bases import (
@@ -75,7 +76,6 @@ def draw_path(self, gc, path, transform, rgbFace=None):
7576 previous_point = end_point
7677 poly_points .append (end_point )
7778 elif code == Path .CLOSEPOLY :
78- print ('close' , poly_points , point )
7979 if len (poly_points ) > 2 :
8080 gfxdraw .filled_polygon (
8181 self .surface ,
@@ -255,9 +255,25 @@ class methods button_press_event, button_release_event,
255255 figure : `matplotlib.figure.Figure`
256256 A high-level Figure instance
257257 """
258+ # File types allowed for saving
259+ filetypes = {
260+ 'jpeg' : 'Joint Photographic Experts Group' ,
261+ 'jpg' : 'Joint Photographic Experts Group' ,
262+ 'png' : 'Portable Network Graphics' ,
263+ 'bmp' : 'Bitmap Image File' ,
264+ 'tga' : 'Truevision Graphics Adapter' ,
265+ }
258266
259267 def __init__ (self , figure = None ):
260- FigureCanvasBase .__init__ (self , figure )
268+ super ().__init__ (figure )
269+
270+ # You should provide a print_xxx function for every file format
271+ # you can write.
272+ for file_extension in self .filetypes .keys ():
273+ setattr (
274+ self , 'print_{}' .format (file_extension ),
275+ self ._print_any
276+ )
261277
262278 def draw (self ):
263279 """
@@ -272,27 +288,13 @@ def draw(self):
272288 renderer .surface = self .figure
273289 self .figure .draw (renderer )
274290
275- # You should provide a print_xxx function for every file format
276- # you can write.
277-
278- # If the file type is not in the base set of filetypes,
279- # you should add it to the class-scope filetypes dictionary as follows:
280- filetypes = {** FigureCanvasBase .filetypes , 'foo' : 'My magic Foo format' }
281-
282- def print_foo (self , filename , * args , ** kwargs ):
283- """
284- Write out format foo.
285-
286- This method is normally called via `.Figure.savefig` and
287- `.FigureCanvasBase.print_figure`, which take care of setting the figure
288- facecolor, edgecolor, and dpi to the desired output values, and will
289- restore them to the original values. Therefore, `print_foo` does not
290- need to handle these settings.
291- """
291+ def _print_any (self , filename , * args , ** kwargs ):
292+ """Call the pygame image saving method for the correct extenstion."""
292293 self .draw ()
294+ pygame .image .save (self .figure , filename )
293295
294296 def get_default_filetype (self ):
295- return 'foo '
297+ return 'jpg '
296298
297299
298300class FigureManagerPygame (FigureManagerBase ):
0 commit comments