When making a hexbin plot in matplotlib, I find that alternating rows of hexagon tiles have different sizes, sometimes significantly so. This demonstration code shows the effect:
from matplotlib import pyplot as plt
from matplotlib import cm as cm
from matplotlib import mlab as ml
import numpy as np
n = 1e5
x = y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z1 = ml.bivariate_normal(X, Y, 2, 2, 0, 0)
Z2 = ml.bivariate_normal(X, Y, 4, 1, 1, 1)
ZD = Z2 - Z1
x = X.ravel()
y = Y.ravel()
z = ZD.ravel()
plt.subplot(111)
plt.hexbin(x, y, C=z, gridsize=30, cmap=cm.jet, bins=None)
plt.axis([x.min(), x.max(), y.min(), y.max()])
cb = plt.colorbar()
cb.set_label('mean value')
plt.show()
In this image, with a gridsize of 30, you can see that alternate rows are squished a bit vertically:
The effect is not very significant, but in this magnified view of the same hexbin plot but with a gridsize of 80, the small rows are nearly half the size of the large rows. (The generated sample data starts to mis-align with the grid, too, but that's an unimportant artifact.)
The hexbin documentation reads:
gridsize: [ 100 | integer ]
The number of hexagons in the x-direction, default is 100. The corresponding number of hexagons in the y-direction is chosen such that the hexagons are approximately regular. Alternatively, gridsize can be a tuple with two elements specifying the number of hexagons in the x-direction and the y-direction.
It only guarantees that the hexagons be "approximately" regular, but it seems that, especially in cases like the 80-gridsize image above, the hexagons could be made a lot closer to regular by reducing the number of rows so small rows could be enlarged and made more regular. Or, the normal-sized rows could be shrunk vertically while the small ones are enlarged vertically, keeping all rows the same height, even if the tiles aren't regularly-shaped.
What is the cause of this irregularity, and can it be avoided?