{"record":{"id":"1b58af9da86d9435","repo":"matplotlib/matplotlib","slug":"margin-must-be-greater-than-0-5","errorCode":null,"errorMessage":"margin must be greater than -0.5","messagePattern":"margin must be greater than -0\\.5","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/axes/_base.py","lineNumber":2829,"sourceCode":"        the data range instead of expanding it.\n\n        For example, if your data is in the range [0, 2], a margin of 0.1 will\n        result in a range [-0.2, 2.2]; a margin of -0.1 will result in a range\n        of [0.2, 1.8].\n\n        Parameters\n        ----------\n        m : float greater than -0.5\n\n        See Also\n        --------\n        :ref:`autoscale_margins`\n        matplotlib.axes.Axes.margins\n        matplotlib.axes.Axes.get_xmargin\n\n        \"\"\"\n        if m <= -0.5:\n            raise ValueError(\"margin must be greater than -0.5\")\n        self._xmargin = m\n        self._request_autoscale_view(\"x\")\n        self.stale = True\n\n    def set_ymargin(self, m):\n        \"\"\"\n        Set padding of Y data limits prior to autoscaling.\n\n        *m* times the data interval will be added to each end of that interval\n        before it is used in autoscaling.  If *m* is negative, this will clip\n        the data range instead of expanding it.\n\n        For example, if your data is in the range [0, 2], a margin of 0.1 will\n        result in a range [-0.2, 2.2]; a margin of -0.1 will result in a range\n        of [0.2, 1.8].\n\n        Parameters\n        ----------","sourceCodeStart":2811,"sourceCodeEnd":2847,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/axes/_base.py#L2811-L2847","documentation":"set_xmargin(m) pads the x data interval by m times the interval on each end before autoscaling. A margin of m <= -0.5 shrinks the resulting span to (1 + 2m) <= 0, i.e. zero or negative width, which cannot define usable view limits, so matplotlib raises this ValueError. Valid values are any float strictly greater than -0.5.","triggerScenarios":"ax.set_xmargin(-0.6); indirectly ax.margins(x=-0.6) or ax.margins(-0.6); margin values loaded from config files or computed from data that fall outside (-0.5, inf).","commonSituations":"Using negative margins to clip into the data range via autoscaling; dynamically computed margins (e.g. -0.5 - epsilon from floating-point arithmetic or user sliders) crossing the boundary.","solutions":["Keep m strictly greater than -0.5; use small negatives like -0.1 to trim 10% off each end of the data range.","For zero padding use ax.margins(x=0) instead of trying to negate padding.","For arbitrary limits, skip margins and call ax.set_xlim directly."],"exampleFix":"# before\nax.set_xmargin(-0.6)\n\n# after\nax.set_xmargin(-0.1)  # trims 10% from each end\n# or\nax.margins(x=0)","handlingStrategy":"validation","validationCode":"def safe_xmargin(m):\n    if not (m > -0.5):\n        raise ValueError('x margin must be > -0.5')\n    return m\n\nax.set_xmargin(safe_xmargin(m))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp margin config: m = max(m, -0.49) when values come from users or files.","Remember the model: final span is (1 + 2m) * interval, so m <= -0.5 collapses the range.","Use ax.set_xlim for limits outside what margins can express."],"tags":["matplotlib","margins","autoscale","range-validation"],"backgroundTag":"value-out-of-range","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}