summaryrefslogtreecommitdiff
path: root/numpy/lib/twodim_base.py
diff options
context:
space:
mode:
authorFrank Breitling <frank.breitling@gmx.de>2013-07-13 15:44:49 +0200
committerFrank Breitling <frank.breitling@gmx.de>2013-07-13 15:44:49 +0200
commit5b10b1b38044f0a4431ee587eec186767d07fe93 (patch)
tree58f0add91b4277c97570c12a0d30e98b7c5b2352 /numpy/lib/twodim_base.py
parentb3f37edd68e03dc79a48f38c66e4a757aaa4d8cc (diff)
downloadnumpy-5b10b1b38044f0a4431ee587eec186767d07fe93.tar.gz
Update twodim_base.py
Diffstat (limited to 'numpy/lib/twodim_base.py')
-rw-r--r--numpy/lib/twodim_base.py77
1 files changed, 44 insertions, 33 deletions
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index 9e3377411..17d20e4df 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -584,40 +584,51 @@ l belonging to the samples falling into each bin.
Examples
--------
- 2D-histogram with fixed bin width:
-
- >>> x, y = np.random.randn(2, 100)
- >>> H, xedges, yedges = np.histogram2d(x, y, bins=(5, 8))
- >>> H.shape, xedges.shape, yedges.shape
- ((5, 8), (6,), (9,))
-
- We can use imshow from Matplotlib to visualize this 2D-histogram:
-
- >>> extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
- >>> import matplotlib.pyplot as plt
- >>> plt.imshow(H, extent=extent, interpolation='nearest')
- <matplotlib.image.AxesImage object at ...>
- >>> plt.colorbar()
- <matplotlib.colorbar.Colorbar instance at ...>
- >>> plt.show()
-
-
- 2D-histogram with variable bin size:
-
- >>>import numpy as np, matplotlib.pyplot as plt
- x = np.random.normal(3, 2, 1000)
- y = np.random.normal(3, 1, 1000)
- yedges=xedges=[0,2,3,4,6]
- H, yedges, xedges = np.histogram2d(y,x, bins=(yedges,xedges))
- extent = [xedges[0], xedges[-1], yedges[-1], yedges[0]]
- X,Y = np.meshgrid(xedges, yedges)
- plt.pcolormesh(X, Y,H)
- plt.colorbar()
- plt.show()
-
+ 2D-histogram with variable bin width:
+
+ import matplotlib as ml
+ import matplotlib.pyplot as plt
+ import numpy as np
+
+ # First we define the bin edges
+ xedges = [0, 1, 1.5, 3, 5]
+ yedges = [0, 2, 3, 4, 6]
+
+ # Next we create a histogram H with random bin content
+ x = np.random.normal(3, 1, 100)
+ y = np.random.normal(1, 1, 100)
+ H, xedges, yedges = np.histogram2d(y, x, bins=(xedges, yedges))
+
+ # Or we fill the histogram H with a determined bin content
+ H = np.ones((4, 4)).cumsum().reshape(4, 4)
+ print H[::-1] # This shows the bin content in the order as plotted
+
+ fig = plt.figure(figsize=(7, 3))
+ # Imshow can only do an equidistant representation of bins
+ ax = fig.add_subplot(131)
+ ax.set_title('imshow:\nequidistant')
+ im = plt.imshow(H, interpolation='nearest', origin='low',
+ extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])
+
+ # pcolormesh can displaying exact bin edges
+ ax = fig.add_subplot(132)
+ ax.set_title('pcolormesh:\nexact bin edges')
+ X, Y = np.meshgrid(xedges, yedges)
+ ax.pcolormesh(X, Y, H)
+ ax.set_aspect('equal')
- For interpolated visulization matplotlib provides the NonUniformImage. See
- http://matplotlib.org/examples/pylab_examples/image_nonuniform.html
+ # NonUniformImage displays exact bin edges with interpolation
+ ax = fig.add_subplot(133)
+ ax.set_title('NonUniformImage:\ninterpolated')
+ im = ml.image.NonUniformImage(ax, interpolation='bilinear')
+ xcenters = xedges[:-1] + 0.5 * (xedges[1:] - xedges[:-1])
+ ycenters = yedges[:-1] + 0.5 * (yedges[1:] - yedges[:-1])
+ im.set_data(xcenters, ycenters, H)
+ ax.images.append(im)
+ ax.set_xlim(xedges[0], xedges[-1])
+ ax.set_ylim(yedges[0], yedges[-1])
+ ax.set_aspect('equal')
+ plt.show()
"""
from numpy import histogramdd