Two-dimensional histograms are a compact way to summarize robot calibration pairs such as commanded position and measured position, or range and intensity. NumPy's `histogram2d` returns a matrix, but its first dimension corresponds to the first input and its second dimension to the second input. That row-column contract is easy to reverse when the matrix is plotted as an image with horizontal and vertical axes.

This article uses three invented calibration pairs. The numbers are a teaching fixture, not a sensor accuracy claim or a production calibration report. The central check is to compute each bin by hand and then keep the returned edges alongside the matrix.

Define two coordinate arrays

Use x as a commanded position in arbitrary units and y as a measured response:

x = np.array([0.2, 0.8, 1.2])

y = np.array([10., 20., 10.])

x_edges = np.array([0., 1., 2.])

y_edges = np.array([0., 15., 30.])

There are two x bins, [0,1) and [1,2], and two y bins, [0,15) and [15,30]. The final edge is included on the last interval under NumPy's histogram convention. The first two samples fall in x bin zero; the third falls in x bin one. The y values 10, 20, and 10 fall into y bins zero, one, and zero.

Read the returned shape literally

Call:

H, returned_x, returned_y = np.histogram2d(

x, y, bins=[x_edges, y_edges])

The returned `H` has shape (2,2). Its rows are x bins and its columns are y bins:

H = [[1, 1],

[1, 0]]

Row zero counts the two samples whose x values are below 1: one y value in the lower band and one in the upper band. Row one counts the sample whose x value is 1.2: one lower-band observation and no upper-band observation. `returned_x` and `returned_y` equal the supplied edges in this fixture; retaining them still matters when automatic bin construction or floating-point edges are used.

Why a plot can look transposed

Many image displays treat the first matrix dimension as vertical and the second as horizontal. If H is passed to a plotting helper with x along the horizontal axis and y along the vertical axis without checking the library's convention, a reader may see the counts transposed. The underlying histogram is not wrong; the visual mapping is.

Keep the labels explicit: row index i means x in [x_edges[i], x_edges[i+1]) except for the final closed edge, and column index j means y in the corresponding y interval. If a plot requires x horizontal, use the plotting library's documented extent or transpose deliberately and record that operation. Do not silently swap the arrays before calling `histogram2d`, because that changes the shape and the meaning of every cell.

Check boundaries rather than relying on a sketch

Boundary ownership is a frequent source of off-by-one calibration counts. A second fixture can include x values [0, 1, 2] and y values [0, 15, 30]. The exact last-edge rules should be checked against the NumPy documentation and asserted in the installed version before a compliance report uses them. In the primary three-point fixture, no sample lies exactly on an interior boundary, so its expected matrix is unambiguous and isolates the row-column issue.

Histogram counts are not averages

`H` records how many pairs land in each cell. It does not report the mean y response for each x bin, a probability, or a calibration residual. A cell with count two can contain two very different measurements. If the next stage needs a mean or spread, compute that statistic from the original pairs grouped by the same edges and store the count beside it. Normalizing H by its sum gives a mass table, but it does not change the axis contract.

Record edges with the matrix

A robust artifact stores H, x_edges, y_edges, the order of the input arrays, the boundary policy, and the library version. A reviewer can then reconstruct the cell membership without guessing whether rows refer to command or measurement. If a later resize or transpose is needed for visualization, save the transformed matrix under a different name from the analytical H.

The compact verification is:

assert H.shape == (2, 2)

assert np.array_equal(H, [[1., 1.], [1., 0.]])

assert np.array_equal(returned_x, x_edges)

assert np.array_equal(returned_y, y_edges)

For a robot calibration pipeline, this small assertion prevents a large reporting error. `histogram2d(x, y)` means rows for x bins and columns for y bins. Preserve that relationship in tables, plots, exports, and later statistics. Treat the edges and axis order as part of the calibration data contract, not as display metadata that can be reconstructed from a colored image.

The same discipline applies when bins are generated automatically. `histogram_bin_edges` can choose edges from a data range and a named estimator, but the returned edge array is still the authority for interpreting each row and column. Save it with the histogram rather than recording only a bin count. If a later run sees a slightly different range, the automatic edges may change and a visually similar heatmap can represent different intervals. Comparing matrices without comparing their edge arrays can therefore mistake a changed coordinate partition for a changed robot response.

When data are filtered, keep the filter rule beside the histogram too. Removing invalid pairs before calling `histogram2d` changes counts and possibly the automatically selected edges. A transparent report can show the number of input pairs, the number retained, both edge arrays, and the exact call arguments. That makes a sparse calibration cell explainable and prevents a downstream analyst from filling empty bins with an invented zero response.

Sources

[1] NumPy, numpy.histogram2d: https://numpy.org/doc/stable/reference/generated/numpy.histogram2d.html

[2] NumPy, numpy.histogram_bin_edges: https://numpy.org/doc/stable/reference/generated/numpy.histogram_bin_edges.html