{"record":{"id":"31dd8c3a3bda223d","repo":"matplotlib/matplotlib","slug":"axis-s-out-of-bounds","errorCode":null,"errorMessage":"axis(=%s) out of bounds","messagePattern":"axis\\(=(.+?)\\) out of bounds","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/mlab.py","lineNumber":153,"sourceCode":"    ----------\n    x : array or sequence\n        Array or sequence containing the data\n        Can have any dimensionality\n\n    axis : int\n        The axis along which to take the mean.  See `numpy.mean` for a\n        description of this argument.\n\n    See Also\n    --------\n    detrend_linear : Another detrend algorithm.\n    detrend_none : Another detrend algorithm.\n    detrend : A wrapper around all the detrend algorithms.\n    \"\"\"\n    x = np.asarray(x)\n\n    if axis is not None and axis+1 > x.ndim:\n        raise ValueError('axis(=%s) out of bounds' % axis)\n\n    return x - x.mean(axis, keepdims=True)\n\n\ndef detrend_none(x, axis=None):\n    \"\"\"\n    Return *x*: no detrending.\n\n    Parameters\n    ----------\n    x : any object\n        An object containing the data\n\n    axis : int\n        This parameter is ignored.\n        It is included for compatibility with detrend_mean\n\n    See Also","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/mlab.py#L135-L171","documentation":"The built-in detrend implementations (detrend_mean here, likewise detrend_linear and detrend_none) subtract their statistic along axis and validate it first: if axis is not None and axis + 1 > x.ndim, ValueError. Calling the helper directly with a 1-D array and axis=1 is the classic trigger, since the mlab.detrend wrapper is bypassed.","triggerScenarios":"mlab.detrend_mean(x_1d, axis=1); mlab.detrend_linear(np.array([1.0, 2.0]), axis=1); axis constants from multi-channel pipelines plumbed into these helpers for 1-D segments.","commonSituations":"Calling detrend_mean/detrend_linear directly for speed; reusing 2-D preprocessing constants in single-channel scripts; per-segment processing inside psd-like code.","solutions":["Call with axis=None (or axis=0) for 1-D data; both behave identically here","Route through mlab.detrend(x, key='mean', axis=...), which forwards the axis correctly","Check np.ndim(x) before choosing the axis value"],"exampleFix":"import numpy as np\n# before\ny = mlab.detrend_mean(x, axis=1)  # x is 1-D -> ValueError\n# after\ny = mlab.detrend_mean(x, axis=None)","handlingStrategy":"validation","validationCode":"import numpy as np\nif axis is not None and axis >= np.ndim(x):\n    axis = None\ny = mlab.detrend_mean(x, axis=axis)","typeGuard":null,"tryCatchPattern":"try:\n    y = mlab.detrend_mean(x, axis=axis)\nexcept ValueError as err:\n    if 'out of bounds' in str(err):\n        y = mlab.detrend_mean(x, axis=None)\n    else:\n        raise","preventionTips":["For 1-D data always pass axis=None to the detrend_* helpers","Gate axis parameters on np.ndim(x) in any generic preprocessing utility"],"tags":["matplotlib","mlab","detrend","numpy-axis"],"backgroundTag":"axis-out-of-bounds","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","contentChangedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}