Skip to content

Commit 6d3098b

Browse files
committed
fix tight layout and rotation bugs
1 parent 44c6c7f commit 6d3098b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

pygame_matplotlib/backend_pygame.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
181181
poly_points = []
182182

183183
for point, code in path.iter_segments(transform):
184-
logger.debug(point, code)
184+
logger.debug(f"{point=}, {code=}")
185185
if code == Path.LINETO:
186186
draw_func(self.surface, color, previous_point, point)
187187
previous_point = point
@@ -239,6 +239,11 @@ def draw_image(self, gc, x, y, im):
239239
)
240240

241241
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
242+
243+
logger.info(
244+
f"Drawing text: {s=} at ({x=}, {y=}) with {angle=} and ismath={ismath} "
245+
f"{mtext=} {prop=} {gc=}"
246+
)
242247
# make sure font module is initialized
243248
if not pygame.font.get_init():
244249
pygame.font.init()
@@ -249,6 +254,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
249254
font_surface = myfont.render(
250255
s, gc.get_antialiased(), [val * 255 for val in gc.get_rgb()]
251256
)
257+
if angle:
258+
font_surface = pygame.transform.rotate(font_surface, angle)
259+
252260
# Get the expected size of the font
253261
width, height = myfont.size(s)
254262
# Tuple for the position of the font
@@ -311,6 +319,8 @@ def points_to_pixels(self, points):
311319
# return points/72.0 * self.dpi.get()
312320

313321
def clear(self):
322+
if not hasattr(self, "surface"):
323+
return
314324
self.surface.fill("white")
315325

316326
def copy_from_bbox(self, bbox):

tests/test_mpl_functionalities.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib
3+
4+
matplotlib.use("pygame")
5+
6+
7+
def test_save_jpg():
8+
plt.figure()
9+
plt.plot([1, 2], [1, 2], color="green")
10+
plt.text(1.5, 1.5, "2", size=50)
11+
12+
def test_tight_layout():
13+
plt.figure()
14+
plt.plot([1, 2], [1, 2], color="green")
15+
plt.text(1.5, 1.5, "2", size=50)
16+
plt.tight_layout()
17+
18+
19+
def test_ylabel():
20+
# Issue that the ylabel was not reotated, sadly I cannot visually test this
21+
plt.figure()
22+
plt.plot([1, 2], [1, 2], color="green")
23+
plt.text(1.5, 1.5, "2", size=50)
24+
plt.ylabel("swag")
25+
plt.tight_layout()

0 commit comments

Comments
 (0)