Former-commit-id:a02aeb236c
[formerly9f19e3f712
] [formerlya02aeb236c
[formerly9f19e3f712
] [formerly06a8b51d6d
[formerly 64fa9254b946eae7e61bbc3f513b7c3696c4f54f]]] Former-commit-id:06a8b51d6d
Former-commit-id:8e80217e59
[formerly3360eb6c5f
] Former-commit-id:377dcd10b9
47 lines
1.4 KiB
Python
Executable file
47 lines
1.4 KiB
Python
Executable file
"""
|
|
This example shows how to use a path patch to draw a bunch of
|
|
rectangles. The technique of using lots of Rectangle instances, or
|
|
the faster method of using PolyCollections, were implemented before we
|
|
had proper paths with moveto/lineto, closepoly etc in mpl. Now that
|
|
we have them, we can draw collections of regularly shaped objects with
|
|
homogeous properties more efficiently with a PathCollection. This
|
|
example makes a histogram -- its more work to set up the vertex arrays
|
|
at the outset, but it should be much faster for large numbers of
|
|
objects
|
|
"""
|
|
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.patches as patches
|
|
import matplotlib.path as path
|
|
|
|
fig = plt.figure()
|
|
ax = fig.add_subplot(111)
|
|
|
|
# histogram our data with numpy
|
|
data = np.random.randn(1000)
|
|
n, bins = np.histogram(data, 50)
|
|
|
|
# get the corners of the rectangles for the histogram
|
|
left = np.array(bins[:-1])
|
|
right = np.array(bins[1:])
|
|
bottom = np.zeros(len(left))
|
|
top = bottom + n
|
|
|
|
|
|
# we need a (numrects x numsides x 2) numpy array for the path helper
|
|
# function to build a compound path
|
|
XY = np.array([[left,left,right,right], [bottom,top,top,bottom]]).T
|
|
|
|
# get the Path object
|
|
barpath = path.Path.make_compound_path_from_polys(XY)
|
|
|
|
# make a patch out of it
|
|
patch = patches.PathPatch(barpath, facecolor='blue', edgecolor='gray', alpha=0.8)
|
|
ax.add_patch(patch)
|
|
|
|
# update the view limits
|
|
ax.set_xlim(left[0], right[-1])
|
|
ax.set_ylim(bottom.min(), top.max())
|
|
|
|
plt.show()
|