Skip to content

Commit dc208c0

Browse files
committed
added support for filled plots
1 parent 0196f55 commit dc208c0

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

pygame_matplotlib/backend_pygame.py

Lines changed: 25 additions & 11 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+
from pygame import gfxdraw
1314
from matplotlib._pylab_helpers import Gcf
1415
from matplotlib.backend_bases import (
1516
FigureCanvasBase, FigureManagerBase, GraphicsContextBase, RendererBase)
@@ -18,7 +19,6 @@
1819

1920
class FigureSurface(pygame.Surface, Figure):
2021
def __init__(self, *args, **kwargs):
21-
print("initialized")
2222
Figure.__init__(self, *args, **kwargs)
2323
pygame.Surface.__init__(self, self.bbox.size)
2424
self.fill('white')
@@ -36,9 +36,10 @@ def __init__(self, dpi):
3636

3737
def draw_path(self, gc, path, transform, rgbFace=None):
3838

39-
if rgbFace is None:
40-
rgbFace = gc.get_rgb()
41-
color = tuple([int(val*255) for i, val in enumerate(rgbFace) if i < 3])
39+
if rgbFace is not None:
40+
color = tuple([int(val*255) for i, val in enumerate(rgbFace) if i < 3])
41+
else:
42+
color = tuple([int(val*255) for i, val in enumerate(gc.get_rgb()) if i < 3])
4243

4344
linewidth = int(gc.get_linewidth())
4445

@@ -54,25 +55,38 @@ def draw_path(self, gc, path, transform, rgbFace=None):
5455
)
5556

5657
previous_point = (0, 0)
58+
poly_points = []
5759
for point, code in path.iter_segments(transform):
5860
# previous_point = point
5961
# print(point, code)
62+
6063
if code == Path.LINETO:
6164
draw_func(
6265
self.surface, color, previous_point, point, linewidth
6366
)
6467
previous_point = point
65-
elif code == Path.CURVE3:
68+
poly_points.append(point)
69+
elif code == Path.CURVE3 or code == Path.CURVE4:
6670
end_point = point[2:]
67-
control_point = point[:2]
68-
# TODO: use bezier arcs instead
69-
draw_func(
70-
self.surface, color, previous_point, end_point, linewidth
71+
points_curve = np.concatenate((previous_point, point)).reshape((-1, 2))
72+
gfxdraw.bezier(
73+
self.surface, points_curve, len(points_curve), color
7174
)
7275
previous_point = end_point
73-
else:
76+
poly_points.append(end_point)
77+
elif code == Path.CLOSEPOLY:
78+
print('close', poly_points, point)
79+
if len(poly_points) > 2:
80+
gfxdraw.filled_polygon(
81+
self.surface,
82+
poly_points,
83+
color
84+
)
85+
elif code == Path.MOVETO:
86+
poly_points.append(point)
87+
previous_point = point
88+
else: # STOP
7489
previous_point = point
75-
pass
7690

7791
# draw_markers is optional, and we get more correct relative
7892
# timings by leaving it out. backend implementers concerned with

0 commit comments

Comments
 (0)