From 8b3bd7ebf523b81567f29814bd1d7f407cc5abab Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 23 Nov 2016 20:13:09 -0500 Subject: [PATCH] Add Axes method for drawing infinite lines. --- lib/matplotlib/axes/_axes.py | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 3b1554580f41..e57e4e9faf68 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -793,6 +793,69 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs): self.autoscale_view(scalex=scalex, scaley=False) return l + @docstring.dedent_interpd + def axline(self, slope=1, intercept=0, **kwargs): + """ + Add an infinite line across the axes. + + Parameters + ---------- + slope : scalar, optional, default: 1 + Slope of the line. + + intercept : scalar, optional, default: 0 + Intercept of the line with the y-axis. + + Returns + ------- + :class:`~matplotlib.lines.Line2D` + + Other Parameters + ---------------- + Valid kwargs are :class:`~matplotlib.lines.Line2D` properties, + with the exception of 'transform': + + %(Line2D)s + + Examples + -------- + * draw a thick red line with slope 1 and y-intercept 0:: + + >>> axline(linewidth=4, color='r') + + * draw a default line with slope 1 and y-intercept 1:: + + >>> axline(intercept=1) + + * draw a default line with slope 5 and y-intercept 0:: + + >>> axline(slope=5) + + See Also + -------- + axhline : for strictly horizontal lines + axvline : for strictly vertical lines + + """ + + if "transform" in kwargs: + raise ValueError("'transform' is not allowed as a kwarg; " + "axline generates its own transform.") + + xtrans = mtransforms.BboxTransformTo(self.viewLim) + viewLimT = mtransforms.TransformedBbox( + self.viewLim, + mtransforms.Affine2D().rotate_deg(90).scale(-1, 1)) + ytrans = (mtransforms.BboxTransformTo(viewLimT) + + mtransforms.Affine2D().scale(slope).translate(0, intercept)) + trans = mtransforms.blended_transform_factory(xtrans, ytrans) + + l = mlines.Line2D([0, 1], [0, 1], + transform=trans + self.transData, + **kwargs) + self.add_line(l) + return l + @docstring.dedent_interpd def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs): """