Skip to content

Commit 8884811

Browse files
committed
support for saving the plots
1 parent 2665307 commit 8884811

File tree

6 files changed

+46
-20
lines changed

6 files changed

+46
-20
lines changed

images/test.bmp

900 KB
Binary file not shown.

images/test.jpg

15.1 KB
Loading

images/test2.jpg

15.1 KB
Loading

pygame_matplotlib/backend_pygame.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import numpy as np
1111
from matplotlib.transforms import Affine2D
1212
import pygame
13+
import pygame.image
1314
from pygame import gfxdraw
1415
from matplotlib._pylab_helpers import Gcf
1516
from 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

298300
class FigureManagerPygame(FigureManagerBase):

test.jpg

15.1 KB
Loading

test_save.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
import os
3+
from numpy.ma.core import size
4+
import pygame
5+
6+
import matplotlib
7+
matplotlib.use('module://pygame_matplotlib.backend_pygame')
8+
#matplotlib.use('Qt4Agg')
9+
10+
import matplotlib.pyplot as plt
11+
import matplotlib.figure as fg
12+
13+
import matplotlib.pyplot as plt
14+
fig = plt.figure()
15+
16+
print(fig.canvas.get_supported_filetypes())
17+
18+
plt.plot([1,2], [1,2], color='green')
19+
plt.text(1.5, 1.5, '2', size=50)
20+
plt.xlabel('swag')
21+
22+
plt.savefig('images' + os.sep + 'test.jpg')
23+
plt.savefig('images' + os.sep + 'test.bmp')
24+
plt.savefig('images' + os.sep + 'test2.jpg')

0 commit comments

Comments
 (0)