|
20 | 20 |
|
21 | 21 | # histogram our data with numpy |
22 | 22 | data = np.random.randn(1000) |
23 | | -n, bins = np.histogram(data, 100) |
| 23 | +n, bins = np.histogram(data, 50) |
| 24 | + |
24 | 25 |
|
25 | 26 | # get the corners of the rectangles for the histogram |
26 | 27 | left = np.array(bins[:-1]) |
|
29 | 30 | top = bottom + n |
30 | 31 | nrects = len(left) |
31 | 32 |
|
32 | | -# here comes the tricky part -- we have to set up the vertex and path |
33 | | -# codes arrays using moveto, lineto and closepoly |
34 | | - |
35 | | -# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the |
36 | | -# CLOSEPOLY; the vert for the closepoly is ignored but we still need |
37 | | -# it to keep the codes aligned with the vertices |
38 | | -nverts = nrects*(1+3+1) |
39 | | -verts = np.zeros((nverts, 2)) |
40 | | -codes = np.ones(nverts, int) * path.Path.LINETO |
41 | | -codes[0::5] = path.Path.MOVETO |
42 | | -codes[4::5] = path.Path.CLOSEPOLY |
43 | | -verts[0::5,0] = left |
44 | | -verts[0::5,1] = bottom |
45 | | -verts[1::5,0] = left |
46 | | -verts[1::5,1] = top |
47 | | -verts[2::5,0] = right |
48 | | -verts[2::5,1] = top |
49 | | -verts[3::5,0] = right |
50 | | -verts[3::5,1] = bottom |
51 | | - |
52 | | -barpath = path.Path(verts, codes) |
53 | | -patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5) |
| 33 | +XY = np.zeros((nrects, 4, 2)) |
| 34 | +XY[:,0,0] = left |
| 35 | +XY[:,0,1] = bottom |
| 36 | + |
| 37 | +XY[:,1,0] = left |
| 38 | +XY[:,1,1] = top |
| 39 | + |
| 40 | +XY[:,2,0] = right |
| 41 | +XY[:,2,1] = top |
| 42 | + |
| 43 | +XY[:,3,0] = right |
| 44 | +XY[:,3,1] = bottom |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +barpath = path.Path.make_compound_path_from_polys(XY) |
| 49 | +print barpath.codes[:7], barpath.codes[-7:] |
| 50 | +patch = patches.PathPatch(barpath, facecolor='blue', edgecolor='gray', alpha=0.8) |
54 | 51 | ax.add_patch(patch) |
55 | 52 |
|
56 | 53 | ax.set_xlim(left[0], right[-1]) |
|
0 commit comments