diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index a038860d255c..c46e9cde521f 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -858,6 +858,19 @@ def get_clip_path(self): return self._clippath.get_transformed_path_and_affine() return None, None + @staticmethod + def scale_dashes(linewidth, offset_dashes): + """ + Scale the given offset, dashlist tuple to fit linewidth. + """ + scale = max(1.0, linewidth) + offset, dashes = offset_dashes + if offset is not None: + offset = offset * scale + if dashes is not None: + dashes = [x * scale for x in dashes] + return offset, dashes + def get_dashes(self): """ Return the dash information as an offset dashlist tuple. @@ -874,13 +887,7 @@ def get_dashes(self): if rcParams['_internal.classic_mode']: return self._dashes else: - scale = max(1.0, self.get_linewidth()) - offset, dashes = self._dashes - if offset is not None: - offset = offset * scale - if dashes is not None: - dashes = [x * scale for x in dashes] - return offset, dashes + return self.scale_dashes(self.get_linewidth(), self._dashes) def get_forced_alpha(self): """ diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 21719273c423..504d788dfd31 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2206,15 +2206,19 @@ def capstyle_cmd(self, style): def joinstyle_cmd(self, style): return [self.joinstyles[style], Op.setlinejoin] - def linewidth_cmd(self, width): - return [width, Op.setlinewidth] - - def dash_cmd(self, dashes): - offset, dash = dashes - if dash is None: - dash = [] - offset = 0 - return [list(dash), offset, Op.setdash] + def linewidth_dash_cmd(self, width, dashes): + result = [] + if self.get_linewidth() != width: + result += [width, Op.setlinewidth] + if self._dashes != dashes: + offset, dash = self.scale_dashes(width, dashes) + if offset is None or dash is None: + dash = [] + offset = 0 + if sum(dash) > 0: + offset %= sum(dash) + result += [list(dash), offset, Op.setdash] + return result def alpha_cmd(self, alpha, forced, effective_alphas): name = self.file.alphaState(effective_alphas) @@ -2288,14 +2292,11 @@ def clip_cmd(self, cliprect, clippath): (('_capstyle',), capstyle_cmd), (('_fillcolor',), fillcolor_cmd), (('_joinstyle',), joinstyle_cmd), - (('_linewidth',), linewidth_cmd), - (('_dashes',), dash_cmd), + (('_linewidth', '_dashes'), linewidth_dash_cmd), (('_rgb',), rgb_cmd), (('_hatch',), hatch_cmd), # must come after fillcolor and rgb ) - # TODO: _linestyle - def delta(self, other): """ Copy properties of other into self and return PDF commands diff --git a/lib/matplotlib/tests/baseline_images/test_axes/dash_offset.pdf b/lib/matplotlib/tests/baseline_images/test_axes/dash_offset.pdf index 486d6a723afb..4ac809840adb 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_axes/dash_offset.pdf and b/lib/matplotlib/tests/baseline_images/test_axes/dash_offset.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/step_linestyle.pdf b/lib/matplotlib/tests/baseline_images/test_axes/step_linestyle.pdf index 3944c50f50e8..049110de2b2d 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_axes/step_linestyle.pdf and b/lib/matplotlib/tests/baseline_images/test_axes/step_linestyle.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_collections/EventCollection_plot__set_linestyle.pdf b/lib/matplotlib/tests/baseline_images/test_collections/EventCollection_plot__set_linestyle.pdf index 4a4c75b5a6af..917db0047037 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_collections/EventCollection_plot__set_linestyle.pdf and b/lib/matplotlib/tests/baseline_images/test_collections/EventCollection_plot__set_linestyle.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_collections/EventCollection_plot__set_ls_dash.pdf b/lib/matplotlib/tests/baseline_images/test_collections/EventCollection_plot__set_ls_dash.pdf index 3952787106ec..6156bb874a3e 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_collections/EventCollection_plot__set_ls_dash.pdf and b/lib/matplotlib/tests/baseline_images/test_collections/EventCollection_plot__set_ls_dash.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_image/rotate_image.pdf b/lib/matplotlib/tests/baseline_images/test_image/rotate_image.pdf index f205e7ca7468..650aa594bb6c 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_image/rotate_image.pdf and b/lib/matplotlib/tests/baseline_images/test_image/rotate_image.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_lines/line_dashes.pdf b/lib/matplotlib/tests/baseline_images/test_lines/line_dashes.pdf index ca440b3adecb..817d12cd8d2d 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_lines/line_dashes.pdf and b/lib/matplotlib/tests/baseline_images/test_lines/line_dashes.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_patches/patch_alpha_coloring.pdf b/lib/matplotlib/tests/baseline_images/test_patches/patch_alpha_coloring.pdf index 06e31444a10f..e815ade2ca8b 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_patches/patch_alpha_coloring.pdf and b/lib/matplotlib/tests/baseline_images/test_patches/patch_alpha_coloring.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_patches/patch_alpha_override.pdf b/lib/matplotlib/tests/baseline_images/test_patches/patch_alpha_override.pdf index ac317e48faa1..010daed453ef 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_patches/patch_alpha_override.pdf and b/lib/matplotlib/tests/baseline_images/test_patches/patch_alpha_override.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_patches/patch_custom_linestyle.pdf b/lib/matplotlib/tests/baseline_images/test_patches/patch_custom_linestyle.pdf index 1b4d81d6f4bb..d74507042b7e 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_patches/patch_custom_linestyle.pdf and b/lib/matplotlib/tests/baseline_images/test_patches/patch_custom_linestyle.pdf differ