diff options
author | Matti Picus <matti.picus@gmail.com> | 2021-05-25 08:45:24 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-25 08:45:24 +0300 |
commit | f9a1f3199623d48a4aeaacdcbae6a1851e125cb4 (patch) | |
tree | 1f2caa43540a105cab0cc2ccf9b8f03be01c0c24 | |
parent | a988464b5050e4d3aa0db90ea649f254db8ed7fb (diff) | |
parent | 7eab53a8ba51089c3debc6a18523587a8ce1ba0a (diff) | |
download | numpy-f9a1f3199623d48a4aeaacdcbae6a1851e125cb4.tar.gz |
Merge pull request #19089 from melissawm/add-hist2d-example
DOC: Add example to histogram2d docstring
-rw-r--r-- | numpy/lib/twodim_base.py | 34 |
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 |