Skip to content

Conversation

@pirzada-ahmadfaraz
Copy link

Closes #30934

Summary

This PR adds support for the gapcolor property in Patches, similar to the existing gapcolor feature in Line2D and LineCollection. This allows drawing "stripey" patch edges with two alternating colors in dashed patterns.

Use Case

Drawing unfilled patches on backgrounds of unknown color (very light or very dark), where alternating black/white edges ensure the patch is always visible.

Example

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, ax = plt.subplots()
rect = Rectangle((0.1, 0.1), 0.5, 0.5, fill=False,
                  edgecolor='black', gapcolor='white',
                  linestyle='--', linewidth=3)
ax.add_patch(rect)
plt.show()

Changes

  • Add gapcolor parameter to Patch.__init__
  • Add get_gapcolor() and set_gapcolor() methods
  • Add is_dashed() method to check if edge is dashed
  • Modify _draw_paths_with_artist_properties() to draw gaps first with inverse dash pattern when gapcolor is set
  • Update update_from() to copy gapcolor
  • Add tests for the new functionality

Implementation

Following the same approach as Line2D:

  1. If dashed and gapcolor is set, first draw the path with gapcolor using an inverse dash pattern (fills the gaps)
  2. Then draw the normal dashed edge on top

Add support for gapcolor property in Patches, similar to the existing
gapcolor feature in Line2D and LineCollection. This allows drawing
'stripey' patch edges with two alternating colors in dashed patterns.

Use case: Drawing unfilled patches on backgrounds of unknown color,
where alternating black/white edges ensure visibility.

Changes:
- Add gapcolor parameter to Patch.__init__
- Add get_gapcolor() and set_gapcolor() methods
- Add is_dashed() method to check if edge is dashed
- Modify _draw_paths_with_artist_properties() to draw gaps first
  with inverse dash pattern when gapcolor is set
- Update update_from() to copy gapcolor
- Add tests for the new functionality

Closes matplotlib#30934
@rcomer
Copy link
Member

rcomer commented Jan 15, 2026

Thank you for the PR @pirzada-ahmadfaraz! You will need to add the expected image file here. For details of how to do that, see https://matplotlib.org/devdocs/devel/testing.html#writing-an-image-comparison-test

@pirzada-ahmadfaraz
Copy link
Author

Thank you for the PR @pirzada-ahmadfaraz! You will need to add the expected image file here. For details of how to do that, see https://matplotlib.org/devdocs/devel/testing.html#writing-an-image-comparison-test

i am not able to create a code base for the branch it says setting up your codebase from last 30 minutes mind helping me with it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH]: Implement gapcolor for patch edges

2 participants