title: Plot 2D histogram data with pcolormesh
body: <p>I need to plot a binned statistic, as one would get from <a href="https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.binned_statistic_2d.html" rel="nofollow noreferrer"><code>scipy.stats.binned_statistic_2d</code></a>. Basically, that means I have edge values and within-bin data. This also means I <em>cannot</em> (to my knowledge) use <code>plt.hist2d</code>. Here's a code snippet to generate the sort of data I might need to plot:</p>
  
  <pre><code>import numpy as np
  x_edges = np.arange(6)
  y_edges = np.arange(6)
  bin_values = np.random.randn(5, 5)
  </code></pre>
  
  <p>One would imagine that I could use <code>pcolormesh</code> for this, but the issue is that <code>pcolormesh</code> does not allow for bin edge values. The following will only plot the values in bins 1 through 4. The 5th value is excluded, since while pcolormesh "knows" that the value at 4.0 is some value, there is no later value to plot, so the width of the 5th bin is zero.</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  X, Y = np.broadcast_arrays(x_edges[:5, None], y_edges[None, :5])
  
  plt.figure()
  plt.pcolormesh(X, Y, bin_values)
  plt.show()
  </code></pre>
  
  <p>I <em>can</em> get around this with an ugly hack by adding an additional set of values equal to the last values:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  X, Y = np.broadcast_arrays(x_edges[:, None], y_edges[None, :])
  dummy_bin_values = np.zeros([6, 6])
  dummy_bin_values[:5, :5] = bin_values
  dummy_bin_values[5, :] = dummy_bin_values[4, :]
  dummy_bin_values[:, 5] = dummy_bin_values[:, 4]
  
  plt.figure()
  plt.pcolormesh(X, Y, dummy_bin_values)
  plt.show()
  </code></pre>
  
  <p>However, this <em>is</em> an ugly hack. Is there any cleaner way to plot 2D histogram data with bin edge values? "No" is possibly the correct answer, but convince me that's the case if it is.</p>