Skip to content

[Bug]: offset dash linestyle has no effect in patch objects #22977

Closed

Description

@oliverpriebe

Bug summary

When setting the linestyle on a patch object using a dash tuple the offset has no effect.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib as mpl

plt.figure(figsize=(10,10))
ax = plt.gca()
ax.add_patch(mpl.patches.Rectangle((0.5,0.5),1,1, alpha=0.5, edgecolor = 'r', linewidth=4, ls=(0,(10,10))))
ax.add_patch(mpl.patches.Rectangle((0.5,0.5),1,1, alpha=0.5, edgecolor = 'b', linewidth=4, ls=(10,(10,10))))
plt.ylim([0,2])
plt.xlim([0,2])
plt.show()

Actual outcome

Screen Shot 2022-05-04 at 4 45 33 PM

the patch edge lines overlap, not adhering to the offset.

Expected outcome

Haven't been able to get any patch objects to have a proper offset on the edge line style but the expected outcome is shown here with Line2D objects

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

ax_g = plt.gca()

x = np.linspace(0, np.pi*4, 100)
y = np.sin(x+np.pi/2)
z = np.sin(x+np.pi/4)
w = np.sin(x)

plt.plot(x, y, ls=(0, (10, 10)), color='b')
plt.plot(x, y, ls=(10, (10, 10)), color='r')
plt.show()

Screen Shot 2022-05-04 at 4 59 25 PM

Additional information

I have tried the Ellipse patch object as well and found the same issue. I also reproduced in Ubuntu 18.04 VM running matplotlib 3.5.0 with agg backend.

Operating system

OS/X

Matplotlib Version

3.3.4

Matplotlib Backend

MacOSX

Python version

Python 3.8.8

Jupyter version

No response

Installation

conda

Activity

  1. oliverpriebe commented on May 4, 2022

    @oliverpriebe
    Author

    Upon digging deeper into this issue it appears that this actually the intended behavior:

    # Patch has traditionally ignored the dashoffset.

    So it might be prudent to just update the docstring to reflect this fact.

    I'm curious why this was made the default behavior though

  2. oliverpriebe commented on May 5, 2022

    @oliverpriebe
    Author

    replacing the 0 here with the passed offset works completely fine on my OSX and Ubuntu setups.

    self, _dash_pattern=(0, self._dash_pattern[1])), \

  3. tacaswell commented on May 5, 2022

    @tacaswell
    Member

    @oliverpriebe Why do you want to do this?

    On one hand, we will sort out how to manage changing behavior when we need to, but on the other hand we need to have a very good reason to change long-standing behavior!

  4. oliverpriebe commented on May 5, 2022

    @oliverpriebe
    Author

    I'd like to use edge colors (red/blue) to denote a binary property of an entity represented by a rectangular patch that may overlap exactly with another entity with the opposite property value. When they overlap I'd like to easily see the two colors -- which isn't possible by just using low alphas.

    Admittedly this is both a niche use case and can be worked around by hacking the onoffseq as so

    plt.figure(1); plt.clf()
    ax = plt.gca()
    ax.add_patch(mpl.patches.Rectangle(
                      (0, 0),
                      1, 1,
                      facecolor = 'gray',
                      edgecolor = 'r',
                      linestyle = (0, [6, 0, 0, 6]),
                      fill = True
                    ))
    ax.add_patch(mpl.patches.Rectangle(
                      (0, 0),
                      1, 1,
                      facecolor = 'gray',
                      edgecolor = 'r',
                      linestyle = (0, [0, 6, 6, 0]),
                      fill = True
                    ))
    ax.set_xlim([-2, 2])
    ax.set_ylim([-2, 2])
    
  5. oliverpriebe commented on May 5, 2022

    @oliverpriebe
    Author

    but it might save the next poor soul some time if the docstring was updated

  6. timhoffm commented on May 5, 2022

    @timhoffm
    Member

    I couldn't find a reason why we should ignore dash offset here. If this was intended, we should issue a warning if the user sets a non-zero value. However I rather think this was an oversight and even though noticed, nobody bothered to take action.

    # Patch has traditionally ignored the dashoffset.

    This is a niche feature that almost nobody will use. But AFAICS, there's little harm in supporting offests here. The only user code we could break with that is if users would explicitly have set an offset but rely on it not being applied. That's not something we'd have to guard against. To me this is simply a bug (affecting very little users), and we could fix it right away.

  7. added
    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Jun 4, 2022
  8. oscargus commented on Jun 4, 2022

    @oscargus
    Member

    Marking this as good first issue as there is a minor modification required. Most work will be related to tests, probably an equality test with the workaround and the fixed code, and writing a sensible user release note clarifying that this has been fixed.

  9. added this to the v3.6.0 milestone on Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesstatus: has patchpatch suggested, PR still needed🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions