|
| 1 | +""" |
| 2 | +This example shows how to use a path patch to draw a bunch of |
| 3 | +rectangles. The technique of using lots of Rectangle instances, or |
| 4 | +the faster method of using PolyCollections, were implemented before we |
| 5 | +had proper paths with moveto/lineto, closepoly etc in mpl. Now that |
| 6 | +we have them, we can draw collections of regularly shaped objects with |
| 7 | +homogeous properties more efficiently with a PathCollection. This |
| 8 | +example makes a histogram -- its more work to set up the vertex arrays |
| 9 | +at the outset, but it should be much faster for large numbers of |
| 10 | +objects |
| 11 | +""" |
| 12 | +import time |
| 13 | +import numpy as np |
| 14 | +import matplotlib |
| 15 | +matplotlib.use('TkAgg') # do this before importing pylab |
| 16 | + |
| 17 | +import matplotlib.pyplot as plt |
| 18 | +import matplotlib.patches as patches |
| 19 | +import matplotlib.path as path |
| 20 | + |
| 21 | +fig = plt.figure() |
| 22 | +ax = fig.add_subplot(111) |
| 23 | + |
| 24 | +# histogram our data with numpy |
| 25 | +data = np.random.randn(1000) |
| 26 | +n, bins = np.histogram(data, 100) |
| 27 | + |
| 28 | +# get the corners of the rectangles for the histogram |
| 29 | +left = np.array(bins[:-1]) |
| 30 | +right = np.array(bins[1:]) |
| 31 | +bottom = np.zeros(len(left)) |
| 32 | +top = bottom + n |
| 33 | +nrects = len(left) |
| 34 | + |
| 35 | +# here comes the tricky part -- we have to set up the vertex and path |
| 36 | +# codes arrays using moveto, lineto and closepoly |
| 37 | + |
| 38 | +# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the |
| 39 | +# CLOSEPOLY; the vert for the closepoly is ignored but we still need |
| 40 | +# it to keep the codes aligned with the vertices |
| 41 | +nverts = nrects*(1+3+1) |
| 42 | +verts = np.zeros((nverts, 2)) |
| 43 | +codes = np.ones(nverts, int) * path.Path.LINETO |
| 44 | +codes[0::5] = path.Path.MOVETO |
| 45 | +codes[4::5] = path.Path.CLOSEPOLY |
| 46 | +verts[0::5,0] = left |
| 47 | +verts[0::5,1] = bottom |
| 48 | +verts[1::5,0] = left |
| 49 | +verts[1::5,1] = top |
| 50 | +verts[2::5,0] = right |
| 51 | +verts[2::5,1] = top |
| 52 | +verts[3::5,0] = right |
| 53 | +verts[3::5,1] = bottom |
| 54 | + |
| 55 | +barpath = path.Path(verts, codes) |
| 56 | +patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5) |
| 57 | +ax.add_patch(patch) |
| 58 | + |
| 59 | +ax.set_xlim(left[0], right[-1]) |
| 60 | +ax.set_ylim(bottom.min(), top.max()) |
| 61 | + |
| 62 | +def animate(): |
| 63 | + tstart = time.time() # for profiling |
| 64 | + # simulate new data coming in |
| 65 | + data = np.random.randn(1000) |
| 66 | + n, bins = np.histogram(data, 100) |
| 67 | + top = bottom + n |
| 68 | + verts[1::5,1] = top |
| 69 | + verts[2::5,1] = top |
| 70 | + fig.canvas.draw() |
| 71 | + |
| 72 | +def run(): |
| 73 | + for i in range(100): |
| 74 | + fig.canvas.manager.window.after(100, animate) |
| 75 | + |
| 76 | + |
| 77 | +fig.canvas.manager.window.after(100, run) |
| 78 | +plt.show() |
0 commit comments