-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Enable choosing the even-odd rule for filling shapes #32253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| Option to use the even-odd fill rule for patches | ||
| ------------------------------------------------ | ||
|
|
||
| By default, patches such as `~.patches.Polygon` are filled according to the | ||
| `non-zero winding fill rule <https://en.wikipedia.org/wiki/Nonzero-rule>`__. | ||
| There is now the option to instead use the | ||
| `even-odd fill rule <https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule>`__, | ||
| which is specified by setting the patch's ``fill_rule`` property to "evenodd". | ||
| See :doc:`/gallery/shapes_and_collections/fill_rule_demo` for more details and | ||
| an illustration of the difference. |
67 changes: 67 additions & 0 deletions
67
galleries/examples/shapes_and_collections/fill_rule_demo.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """ | ||
| ============== | ||
| Fill rule demo | ||
| ============== | ||
|
|
||
| By default, patches such as `~.patches.Polygon` are filled according to the | ||
| `non-zero winding fill rule <https://en.wikipedia.org/wiki/Nonzero-rule>`__. | ||
| Any given point has a winding number, which is the number of times the path | ||
| wraps around the point in the clockwise direction. For this fill rule, the | ||
| filled regions are where the winding number is non-zero. See | ||
| :doc:`/gallery/shapes_and_collections/donut` for an example of how to leverage | ||
| the winding directions in multiple segments of a path under this fill rule. | ||
|
|
||
| The other option for fill rule is the | ||
| `even-odd fill rule <https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule>`__, | ||
| which is specified by setting the patch's ``fill_rule`` property to "evenodd". | ||
| For this fill rule, the filled regions are where the winding number is an odd | ||
| number. This fill rule allows for the construction of patterns of fill | ||
| regions that would otherwise take many more vertices to construct under the | ||
| non-zero winding fill rule. | ||
|
|
||
| This example demonstrates the difference between the two fill rules for a single | ||
| `~.patches.Polygon` that intersects itself multiple times. The winding number | ||
| for each closed region is labeled. | ||
|
|
||
| """ | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
|
|
||
| from matplotlib.patches import Polygon | ||
|
|
||
| fig, axs = plt.subplots(1, 2) | ||
|
|
||
| vertices = np.array([[0, 0, 6, 6, 1, 1, 5, 5, 2, 2, 4, 4, 3, 3, 5, 5], | ||
| [2, 5, 5, 0, 0, 7, 7, 3, 3, 4, 4, 6, 6, 1, 1, 2]]).T | ||
|
|
||
| labels = ['1', '0', '1', '2', '3', '2', '1', '1', '0'] | ||
| label_xys = np.array([[2.0, 4.0, 0.5, 1.5, 2.5, 4.0, 3.5, 2.0, 3.5], | ||
| [1.5, 1.5, 3.5, 3.5, 3.5, 3.5, 4.5, 5.5, 5.5]]).T | ||
|
|
||
| for ax, fill_rule in zip(axs, ['nonzero', 'evenodd']): | ||
| polygon = Polygon(vertices, facecolor='green', edgecolor='red', | ||
| fill_rule=fill_rule) | ||
| ax.add_patch(polygon) | ||
|
|
||
| ax.plot(*vertices.T, '.', markersize=10, color='red') | ||
|
|
||
| for label, label_xy in zip(labels, label_xys): | ||
| ax.text(*label_xy, label, ha='center', va='center') | ||
|
|
||
| ax.set_axis_off() | ||
| ax.set_title(f'fill_rule={fill_rule}') | ||
|
|
||
| plt.show() | ||
|
|
||
| # %% | ||
| # | ||
| # .. admonition:: References | ||
| # | ||
| # The use of the following functions, methods, classes and modules is shown | ||
| # in this example: | ||
| # | ||
| # - `matplotlib.patches` | ||
| # - `matplotlib.patches.Polygon` | ||
| # - `matplotlib.axes.Axes.add_patch` | ||
| # - `matplotlib.patches.Patch.set_fill_rule` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+2.77 KB
lib/matplotlib/tests/baseline_images/test_backends_rendering/fill_rules.pdf
Binary file not shown.
Binary file added
BIN
+15.8 KB
lib/matplotlib/tests/baseline_images/test_backends_rendering/fill_rules.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.