Skip to content

Single path optimisation for Collection w/ offsets broken #16496

Closed
@dstansby

Description

@dstansby

This is the root cause of #16461. The single path optimisation does not correctly take into account offsets:

if do_single_path_optimization:
gc.set_foreground(tuple(edgecolors[0]))
gc.set_linewidth(self._linewidths[0])
gc.set_dashes(*self._linestyles[0])
gc.set_antialiased(self._antialiaseds[0])
gc.set_url(self._urls[0])
renderer.draw_markers(
gc, paths[0], combined_transform.frozen(),
mpath.Path(offsets), transOffset, tuple(facecolors[0]))

As an example, this code creates two collections which differ only by their edgecolors, and have an offset of [0.5, 0.5]. In both cases the collection should be drawn at the offset, but only the collection with edgecolors=None is correct, because this does not follow the 'optimised' code path above.

import matplotlib.pyplot as plt
import matplotlib.collections as mcoll
import matplotlib.transforms as mtransforms
import numpy as np

polygon = np.array([[-1, -1], [-1, 1], [1, 1], [1, -1]]) * 0.1
offsets = np.array([0.5, 0.5])

fig, axs = plt.subplots(ncols=2)
for ax, edgecolors in zip(axs, [None, 'face']):
    collection = mcoll.PolyCollection(
        [polygon],
        edgecolors=edgecolors,
        linewidths=[1],
        offsets=offsets,
        transOffset=mtransforms.IdentityTransform(),
        offset_position="data"
        )
    ax.add_collection(collection)

plt.show()

Figure_1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions