Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f29cfd

Browse files
committed
Cairo backend: Fix alpha render of collections
Remove RendererCairo.draw_path_collection as it made some wrong assumptions in the grouped draw optimization. Add test for the cairo backend to check rendering of path collections. Fixes #12773
1 parent ed0ae16 commit 7f29cfd

File tree

2 files changed

+54
-51
lines changed

2 files changed

+54
-51
lines changed

lib/matplotlib/backends/backend_cairo.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -286,57 +286,6 @@ def draw_markers(self, gc, marker_path, marker_trans, path, transform,
286286
self._fill_and_stroke(
287287
ctx, rgbFace, gc.get_alpha(), gc.get_forced_alpha())
288288

289-
def draw_path_collection(
290-
self, gc, master_transform, paths, all_transforms, offsets,
291-
offsetTrans, facecolors, edgecolors, linewidths, linestyles,
292-
antialiaseds, urls, offset_position):
293-
294-
path_ids = []
295-
for path, transform in self._iter_collection_raw_paths(
296-
master_transform, paths, all_transforms):
297-
path_ids.append((path, Affine2D(transform)))
298-
299-
reuse_key = None
300-
grouped_draw = []
301-
302-
def _draw_paths():
303-
if not grouped_draw:
304-
return
305-
gc_vars, rgb_fc = reuse_key
306-
gc = copy.copy(gc0)
307-
# We actually need to call the setters to reset the internal state.
308-
vars(gc).update(gc_vars)
309-
for k, v in gc_vars.items():
310-
if k == "_linestyle": # Deprecated, no effect.
311-
continue
312-
try:
313-
getattr(gc, "set" + k)(v)
314-
except (AttributeError, TypeError) as e:
315-
pass
316-
gc.ctx.new_path()
317-
paths, transforms = zip(*grouped_draw)
318-
grouped_draw.clear()
319-
_append_paths(gc.ctx, paths, transforms)
320-
self._fill_and_stroke(
321-
gc.ctx, rgb_fc, gc.get_alpha(), gc.get_forced_alpha())
322-
323-
for xo, yo, path_id, gc0, rgb_fc in self._iter_collection(
324-
gc, master_transform, all_transforms, path_ids, offsets,
325-
offsetTrans, facecolors, edgecolors, linewidths, linestyles,
326-
antialiaseds, urls, offset_position):
327-
path, transform = path_id
328-
transform = (Affine2D(transform.get_matrix())
329-
.translate(xo, yo - self.height).scale(1, -1))
330-
# rgb_fc could be a ndarray, for which equality is elementwise.
331-
new_key = vars(gc0), tuple(rgb_fc) if rgb_fc is not None else None
332-
if new_key == reuse_key:
333-
grouped_draw.append((path, transform))
334-
else:
335-
_draw_paths()
336-
grouped_draw.append((path, transform))
337-
reuse_key = new_key
338-
_draw_paths()
339-
340289
def draw_image(self, gc, x, y, im):
341290
im = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(im[::-1])
342291
surface = cairo.ImageSurface.create_for_data(
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import numpy as np
2+
from io import BytesIO
3+
import os
4+
import tempfile
5+
import warnings
6+
import xml.parsers.expat
7+
8+
import pytest
9+
10+
import matplotlib.pyplot as plt
11+
from matplotlib.testing.decorators import check_figures_equal
12+
import matplotlib
13+
from matplotlib import (
14+
collections as mcollections, patches as mpatches, path as mpath)
15+
16+
@pytest.mark.backend('cairo')
17+
@check_figures_equal(extensions=["png"])
18+
def test_patch_alpha_coloring(fig_test, fig_ref):
19+
"""
20+
Test checks that the patch and collection are rendered with the specified
21+
alpha values in their facecolor and edgecolor.
22+
"""
23+
star = mpath.Path.unit_regular_star(6)
24+
circle = mpath.Path.unit_circle()
25+
# concatenate the star with an internal cutout of the circle
26+
verts = np.concatenate([circle.vertices, star.vertices[::-1]])
27+
codes = np.concatenate([circle.codes, star.codes])
28+
cut_star1 = mpath.Path(verts, codes)
29+
cut_star2 = mpath.Path(verts + 1, codes)
30+
31+
# Reference: two separate patches
32+
ax = fig_ref.subplots()
33+
ax.set_xlim([-1, 2])
34+
ax.set_ylim([-1, 2])
35+
patch = mpatches.PathPatch(cut_star1,
36+
linewidth=5, linestyle='dashdot',
37+
facecolor=(1, 0, 0, 0.5),
38+
edgecolor=(0, 0, 1, 0.75))
39+
ax.add_patch(patch)
40+
patch = mpatches.PathPatch(cut_star2,
41+
linewidth=5, linestyle='dashdot',
42+
facecolor=(1, 0, 0, 0.5),
43+
edgecolor=(0, 0, 1, 0.75))
44+
ax.add_patch(patch)
45+
46+
# Test: path collection
47+
ax = fig_test.subplots()
48+
ax.set_xlim([-1, 2])
49+
ax.set_ylim([-1, 2])
50+
col = mcollections.PathCollection([cut_star1, cut_star2],
51+
linewidth=5, linestyles='dashdot',
52+
facecolor=(1, 0, 0, 0.5),
53+
edgecolor=(0, 0, 1, 0.75))
54+
ax.add_collection(col)

0 commit comments

Comments
 (0)