summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormelissawm <melissawm@gmail.com>2021-05-24 22:48:37 -0300
committermelissawm <melissawm@gmail.com>2021-05-24 22:48:37 -0300
commit7eab53a8ba51089c3debc6a18523587a8ce1ba0a (patch)
tree30e8bfa20f841e695ab6fdc7f817c186aff9401a
parentfe3d717080e812383900507168a6bd7093c4e434 (diff)
downloadnumpy-7eab53a8ba51089c3debc6a18523587a8ce1ba0a.tar.gz
DOC: Add example to histogram2d docstring
-rw-r--r--numpy/lib/twodim_base.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index fd8c73573..ac5fe1c6b 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -737,6 +737,40 @@ def histogram2d(x, y, bins=10, range=None, normed=None, weights=None,
>>> ax.images.append(im)
>>> plt.show()
+ It is also possible to construct a 2-D histogram without specifying bin
+ edges:
+
+ >>> # Generate non-symmetric test data
+ >>> n = 10000
+ >>> x = np.linspace(1, 100, n)
+ >>> y = 2*np.log(x) + np.random.rand(n) - 0.5
+ >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges
+ >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
+
+ Now we can plot the histogram using
+ :func:`pcolormesh <matplotlib.pyplot.pcolormesh>`, and a
+ :func:`hexbin <matplotlib.pyplot.hexbin>` for comparison.
+
+ >>> # Plot histogram using pcolormesh
+ >>> fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True)
+ >>> ax1.pcolormesh(xedges, yedges, H, cmap='rainbow')
+ >>> ax1.plot(x, 2*np.log(x), 'k-')
+ >>> ax1.set_xlim(x.min(), x.max())
+ >>> ax1.set_ylim(y.min(), y.max())
+ >>> ax1.set_xlabel('x')
+ >>> ax1.set_ylabel('y')
+ >>> ax1.set_title('histogram2d')
+ >>> ax1.grid()
+
+ >>> # Create hexbin plot for comparison
+ >>> ax2.hexbin(x, y, gridsize=20, cmap='rainbow')
+ >>> ax2.plot(x, 2*np.log(x), 'k-')
+ >>> ax2.set_title('hexbin')
+ >>> ax2.set_xlim(x.min(), x.max())
+ >>> ax2.set_xlabel('x')
+ >>> ax2.grid()
+
+ >>> plt.show()
"""
from numpy import histogramdd