Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/data.pkl
Binary file not shown.
74 changes: 53 additions & 21 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from matplotlib import _path
from . import _backend_pdf_ps

from matplotlib import transforms as mtransforms

_log = logging.getLogger(__name__)

# Overview
Expand Down Expand Up @@ -1595,6 +1597,16 @@ def writeHatches(self):
'Matrix': [1, 0, 0, 1, 0, self.height * 72]})

stroke_rgb, fill_rgb, hatch, lw = hatch_style

# NEW: Robustness for cases where the edge/stroke is set to None.
if stroke_rgb is None:
# If there is fill, use the same color for the stroke;
# If there is no fill, fall back to black (neutral fallback)
if fill_rgb is not None:
stroke_rgb = fill_rgb
else:
stroke_rgb = (0, 0, 0)

self.output(stroke_rgb[0], stroke_rgb[1], stroke_rgb[2],
Op.setrgb_stroke)
if fill_rgb is not None:
Expand Down Expand Up @@ -1872,13 +1884,29 @@ def writeMarkers(self):
self.endStream()

def pathCollectionObject(self, gc, path, trans, padding, filled, stroked):
simplify = (
mpl.rcParams.get("path.simplify", False)
and mpl.rcParams.get("path.simplify_threshold", 0) > 0
# default False: só simplifica quem for marcado
and getattr(path, "should_simplify", False)
)

if simplify and trans is not None:
try:
cleaned = path.cleaned(transform=trans, simplify=True)
path = cleaned
trans = mtransforms.IdentityTransform()
except Exception:
pass

name = Name('P%d' % len(self.paths))
ob = self.reserveObject('path %d' % len(self.paths))
self.paths.append(
(name, path, trans, ob, gc.get_joinstyle(), gc.get_capstyle(),
padding, filled, stroked))
padding, filled, stroked))
return name


def writePathCollectionTemplates(self):
for (name, path, trans, ob, joinstyle, capstyle, padding, filled,
stroked) in self.paths:
Expand Down Expand Up @@ -1912,7 +1940,7 @@ def pathOperations(path, transform, clip=None, simplify=None, sketch=None):
def writePath(self, path, transform, clip=False, sketch=None):
if clip:
clip = (0.0, 0.0, self.width * 72, self.height * 72)
simplify = path.should_simplify
simplify = getattr(path, "should_simplify", False)
else:
clip = None
simplify = False
Expand Down Expand Up @@ -2053,19 +2081,14 @@ def draw_path(self, gc, path, transform, rgbFace=None):
self.file.output(self.gc.paint())

def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position, *, hatchcolors=None):
# We can only reuse the objects if the presence of fill and
# stroke (and the amount of alpha for each) is the same for
# all of them
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position, *, hatchcolors=None):

can_do_optimization = True
facecolors = np.asarray(facecolors)
edgecolors = np.asarray(edgecolors)

if hatchcolors is None:
hatchcolors = []

if not len(facecolors):
filled = False
can_do_optimization = not gc.get_hatch()
Expand All @@ -2085,15 +2108,21 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
else:
can_do_optimization = False

# Is the optimization worth it? Rough calculation:
# cost of emitting a path in-line is len_path * uses_per_path
# cost of XObject is len_path + 5 for the definition,
# uses_per_path for the uses
len_path = len(paths[0].vertices) if len(paths) > 0 else 0
len_path = len(paths[0].vertices) if paths else 0
uses_per_path = self._iter_collection_uses_per_path(
paths, all_transforms, offsets, facecolors, edgecolors)
should_do_optimization = \
len_path + uses_per_path + 5 < len_path * uses_per_path

any_should_simplify = any(
getattr(p, "should_simplify", False) for p in paths
)

should_do_optimization = (
(len_path + 3) + 9 * uses_per_path
< (len_path + 5) * uses_per_path
)

if any_should_simplify:
should_do_optimization = True

if (not can_do_optimization) or (not should_do_optimization):
return RendererBase.draw_path_collection(
Expand All @@ -2106,8 +2135,10 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
path_codes = []
for i, (path, transform) in enumerate(self._iter_collection_raw_paths(
master_transform, paths, all_transforms)):

name = self.file.pathCollectionObject(
gc, path, transform, padding, filled, stroked)
gc, path, transform, padding, filled, stroked
)
path_codes.append(name)

output = self.file.output
Expand All @@ -2116,12 +2147,13 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
gc, path_codes, offsets, offset_trans,
facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position, hatchcolors=hatchcolors):
antialiaseds, urls, offset_position,
hatchcolors=hatchcolors):

