<p dir="auto">I found that the example code for the histogram2d has an issue when adapted. The code below works for a symmetric amount of bins. But if one changes the number of bins (e.g. removing a bin on <a href="https://github.com/numpy/numpy/blob/4092a9e160cc247a4a45724579a0c829733688ca/numpy/lib/twodim_base.py#L655">line 655</a> <code class="notranslate"> xedges = [0, 1, 1.5, 3])</code> in only one dimensions, the example raises an exception:</p>
<p dir="auto"><code class="notranslate">TypeError: Axes don't match array shape</code></p>
<p dir="auto">My understanding of this error: This is due to the fact that <code class="notranslate"> im.set_data(xcenters, ycenters, H)</code> on line 696 is erroneous. The <code class="notranslate">set_data</code> functions behaves counter-intuitive and sets data in screen coordinates and not in plot coordinates, see theses <a href="https://github.com/matplotlib/matplotlib/blob/e18af550cf20c8c5e4499720afee2c1e38c020db/lib/matplotlib/image.py#L945">lines</a> in matplotlib. The fix for this is to invert the xedges, yedges order in the <code class="notranslate">set_data</code> call. With that adjustment the code works fine for an asymmetric number of bins. Although, I don't understand why the data is displayed correctly in the symmetric bin number case.</p>
<p dir="auto">I am happy to fix this (by inverting the order), but am unsure about how to move on. Is it OK to just go along, fix it, and create a pull request? Can anyone guide me along?</p>
<p dir="auto"></p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/numpy/numpy/blob/4092a9e160cc247a4a45724579a0c829733688ca/numpy/lib/twodim_base.py#L689-L702">numpy/numpy/lib/twodim_base.py</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 689 to 702
      in
      <a data-pjax="true" class="commit-tease-sha" href="/numpy/numpy/commit/4092a9e160cc247a4a45724579a0c829733688ca">4092a9e</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L689" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="689"></td>
          <td id="LC689" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    NonUniformImage displays exact bin edges with interpolation:</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L690" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="690"></td>
          <td id="LC690" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s"></span> </td>
        </tr>

        <tr class="border-0">
          <td id="L691" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="691"></td>
          <td id="LC691" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; ax = fig.add_subplot(133)</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L692" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="692"></td>
          <td id="LC692" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; ax.set_title('NonUniformImage: interpolated')</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L693" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="693"></td>
          <td id="LC693" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; im = mpl.image.NonUniformImage(ax, interpolation='bilinear')</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L694" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="694"></td>
          <td id="LC694" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; xcenters = xedges[:-1] + 0.5 * (xedges[1:] - xedges[:-1])</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L695" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="695"></td>
          <td id="LC695" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; ycenters = yedges[:-1] + 0.5 * (yedges[1:] - yedges[:-1])</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L696" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="696"></td>
          <td id="LC696" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; im.set_data(xcenters, ycenters, H)</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L697" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="697"></td>
          <td id="LC697" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; ax.images.append(im)</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L698" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="698"></td>
          <td id="LC698" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; ax.set_xlim(xedges[0], xedges[-1])</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L699" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="699"></td>
          <td id="LC699" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; ax.set_ylim(yedges[0], yedges[-1])</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L700" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="700"></td>
          <td id="LC700" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; ax.set_aspect('equal')</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L701" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="701"></td>
          <td id="LC701" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s">    &gt;&gt;&gt; plt.show()</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L702" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="702"></td>
          <td id="LC702" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s"></span> </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
<p dir="auto">Hi all,</p>
<p dir="auto">np.histogram2d online documentation gives an non-functioning example.</p>
<p dir="auto">Documentation page:<br>
<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html" rel="nofollow">http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html</a></p>
<p dir="auto">Non-functioning example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="pcolormesh can display exact bin edges:
&gt;&gt;&gt; ax = fig.add_subplot(132)
&gt;&gt;&gt; ax.set_title('pcolormesh: exact bin edges')
&gt;&gt;&gt; X, Y = np.meshgrid(xedges, yedges)
&gt;&gt;&gt; ax.pcolormesh(X, Y, H)
&gt;&gt;&gt; ax.set_aspect('equal')"><pre class="notranslate"><code class="notranslate">pcolormesh can display exact bin edges:
&gt;&gt;&gt; ax = fig.add_subplot(132)
&gt;&gt;&gt; ax.set_title('pcolormesh: exact bin edges')
&gt;&gt;&gt; X, Y = np.meshgrid(xedges, yedges)
&gt;&gt;&gt; ax.pcolormesh(X, Y, H)
&gt;&gt;&gt; ax.set_aspect('equal')
</code></pre></div>
<p dir="auto">Problem with example:<br>
numpy.histogram2d returns an array H shaped (nx, ny).<br>
numpy.meshgrid returns X, Y arrays shaped (ny, nx).   [When called with the default Cartesian indexing]<br>
Therefore, X,Y are incompatible with H for pcolormesh, which gives an error.</p>
<p dir="auto">Proposed updated example (transpose H):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="pcolormesh can display exact bin edges:
&gt;&gt;&gt; ax = fig.add_subplot(132)
&gt;&gt;&gt; ax.set_title('pcolormesh: exact bin edges')
&gt;&gt;&gt; X, Y = np.meshgrid(xedges, yedges)
&gt;&gt;&gt; ax.pcolormesh(X, Y, H.T)
&gt;&gt;&gt; ax.set_aspect('equal')"><pre class="notranslate"><code class="notranslate">pcolormesh can display exact bin edges:
&gt;&gt;&gt; ax = fig.add_subplot(132)
&gt;&gt;&gt; ax.set_title('pcolormesh: exact bin edges')
&gt;&gt;&gt; X, Y = np.meshgrid(xedges, yedges)
&gt;&gt;&gt; ax.pcolormesh(X, Y, H.T)
&gt;&gt;&gt; ax.set_aspect('equal')
</code></pre></div>