Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve hatch demo #17929

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 2 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions examples/shapes_and_collections/hatch_demo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
"""
==========
Hatch Demo
Hatch demo
==========

Hatching (pattern filled polygons) is supported currently in the PS,
PDF, SVG and Agg backends only.
Hatches can be added to most polygons in Matplotlib, including `~.Axes.bar`,
`~.Axes.fill_between`, `~.Axes.contourf`, and childern of `~.patches.Polygon`.
They are currently supported in the PS, PDF, SVG, OSX, and Agg backends. The WX
and Cairo backends do not currently support hatching.

See also :doc:`/gallery/images_contours_and_fields/contourf_hatching` for
an example using `~.Axes.contourf`, and
:doc:`/gallery/shapes_and_collections/hatch_style_reference` for swatches
of the existing hatches.

"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Polygon
Expand All @@ -14,23 +23,26 @@
y1 = np.arange(1, 5)
y2 = np.ones(y1.shape) * 4

fig, (ax1, ax2, ax3) = plt.subplots(3)

ax1.bar(x, y1, edgecolor='black', hatch="/")
ax1.bar(x, y2, bottom=y1, edgecolor='black', hatch='//')
ax1.set_xticks([1.5, 2.5, 3.5, 4.5])
fig = plt.figure()
axs = fig.subplot_mosaic([['bar1', 'patches'], ['bar2', 'patches']])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy dance that you're using mosiac (for the record fig, axs = plt.subplot_mosaic also works) 'cause I so think it's well suited for these kinds of things.
But also not clear why you're reassigning them:

ax = axs['bar1']
ax.bar(x, y2, bottom=y1, edgecolor='black', hatch=['*', 'o', 'O', '.'])

versus using 'em directly:

axs['bar'].bar(x, y2, bottom=y1, edgecolor='black', hatch=['*', 'o', 'O', '.'])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less typing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's that much less and I think the reassignment especially w/ same variable name is potential unnecessary source of failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... wrote out the long form

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reassignment especially w/ same variable name is potential unnecessary source of failure.

I'd argue the opposite - its less prone to typos and easier to refactor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's easier to refactor, but it's prone to leaving out that assignment statement and therefore messing up all the references.


ax2.bar(x, y1, edgecolor='black', hatch=['-', '+', 'x', '\\'])
ax2.bar(x, y2, bottom=y1, edgecolor='black', hatch=['*', 'o', 'O', '.'])
ax2.set_xticks([1.5, 2.5, 3.5, 4.5])
axs['bar1'].bar(x, y1, edgecolor='black', hatch="/")
axs['bar1'].bar(x, y2, bottom=y1, edgecolor='black', hatch='//')

ax3.fill([1, 3, 3, 1], [1, 1, 2, 2], fill=False, hatch='\\')
ax3.add_patch(Ellipse((4, 1.5), 4, 0.5, fill=False, hatch='*'))
ax3.add_patch(Polygon([[0, 0], [4, 1.1], [6, 2.5], [2, 1.4]], closed=True,
fill=False, hatch='/'))
ax3.set_xlim((0, 6))
ax3.set_ylim((0, 2.5))
axs['bar2'].bar(x, y1, edgecolor='black', hatch=['--', '+', 'x', '\\'])
axs['bar2'].bar(x, y2, bottom=y1, edgecolor='black',
hatch=['*', 'o', 'O', '.'])

x = np.arange(0, 40, 0.2)
axs['patches'].fill_between(x, np.sin(x) * 4 + 30, y2=0,
hatch='///', zorder=2, fc='c')
axs['patches'].add_patch(Ellipse((4, 50), 10, 10, fill=True,
hatch='*', facecolor='y'))
axs['patches'].add_patch(Polygon([(10, 20), (30, 50), (50, 10)],
hatch='\\/...', facecolor='g'))
axs['patches'].set_xlim([0, 40])
axs['patches'].set_ylim([10, 60])
axs['patches'].set_aspect(1)
plt.show()

#############################################################################
Expand Down
11 changes: 9 additions & 2 deletions examples/shapes_and_collections/hatch_style_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
Hatch style reference
=====================
Hatching (pattern filled polygons) is currently supported in the backends
PS, PDF, SVG and Agg. The backends OSX, WX and Cairo ignore hatching.
Hatches can be added to most polygons in Matplotlib, including `~.Axes.bar`,
`~.Axes.fill_between`, `~.Axes.contourf`, and childern of `~.patches.Polygon`.
They are currently supported in the PS, PDF, SVG, OSX, and Agg backends. The WX
and Cairo backends do not currently support hatching.
See also :doc:`/gallery/images_contours_and_fields/contourf_hatching` for
an example using `~.Axes.contourf`, and
:doc:`/gallery/shapes_and_collections/hatch_demo` for more usage examples.
"""
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
Expand Down