self.check_gc(gc0, rgbFace)
dx, dy = xo - lastx, yo - lasty
output(1, 0, 0, 1, dx, dy, Op.concat_matrix, path_id,
Op.use_xobject)
Op.use_xobject)
lastx, lasty = xo, yo
output(*self.gc.pop())

Expand Down
62 changes: 43 additions & 19 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from matplotlib.transforms import Affine2D, Affine2DBase



_log = logging.getLogger(__name__)


Expand Down Expand Up @@ -665,20 +666,32 @@ def option_image_nocomposite(self):
return not mpl.rcParams['image.composite_image']

def _convert_path(self, path, transform=None, clip=None, simplify=None,
sketch=None):
sketch=None):
if clip:
clip = (0.0, 0.0, self.width, self.height)
else:
clip = None

if simplify is None:
simplify = (
mpl.rcParams.get("path.simplify", False)
and mpl.rcParams.get("path.simplify_threshold", 0) > 0
# default = False, only simplifies what's already marked
and getattr(path, "should_simplify", False)
)

return _path.convert_to_string(
path, transform, clip, simplify, sketch, 6,
[b'M', b'L', b'Q', b'C', b'z'], False).decode('ascii')
[b'M', b'L', b'Q', b'C', b'z'], False
).decode('ascii')


def draw_path(self, gc, path, transform, rgbFace=None):
# docstring inherited
trans_and_flip = self._make_flip_transform(transform)
clip = (rgbFace is None and gc.get_hatch_path() is None)
simplify = path.should_simplify and clip
should = getattr(path, "should_simplify", False)
simplify = should
path_data = self._convert_path(
path, trans_and_flip, clip=clip, simplify=simplify,
sketch=gc.get_sketch_params())
Expand Down Expand Up @@ -734,35 +747,44 @@ def draw_markers(
writer.end('g')

def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position, *, hatchcolors=None):
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position, *, hatchcolors=None):

if hatchcolors is None:
hatchcolors = []
# Is the optimization worth it? Rough calculation:
# cost of emitting a path in-line is
# (len_path + 5) * uses_per_path
# cost of definition+use is
# (len_path + 3) + 9 * uses_per_path
len_path = len(paths[0].vertices) if len(paths) > 0 else 0
if not paths:
return

len_path = len(paths[0].vertices) if paths else 0
uses_per_path = self._iter_collection_uses_per_path(
paths, all_transforms, offsets, facecolors, edgecolors)
should_do_optimization = \
len_path + 9 * uses_per_path + 3 < (len_path + 5) * uses_per_path
should_do_optimization = (
(len_path + 3) + 9 * uses_per_path
< (len_path + 5) * uses_per_path
)

if not should_do_optimization:
return super().draw_path_collection(
gc, master_transform, paths, all_transforms,
offsets, offset_trans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position, hatchcolors=hatchcolors)
offset_position)

writer = self.writer
path_codes = []
writer.start('defs')
for i, (path, transform) in enumerate(self._iter_collection_raw_paths(
master_transform, paths, all_transforms)):

# transform on display coords
transform = Affine2D(transform.get_matrix()).scale(1.0, -1.0)
d = self._convert_path(path, transform, simplify=False)

# _convert_path decides whether or not to simplify, based on rcParams
# and a flag like "path.should_simplify".
simplify = None # None == "use default / rcParams / path flag"
d = self._convert_path(path, transform, simplify=simplify)

oid = 'C{:x}_{:x}_{}'.format(
self._path_collection_id, i, self._make_id('', d))
writer.element('path', id=oid, d=d)
Expand All @@ -772,7 +794,9 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
for xo, yo, path_id, gc0, rgbFace in self._iter_collection(
gc, path_codes, offsets, offset_trans,
facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position, hatchcolors=hatchcolors):
antialiaseds, urls, offset_position,
hatchcolors=hatchcolors):

url = gc0.get_url()
if url is not None:
writer.start('a', attrib={'xlink:href': url})
Expand All @@ -783,8 +807,8 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
'xlink:href': f'#{path_id}',
'x': _short_float_fmt(xo),
'y': _short_float_fmt(self.height - yo),
'style': self._get_style(gc0, rgbFace)
}
'style': self._get_style(gc0, rgbFace),
}
writer.element('use', attrib=attrib)
if clip_attrs:
writer.end('g')
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,15 @@ def __init__(
verts = self._make_verts(t, f1, f2, where)
super().__init__(verts, **kwargs)

def set_verts(self, verts, closed=True):
super().set_verts(verts, closed=closed)
if self._paths is not None:
for p in self._paths:
setattr(p, "should_simplify", True)

# make sure set_paths also uses this version of set_verts
set_paths = set_verts

@staticmethod
def _f_dir_from_t(t_direction):
"""The direction that is other than `t_direction`."""
Expand Down
Loading
Loading