@@ -816,6 +816,7 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
816816 --------
817817 hlines : Add horizontal lines in data coordinates.
818818 axhspan : Add a horizontal span (rectangle) across the axis.
819+ axline : Add a line with an arbitrary slope.
819820
820821 Examples
821822 --------
@@ -899,6 +900,7 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
899900 --------
900901 vlines : Add vertical lines in data coordinates.
901902 axvspan : Add a vertical span (rectangle) across the axis.
903+ axline : Add a line with an abritrary slope.
902904 """
903905
904906 if "transform" in kwargs :
@@ -919,6 +921,63 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs):
919921 self ._request_autoscale_view (scalex = scalex , scaley = False )
920922 return l
921923
924+ @docstring .dedent_interpd
925+ def axline (self , xy1 , xy2 , ** kwargs ):
926+ """
927+ Add an infinitely long straight line that passes through two points.
928+
929+ This draws a straight line "on the screen", regardless of the x and y
930+ scales, and is thus also suitable for drawing exponential decays in
931+ semilog plots, power laws in loglog plots, etc.
932+
933+ Parameters
934+ ----------
935+ xy1, xy2 : (float, float)
936+ Points for the line to pass through.
937+
938+ Returns
939+ -------
940+ :class:`~matplotlib.lines.Line2D`
941+
942+ Other Parameters
943+ ----------------
944+ **kwargs
945+ Valid kwargs are :class:`~matplotlib.lines.Line2D` properties,
946+ with the exception of 'transform':
947+
948+ %(_Line2D_docstr)s
949+
950+ Examples
951+ --------
952+ Draw a thick red line passing through (0, 0) and (1, 1)::
953+
954+ >>> axline((0, 0), (1, 1), linewidth=4, color='r')
955+
956+ See Also
957+ --------
958+ axhline : for horizontal lines
959+ axvline : for vertical lines
960+ """
961+
962+ if "transform" in kwargs :
963+ raise TypeError ("'transform' is not allowed as a kwarg; "
964+ "axline generates its own transform" )
965+ x1 , y1 = xy1
966+ x2 , y2 = xy2
967+ line = mlines ._AxLine ([x1 , x2 ], [y1 , y2 ], ** kwargs )
968+ # Like add_line, but correctly handling data limits.
969+ self ._set_artist_props (line )
970+ if line .get_clip_path () is None :
971+ line .set_clip_path (self .patch )
972+ if not line .get_label ():
973+ line .set_label (f"_line{ len (self .lines )} " )
974+ self .lines .append (line )
975+ line ._remove_method = self .lines .remove
976+ self .update_datalim ([xy1 , xy2 ])
977+
978+ self ._request_autoscale_view ()
979+ return line
980+
922981 @docstring .dedent_interpd
923982 def axhspan (self , ymin , ymax , xmin = 0 , xmax = 1 , ** kwargs ):
924983 """
0 commit comments