Skip to content

Commit f79ef0d

Browse files
committed
added image capability
1 parent 802a87f commit f79ef0d

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

images/lenna.jpg

38.7 KB
Loading

images/long_dog.jpg

10.1 KB
Loading

images/qt_agg.png

35.7 KB
Loading

pygame_matplotlib/backend_pygame.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
...
2929
plt.savefig("figure.xyz")
3030
"""
31+
import numpy as np
3132
from matplotlib.transforms import Affine2D
3233
import pygame
3334
from matplotlib._pylab_helpers import Gcf
@@ -143,7 +144,10 @@ def draw_path(self, gc, path, transform, rgbFace=None):
143144
# pass
144145

145146
def draw_image(self, gc, x, y, im):
146-
pass
147+
print(type(im))
148+
print(x, y)
149+
img_surf = pygame.image.frombuffer(np.ascontiguousarray(np.flip(im, axis=0)), (im.shape[1], im.shape[0]), 'RGBA')
150+
self.surface.blit(img_surf, (x, self.surface.get_height() - y - im.shape[0]))
147151

148152
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
149153
# make sure font module is initialized
@@ -167,7 +171,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
167171

168172
def flipy(self):
169173
# docstring inherited
170-
return True
174+
return False
171175

172176
def get_canvas_width_height(self):
173177
# docstring inherited
@@ -327,7 +331,10 @@ class FigureManagerTemplate(FigureManagerBase):
327331
def show(self):
328332
# do something to display the GUI
329333
pygame.init()
330-
main_display = pygame.display.set_mode(self.canvas.figure.get_size())
334+
main_display = pygame.display.set_mode(
335+
self.canvas.figure.get_size(), # Size matches figure size
336+
pygame.RESIZABLE # Allow resizing
337+
)
331338

332339
FPS = 144
333340
FramePerSec = pygame.time.Clock()

test_plt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
14+
plt.plot([1,2], [1,2], color='green')
15+
plt.text(1.5, 1.5, '2', size=50)
16+
plt.xlabel('swag')
17+
18+
19+
plt.show()

test_show.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55

66
import matplotlib
77
matplotlib.use('module://pygame_matplotlib.backend_pygame')
8-
#matplotlib.use('Qt4Agg')
8+
# matplotlib.use('Qt4Agg')
99

1010
import matplotlib.pyplot as plt
1111
import matplotlib.figure as fg
1212

13-
fig, ax = plt.subplots(figsize=(16, 9))
13+
import matplotlib.image as mpimg
14+
15+
fig, axes = plt.subplots(2,2,figsize=(16, 9))
1416
print(type(fig))
15-
ax.plot([1,2], [1,2], color='green')
16-
ax.text(1.5, 1.5, '2', size=50)
17-
ax.set_xlabel('swag')
17+
axes[0, 0].plot([1,2], [1,2], color='green')
18+
axes[0, 1].text(0.5, 0.5, '2', size=50)
19+
axes[1, 0].set_xlabel('swag')
20+
axes[1, 1].imshow(mpimg.imread('images' + os.sep + 'long_dog.jpg'))
1821

1922

2023
plt.show()

0 commit comments

Comments
 (0)