|
4 | 4 | ======================================================== |
5 | 5 |
|
6 | 6 | 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. |
15 | 15 | """ |
16 | 16 |
|
17 | 17 | import numpy as np |
18 | 18 | import matplotlib.pyplot as plt |
19 | 19 | import matplotlib.patches as patches |
20 | 20 | import matplotlib.path as path |
21 | 21 |
|
22 | | -fig, ax = plt.subplots() |
23 | | - |
24 | | -# Fixing random state for reproducibility |
25 | | -np.random.seed(19680801) |
| 22 | +fig, axs = plt.subplots(2) |
26 | 23 |
|
| 24 | +np.random.seed(19680801) # Fixing random state for reproducibility |
27 | 25 |
|
28 | 26 | # histogram our data with numpy |
29 | | - |
30 | 27 | data = np.random.randn(1000) |
31 | 28 | n, bins = np.histogram(data, 50) |
32 | 29 |
|
|
36 | 33 | bottom = np.zeros(len(left)) |
37 | 34 | top = bottom + n |
38 | 35 |
|
39 | | - |
40 | 36 | # we need a (numrects x numsides x 2) numpy array for the path helper |
41 | 37 | # function to build a compound path |
42 | 38 | XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T |
43 | 39 |
|
44 | 40 | # get the Path object |
45 | 41 | barpath = path.Path.make_compound_path_from_polys(XY) |
46 | 42 |
|
47 | | -# make a patch out of it |
| 43 | +# make a patch out of it, don't add a margin at y=0 |
48 | 44 | 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() |
56 | 48 |
|
57 | 49 | ############################################################################# |
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 |
61 | 53 |
|
62 | 54 | nrects = len(left) |
63 | 55 | nverts = nrects*(1+3+1) |
|
76 | 68 |
|
77 | 69 | barpath = path.Path(verts, codes) |
78 | 70 |
|
| 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 | + |
79 | 79 | ############################################################################# |
80 | 80 | # |
81 | 81 | # .. admonition:: References |
|
0 commit comments