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

Skip to content

Commit c8bafcd

Browse files
authored
Merge pull request #24213 from anntzer/mcpfp
Cleanup make_compound_path_from_poly doc, example.
2 parents a39c9e6 + b260408 commit c8bafcd

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

examples/misc/histogram_path.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,26 @@
44
========================================================
55
66
Using a path patch to draw rectangles.
7-
The technique of using lots of Rectangle instances, or
8-
the faster method of using PolyCollections, were implemented before we
9-
had proper paths with moveto/lineto, closepoly etc in mpl. Now that
10-
we have them, we can draw collections of regularly shaped objects with
11-
homogeneous properties more efficiently with a PathCollection. This
12-
example makes a histogram -- it's more work to set up the vertex arrays
13-
at the outset, but it should be much faster for large numbers of
14-
objects.
7+
8+
The technique of using lots of `.Rectangle` instances, or the faster method of
9+
using `.PolyCollection`, were implemented before we had proper paths with
10+
moveto, lineto, closepoly, etc. in Matplotlib. Now that we have them, we can
11+
draw collections of regularly shaped objects with homogeneous properties more
12+
efficiently with a PathCollection. This example makes a histogram -- it's more
13+
work to set up the vertex arrays at the outset, but it should be much faster
14+
for large numbers of objects.
1515
"""
1616

1717
import numpy as np
1818
import matplotlib.pyplot as plt
1919
import matplotlib.patches as patches
2020
import matplotlib.path as path
2121

22-
fig, ax = plt.subplots()
23-
24-
# Fixing random state for reproducibility
25-
np.random.seed(19680801)
22+
fig, axs = plt.subplots(2)
2623

24+
np.random.seed(19680801) # Fixing random state for reproducibility
2725

2826
# histogram our data with numpy
29-
3027
data = np.random.randn(1000)
3128
n, bins = np.histogram(data, 50)
3229

@@ -36,28 +33,23 @@
3633
bottom = np.zeros(len(left))
3734
top = bottom + n
3835

39-
4036
# we need a (numrects x numsides x 2) numpy array for the path helper
4137
# function to build a compound path
4238
XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T
4339

4440
# get the Path object
4541
barpath = path.Path.make_compound_path_from_polys(XY)
4642

47-
# make a patch out of it
43+
# make a patch out of it, don't add a margin at y=0
4844
patch = patches.PathPatch(barpath)
49-
ax.add_patch(patch)
50-
51-
# update the view limits
52-
ax.set_xlim(left[0], right[-1])
53-
ax.set_ylim(bottom.min(), top.max())
54-
55-
plt.show()
45+
patch.sticky_edges.y[:] = [0]
46+
axs[0].add_patch(patch)
47+
axs[0].autoscale_view()
5648

5749
#############################################################################
58-
# It should be noted that instead of creating a three-dimensional array and
59-
# using `~.path.Path.make_compound_path_from_polys`, we could as well create
60-
# the compound path directly using vertices and codes as shown below
50+
# Instead of creating a three-dimensional array and using
51+
# `~.path.Path.make_compound_path_from_polys`, we could as well create the
52+
# compound path directly using vertices and codes as shown below
6153

6254
nrects = len(left)
6355
nverts = nrects*(1+3+1)
@@ -76,6 +68,14 @@
7668

7769
barpath = path.Path(verts, codes)
7870

71+
# make a patch out of it, don't add a margin at y=0
72+
patch = patches.PathPatch(barpath)
73+
patch.sticky_edges.y[:] = [0]
74+
axs[1].add_patch(patch)
75+
axs[1].autoscale_view()
76+
77+
plt.show()
78+
7979
#############################################################################
8080
#
8181
# .. admonition:: References

lib/matplotlib/path.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,15 @@ def __deepcopy__(self, memo=None):
293293
@classmethod
294294
def make_compound_path_from_polys(cls, XY):
295295
"""
296-
Make a compound path object to draw a number
297-
of polygons with equal numbers of sides XY is a (numpolys x
298-
numsides x 2) numpy array of vertices. Return object is a
299-
:class:`Path`.
296+
Make a compound `Path` object to draw a number of polygons with equal
297+
numbers of sides.
300298
301299
.. plot:: gallery/misc/histogram_path.py
302300
301+
Parameters
302+
----------
303+
XY : (numpolys, numsides, 2) array
303304
"""
304-
305305
# for each poly: 1 for the MOVETO, (numsides-1) for the LINETO, 1 for
306306
# the CLOSEPOLY; the vert for the closepoly is ignored but we still
307307
# need it to keep the codes aligned with the vertices
@@ -316,7 +316,6 @@ def make_compound_path_from_polys(cls, XY):
316316
codes[numsides::stride] = cls.CLOSEPOLY
317317
for i in range(numsides):
318318
verts[i::stride] = XY[:, i]
319-
320319
return cls(verts, codes)
321320

322321
@classmethod

0 commit comments

Comments
 (0)